diff options
Diffstat (limited to 'internal/handlers/shopping_template_test.go')
| -rw-r--r-- | internal/handlers/shopping_template_test.go | 38 |
1 files changed, 28 insertions, 10 deletions
diff --git a/internal/handlers/shopping_template_test.go b/internal/handlers/shopping_template_test.go index 06f63b3..07baffc 100644 --- a/internal/handlers/shopping_template_test.go +++ b/internal/handlers/shopping_template_test.go @@ -31,6 +31,24 @@ func renderShoppingTab(t *testing.T, data any) string { return buf.String() } +// renderShoppingItemsContent renders just the swapped-in portion of the tab. +func renderShoppingItemsContent(t *testing.T, data any) string { + t.Helper() + dir := filepath.Join("..", "..", "web", "templates") + tmpl, err := template.New("").Funcs(templateFuncMap()).ParseGlob(filepath.Join(dir, "*.html")) + if err != nil { + t.Fatalf("parsing top-level templates: %v", err) + } + if _, err := tmpl.ParseGlob(filepath.Join(dir, "partials", "*.html")); err != nil { + t.Fatalf("parsing partials: %v", err) + } + var buf bytes.Buffer + if err := tmpl.ExecuteTemplate(&buf, "shopping-items-content", data); err != nil { + t.Fatalf("executing shopping-items-content: %v", err) + } + return buf.String() +} + func twoStoreTabData() any { return struct { Stores []models.ShoppingStore @@ -73,21 +91,21 @@ func TestShoppingTab_InlineAddScrollsToItsOwnStore(t *testing.T) { } } -// Every inline add form must carry a scroll target. A form without one is the -// bug, and it is easy to reintroduce by adding a new store section by copy. -func TestShoppingTab_EveryInlineAddFormHasAScrollTarget(t *testing.T) { - html := renderShoppingTab(t, twoStoreTabData()) +// Every form that lives *inside* the swap target must carry a scroll modifier. +// Those forms are destroyed by their own swap, and without an explicit target +// the page lands at the bottom. Forms outside the target (the store-create +// form) do not have this problem and are excluded by rendering only the items +// content here. +func TestShoppingItemsContent_EveryFormInsideTheSwapTargetScrolls(t *testing.T) { + html := renderShoppingItemsContent(t, twoStoreTabData()) - // Count the class attribute specifically — a bare "inline-add-form" also - // matches the string literal in the page's own JS handler. - forms := strings.Count(html, `class="inline-add-form`) + forms := strings.Count(html, `hx-target="#shopping-items-list"`) scrolls := strings.Count(html, "scroll:#store-") if forms == 0 { - t.Fatal("expected at least one inline add form in the grouped view") + t.Fatal("expected forms inside the items content") } if forms != scrolls { - t.Errorf("found %d inline add forms but %d scroll targets; every form needs one", - forms, scrolls) + t.Errorf("%d forms target the items list but only %d carry a scroll target", forms, scrolls) } } |
