Skip to content
quarry Artifact [98496c2fbc]
Login

Artifact [98496c2fbc]

Artifact 98496c2fbc63e45cf492f7ccda11d8110f89f9981a82cb9db9a03d511e784e0c:


package web

import (
	"bytes"
	"net/http/httptest"
	"strings"
	"testing"

	"craftdesign.group/quarry/internal/api"
	"craftdesign.group/quarry/internal/ops"
)

// The explore tabs are pages, and with script a click morphs the next
// one in under a view transition instead of reloading: every tab carries
// the morph, the strip carries the back-button morph, and a page fetched
// by script answers with the header that asks for the transition.
func TestExploreTabsMorphBetweenPages(t *testing.T) {
	var b bytes.Buffer
	if err := explorePage.Execute(&b, pageData{Title: "Home", TeamsTotal: 2}); err != nil {
		t.Fatal(err)
	}
	got := b.String()
	nav := got[strings.Index(got, `<nav class="tabs"`):]
	nav = nav[:strings.Index(nav, "</nav>")]
	if n := strings.Count(nav, string(pageMorphAttr)); n != 3 {
		t.Errorf("FAIL: %d of 3 tabs morph", n)
	}
	if !strings.Contains(nav, string(pageMorphBackAttr)) {
		t.Error("FAIL: back does not morph the strip's pages")
	}

	r := ops.NewRegistry()
	if err := api.RegisterAll(r, api.Deps{Version: "v", Repos: api.NewRepos(map[string]*api.Repo{})}); err != nil {
		t.Fatal(err)
	}
	h := Handler(r, nil, nil)
	for want, script := range map[string]bool{"true": true, "": false} {
		req := httptest.NewRequest("GET", "/people", nil)
		if script {
			req.Header.Set("Datastar-Request", "true")
		}
		rec := httptest.NewRecorder()
		h.ServeHTTP(rec, req)
		if got := rec.Header().Get("Datastar-Use-View-Transition"); got != want {
			t.Errorf("FAIL: script=%v asks for transition %q, want %q", script, got, want)
		}
	}
	t.Log("ok: tabs morph, back morphs, and a scripted fetch asks for the transition")
}