Files
Spaghet.io/src/components/__tests__/list-islands.test.ts
T
spaghet fb8e42f632 refactor: consolidate blog and project filtering into shared component
The /blog and /projects listings previously used byte-identical copies of the same
filtering island, which meant bugs like inconsistent AND-vs-OR tag semantics,
duplicate-case tags, and a missing empty state were duplicated across both.

This commit introduces `FilteredList`, a single shared Solid component that:

- Unifies tag, keyword, and sort filtering for both collections
- Seeds initial filter state from the SSR request URL to avoid hydration mismatches
- Syncs active filters into the address bar via `history.replaceState` so filtered
  views survive refreshes and can be shared
- Restores filters on browser back/forward navigation (`popstate`)
- Case-folds and dedupes tags when parsing URL params

Filter logic (collectTags, filterEntries, sortKeys, URL param encoding) is extracted
into `src/lib/filters.ts` with full unit test coverage.
2026-11-12 22:38:58 -05:00

154 lines
5.6 KiB
TypeScript

// src/components/__tests__/list-islands.test.ts
import { describe, it, expect } from "vitest"
import { renderToString } from "solid-js/web"
import FilteredList from "@components/FilteredList"
import type { FilterState, ListEntry } from "@lib/filters"
/**
* Renders the shared listing island through solid-js/web's server renderer,
* which is the same path Astro takes for `output: "server"` + `client:load`.
*
* The regression locked in here is that the list must exist in the SERVER HTML:
* the original `createEffect` + signal version rendered "SHOWING 0 OF 3" with an
* empty <ul>, so any hydration failure hid every entry. The second regression is
* the case-duplicated tag list, which made /blog and /projects render two
* checkboxes that did exactly the same thing.
*/
function entry(id: string, tags: string[], overrides: Partial<ListEntry> = {}): ListEntry {
return {
id,
title: `Entry ${id}`,
summary: `Summary ${id}`,
date: "2026-01-01T00:00:00.000Z",
tags,
...overrides,
}
}
const posts: ListEntry[] = [
entry("a", ["CSS", "Html"], { title: "Alpha", date: "2025-01-01T00:00:00.000Z" }),
entry("b", ["CSS", "css"], { title: "Bravo", date: "2026-01-01T00:00:00.000Z" }),
entry("c", ["html"], { title: "Charlie", date: "2025-06-01T00:00:00.000Z" }),
]
/**
* Solid's SSR output interleaves hydration markers around every dynamic value
* (`SHOWING <!--$-->3<!--/--> OF …`). Stripping comments keeps the assertions
* about real content instead of about marker placement.
*/
function render(props: {
entries?: ListEntry[]
collection?: "blog" | "projects"
initial?: FilterState
}): string {
const html = renderToString(() =>
FilteredList({
entries: props.entries ?? posts,
collection: props.collection ?? "blog",
initial: props.initial,
})
)
return html.replace(/<!--.*?-->/g, "")
}
describe("FilteredList island (SSR render)", () => {
it("should render every entry in the server HTML with no filter applied", () => {
const html = render({})
expect(html).toContain("SHOWING 3 OF 3 POSTS")
for (const item of posts) {
expect(html).toContain(`href="/blog/${item.id}"`)
}
})
it("should not regress to an empty list when no initial state is passed", () => {
// Guards the createEffect bug specifically: an untouched filter must show all.
expect(render({})).not.toContain("SHOWING 0")
})
it("should apply initial state server-side so a filtered deep link renders filtered", () => {
const html = render({ initial: { tags: ["css"], query: "", sort: "newest" } })
expect(html).toContain("SHOWING 2 OF 3 POSTS")
expect(html).toContain('href="/blog/a"')
expect(html).toContain('href="/blog/b"')
expect(html).not.toContain('href="/blog/c"')
})
it("should render one checkbox per tag, deduped across spellings", () => {
// `CSS`/`css` and `html`/`Html` collapse to two options, not four.
const html = render({})
expect(html).toContain(">CSS</span>")
expect(html).toContain(">Html</span>")
expect(html.match(/aria-pressed/g)).toHaveLength(2)
})
it("should show a per-tag count next to each option", () => {
// css -> a, b; html -> a, c. Assert on the rendered counts rather than on
// the exact class string so the test survives a restyle.
const html = render({})
const counts = [...html.matchAll(/<\/span><span[^>]*>(\d+)<\/span>/g)].map((m) => m[1])
expect(counts).toHaveLength(2)
expect(counts.every((count) => count === "2")).toBe(true)
})
it("should render the keyword input, sort select and live count region", () => {
const html = render({})
expect(html).toContain('id="filter-q"')
expect(html).toContain('id="filter-sort"')
expect(html).toContain('aria-live="polite"')
expect(html).toContain('role="group"')
expect(html).toContain("Newest first")
})
it("should sort by the requested key in the server HTML", () => {
const html = render({ initial: { tags: [], query: "", sort: "az" } })
expect(html.indexOf('href="/blog/a"')).toBeLessThan(html.indexOf('href="/blog/b"'))
})
it("should render an empty state instead of a blank list when nothing matches", () => {
const html = render({ initial: { tags: [], query: "zzzz", sort: "newest" } })
expect(html).toContain("SHOWING 0 OF 3 POSTS")
expect(html).toContain("No posts match these filters.")
expect(html).toContain("Clear filters")
})
it("should offer Clear all only once a filter is active", () => {
expect(render({})).not.toContain("Clear all")
expect(render({ initial: { tags: ["css"], query: "", sort: "newest" } })).toContain("Clear all")
})
it("should label the count PROJECTS for the projects collection", () => {
const html = render({
entries: [entry("x", ["react"]), entry("y", ["react", "rust"])],
collection: "projects",
})
expect(html).toContain("SHOWING 2 OF 2 PROJECTS")
expect(html).toContain('href="/projects/x"')
expect(html).toContain('href="/projects/y"')
})
it("should render an empty collection without throwing", () => {
// A collection where every entry is still a draft: no tags to list, nothing
// to show, and the empty state must carry the panel.
const html = render({ entries: [] })
expect(html).toContain("SHOWING 0 OF 0 POSTS")
expect(html).toContain("No posts match these filters.")
expect(html.match(/aria-pressed/g)).toBeNull()
})
it("should render entries that carry no tags at all", () => {
const html = render({ entries: [entry("solo", [])] })
expect(html).toContain("SHOWING 1 OF 1 POSTS")
expect(html).toContain('href="/blog/solo"')
})
})