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.
This commit is contained in:
1 parent
9e157a4fa3
commit
fb8e42f632
11 files changed
+811
-266
No files matched your search
@@ -1,83 +1,153 @@
|
||||
// src/components/__tests__/list-islands.test.ts
|
||||
import { describe, it, expect } from "vitest"
|
||||
import { renderToString } from "solid-js/web"
|
||||
import Blog from "@components/Blog"
|
||||
import Projects from "@components/Projects"
|
||||
import type { BlogPost, Project } from "@lib/types"
|
||||
import FilteredList from "@components/FilteredList"
|
||||
import type { FilterState, ListEntry } from "@lib/filters"
|
||||
|
||||
/**
|
||||
* These render the islands through solid-js/web's server renderer, which is
|
||||
* the same path Astro takes for `output: "server"` + `client:load`. That is
|
||||
* the point: the regression being locked in here is that the filtered list
|
||||
* must exist in the SERVER HTML. The old `createEffect` + signal version
|
||||
* rendered "SHOWING 0 OF 3" with an empty <ul> because effects never run
|
||||
* during SSR, so any island/hydration failure hid every post.
|
||||
* 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 post(id: string, tags: string[]): BlogPost {
|
||||
function entry(id: string, tags: string[], overrides: Partial<ListEntry> = {}): ListEntry {
|
||||
return {
|
||||
id,
|
||||
title: `Post ${id}`,
|
||||
title: `Entry ${id}`,
|
||||
summary: `Summary ${id}`,
|
||||
date: "2026-01-01T00:00:00.000Z",
|
||||
tags,
|
||||
draft: false,
|
||||
content: "",
|
||||
template: "default",
|
||||
featured: false,
|
||||
...overrides,
|
||||
}
|
||||
}
|
||||
|
||||
function project(id: string, tags: string[]): Project {
|
||||
return {
|
||||
id,
|
||||
title: `Project ${id}`,
|
||||
summary: `Summary ${id}`,
|
||||
date: "2026-01-01T00:00:00.000Z",
|
||||
tags,
|
||||
draft: false,
|
||||
content: "",
|
||||
}
|
||||
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("Blog island (SSR render)", () => {
|
||||
const data = [post("a", ["css", "html"]), post("b", ["css"]), post("c", ["html"])]
|
||||
describe("FilteredList island (SSR render)", () => {
|
||||
it("should render every entry in the server HTML with no filter applied", () => {
|
||||
const html = render({})
|
||||
|
||||
it("should render every post in the server HTML with no filter applied", () => {
|
||||
const html = renderToString(() => Blog({ data, tags: ["css", "html"] }))
|
||||
|
||||
expect(html).toContain("SHOWING <!--$-->3<!--/--> OF <!--$-->3<!--/--> POSTS")
|
||||
for (const entry of data) {
|
||||
expect(html).toContain(`href="/blog/${entry.id}"`)
|
||||
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 the filter signal is untouched", () => {
|
||||
// Guards the createEffect bug specifically: an empty filter must still show all.
|
||||
const html = renderToString(() => Blog({ data, tags: [] }))
|
||||
expect(html).not.toContain("SHOWING <!--$-->0")
|
||||
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 render the tag filter buttons server-side", () => {
|
||||
const html = renderToString(() => Blog({ data, tags: ["css", "html"] }))
|
||||
expect(html).toContain(">css<!--/--></button>")
|
||||
expect(html).toContain(">html<!--/--></button>")
|
||||
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"')
|
||||
})
|
||||
})
|
||||
|
||||
describe("Projects island (SSR render)", () => {
|
||||
const data = [project("x", ["react"]), project("y", ["react", "rust"])]
|
||||
it("should render one checkbox per tag, deduped across spellings", () => {
|
||||
// `CSS`/`css` and `html`/`Html` collapse to two options, not four.
|
||||
const html = render({})
|
||||
|
||||
it("should render every project in the server HTML", () => {
|
||||
const html = renderToString(() => Projects({ data, tags: ["react", "rust"] }))
|
||||
expect(html).toContain(">CSS</span>")
|
||||
expect(html).toContain(">Html</span>")
|
||||
expect(html.match(/aria-pressed/g)).toHaveLength(2)
|
||||
})
|
||||
|
||||
expect(html).toContain("SHOWING <!--$-->2<!--/--> OF <!--$-->2<!--/--> PROJECTS")
|
||||
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 tolerate entries without optional demoUrl/repoUrl", () => {
|
||||
// The content schema marks both optional; the memo must not throw on SSR.
|
||||
expect(() => renderToString(() => Projects({ data, tags: [] }))).not.toThrow()
|
||||
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"')
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user