feat: replace CJS require with static imports, add comprehensive layout system tests

This commit is contained in:
saberzero1
2026-07-27 20:23:31 +02:00
parent 6f27207fbf
commit 508f73a33f
9 changed files with 1096 additions and 183 deletions
+67 -1
View File
@@ -1,6 +1,6 @@
import test, { describe } from "node:test"
import assert from "node:assert"
import { resolveLayout } from "./dispatcher"
import { collectComponents, resolveLayout } from "./dispatcher"
import { QuartzPageTypePluginInstance } from "../types"
import { QuartzComponent } from "../../components/types"
@@ -62,6 +62,26 @@ describe("resolveLayout", () => {
)
assert.deepStrictEqual(result.header, [])
})
test("all array slots default to [] when sharedDefaults only has head", () => {
const result = resolveLayout(makePageType(), { head: StubHead }, {})
assert.deepStrictEqual(result.header, [])
assert.deepStrictEqual(result.left, [])
assert.deepStrictEqual(result.right, [])
assert.deepStrictEqual(result.beforeBody, [])
assert.deepStrictEqual(result.afterBody, [])
assert.deepStrictEqual(result.footer, [])
})
test("preserves component references through override", () => {
const result = resolveLayout(
makePageType(),
{ head: StubHead, footer: [StubA, StubB] },
{},
)
assert.strictEqual(result.footer[0], StubA)
assert.strictEqual(result.footer[1], StubB)
})
})
describe("resolveLayout frame resolution", () => {
@@ -83,4 +103,50 @@ describe("resolveLayout frame resolution", () => {
const result = resolveLayout(makePageType(), { head: StubHead }, {})
assert.strictEqual(result.frame, "default")
})
test("defaults to 'default' when byPageType entry exists but has no frame", () => {
const result = resolveLayout(
makePageType(),
{ head: StubHead },
{ content: { left: [StubA] } },
)
assert.strictEqual(result.frame, "default")
})
})
describe("collectComponents", () => {
test("collects all unique components across page types", () => {
const pageTypes = [makePageType(), makePageType({ layout: "landing" })]
const sharedDefaults = { head: StubHead }
const byPageType = {
content: { footer: [StubA] },
landing: { footer: [StubB] },
}
const result = collectComponents(pageTypes, sharedDefaults, byPageType)
assert.ok(result.includes(StubA))
assert.ok(result.includes(StubB))
})
test("deduplicates shared components", () => {
const pageTypes = [makePageType(), makePageType({ layout: "landing" })]
const sharedDefaults = { head: StubHead }
const byPageType = {
content: { left: [StubA] },
landing: { left: [StubA] },
}
const result = collectComponents(pageTypes, sharedDefaults, byPageType)
const matches = result.filter((component) => component === StubA)
assert.strictEqual(matches.length, 1)
})
test("handles empty footer and header arrays", () => {
const pageTypes = [makePageType({ layout: "empty" })]
const sharedDefaults = { head: StubHead }
const byPageType = { empty: { footer: [], header: [] } }
const result = collectComponents(pageTypes, sharedDefaults, byPageType)
assert.ok(result.every((component) => component))
})
})
+2 -1
View File
@@ -37,7 +37,8 @@ export function resolveLayout(
}
}
function collectComponents(
/** @internal Exported for testing only. */
export function collectComponents(
pageTypes: QuartzPageTypePluginInstance[],
sharedDefaults: Partial<FullPageLayout>,
byPageType: Record<string, Partial<FullPageLayout>>,