Refactor components and add tests for improved functionality and coverage

This commit is contained in:
spaghet committed 2026-11-04 22:16:17 -05:00
1 parent bbb76eaa31
commit ab6cd7c162
13 files changed
+253 -54

No files matched your search

@@ -0,0 +1,83 @@
// 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"
/**
* 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.
*/
function post(id: string, tags: string[]): BlogPost {
return {
id,
title: `Post ${id}`,
summary: `Summary ${id}`,
date: "2026-01-01T00:00:00.000Z",
tags,
draft: false,
content: "",
template: "default",
featured: false,
}
}
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: "",
}
}
describe("Blog island (SSR render)", () => {
const data = [post("a", ["css", "html"]), post("b", ["css"]), post("c", ["html"])]
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}"`)
}
})
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 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>")
})
})
describe("Projects island (SSR render)", () => {
const data = [project("x", ["react"]), project("y", ["react", "rust"])]
it("should render every project in the server HTML", () => {
const html = renderToString(() => Projects({ data, tags: ["react", "rust"] }))
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()
})
})