import { describe, test, expect, beforeEach } from "vitest" import { Slots } from "../src/slots/slots" import { SLOT_EVENT } from "../src/shared/events" import type { SlotEventDetail } from "../src/types" function nth(html: string, occurrence: number): string { return html.replace(/(/g, `$1:${occurrence}-->`) } const FILE = "app/views/posts/index.html.erb" const EMPTY_COND = `
` describe("a branch whose markup was never in the DOM", () => { beforeEach(() => { document.body.innerHTML = "" }) test("an untaken conditional holds an addressable but empty position", () => { const index = new Slots() document.body.innerHTML = EMPTY_COND index.scan(document.body) const slot = index.slot(FILE, 0)! expect(slot.type).toBe("conditional") expect(slot.branch).toBeNull() expect(index.rangeOf(slot).toString()).toBe("") expect(index.descendantsOf(slot)).toEqual([]) }) test("the branch's own markers arrive with its html and become addressable", () => { const index = new Slots() document.body.innerHTML = EMPTY_COND index.scan(document.body) const slot = index.slot(FILE, 0)! index.update(slot, `secret`) expect(index.slot(FILE, 3)?.type).toBe("child") expect(index.rangeOf(index.slot(FILE, 3)!).toString()).toBe("secret") expect(index.slot(FILE, 3)?.parent).toBe(slot) expect(index.slot(FILE, 0)?.branch).toBe(1) }) }) describe("markup parked in a template", () => { beforeEach(() => { document.body.innerHTML = "" }) test("does not index slots inside a template element", () => { const index = new Slots() document.body.innerHTML = ` ` index.scan(document.body) expect(index.slot(FILE, 0)?.type).toBe("conditional") expect(index.slot(FILE, 3), "the parked branch must not be addressable").toBeNull() expect(index.descendantsOf(index.slot(FILE, 0)!)).toEqual([]) }) test("becomes addressable once the parked markup is moved into the document", () => { const index = new Slots() document.body.innerHTML = ` secret ` index.scan(document.body) expect(index.slot(FILE, 3)).toBeNull() const parked = index.parked(FILE, "0:1")!.cloneNode(true) as DocumentFragment const range = index.rangeOf(index.slot(FILE, 0)!) range.insertNode(parked) index.scan(document.body) expect(index.slot(FILE, 3)?.type).toBe("child") expect(index.rangeOf(index.slot(FILE, 3)!).toString()).toBe("secret") }) }) describe("parked statics once they have been read", () => { beforeEach(() => { document.body.innerHTML = "" }) const page = `Hello` test("leaves the document, and leaves the rendered markers alone", () => { const index = new Slots() document.body.innerHTML = page index.scan(document.body) expect(document.querySelector("template")).toBeNull() expect(index.slot(FILE, 0)?.type).toBe("conditional") expect(index.regionsFor(FILE)).toHaveLength(1) expect(index.parked(FILE, "0:1")?.textContent).toBe("Hello") }) test("cannot be copied into the page by an update that spans it", () => { const index = new Slots() document.body.innerHTML = page index.scan(document.body) const region = index.regionsFor(FILE)[0] const range = document.createRange() range.setStartAfter(region.ranges[0].start) range.setEndBefore(region.ranges[0].end!) expect(range.cloneContents().querySelector("template")).toBeNull() }) test("stays registered when the rendering that carried it is replaced", () => { const index = new Slots() document.body.innerHTML = page index.scan(document.body) document.body.innerHTML = page index.prune() index.scan(document.body) expect(document.querySelector("template")).toBeNull() expect(index.materialize(FILE, "0:1")?.textContent).toBe("Hello") }) }) describe("rendering a branch that never rendered, without a round trip", () => { beforeEach(() => { document.body.innerHTML = "" }) const page = ` Hello ` test("parks the statics without making them addressable", () => { const index = new Slots() document.body.innerHTML = page index.scan(document.body) expect(index.parkedKeys(FILE)).toEqual(["0:1"]) expect(index.slot(FILE, 3)).toBeNull() }) test("builds the branch from the statics and the values alone", () => { const index = new Slots() document.body.innerHTML = page index.scan(document.body) const built = index.materialize(FILE, "0:1", { 3: "Marco" })! index.update(index.slot(FILE, 0)!, "") index.rangeOf(index.slot(FILE, 0)!).insertNode(built) index.scan(document.body) expect(document.querySelector("b")?.textContent).toBe("Hello Marco") expect(index.slot(FILE, 3)?.type).toBe("child") expect(index.slot(FILE, 0)?.branch).toBe(1) }) test("keeps one copy of the statics however many times the template rendered", () => { const index = new Slots() document.body.innerHTML = page + nth(page, 1) + nth(page, 2) index.scan(document.body) expect(index.regionsFor(FILE)).toHaveLength(3) expect(index.parkedKeys(FILE)).toEqual(["0:1"]) }) test("leaves a slot alone when the server said nothing about it", () => { const index = new Slots() document.body.innerHTML = page index.scan(document.body) const built = index.materialize(FILE, "0:1", {})! expect(built.textContent).toContain("Hello") }) }) describe("a page where the server chose a different format per template", () => { beforeEach(() => { document.body.innerHTML = "" }) const OTHER = "app/views/posts/_row.html.erb" const withStatics = ` Hello ` const withoutStatics = ` ` test("reports what a template sent, so a caller knows whether to ask the server", () => { const index = new Slots() document.body.innerHTML = withStatics + withoutStatics index.scan(document.body) expect(index.renderModeFor(FILE)).toBe("client") expect(index.renderModeFor(OTHER)).toBe("server") expect(index.materialize(OTHER, "0:1")).toBeNull() }) test("reports a template the index has never seen as server-rendered", () => { const index = new Slots() expect(index.renderModeFor("app/views/nothing.html.erb")).toBe("server") }) test("answers per slot, so one template can be both", () => { const index = new Slots() document.body.innerHTML = ` public ` index.scan(document.body) expect(index.renderModeFor(FILE, 0)).toBe("client") expect(index.renderModeFor(FILE, 7)).toBe("server") expect(index.branchesFor(FILE, 7)).toEqual([]) }) test("takes statics as they arrive rather than as the whole set", () => { const index = new Slots() document.body.innerHTML = withStatics index.scan(document.body) expect(index.branchesFor(FILE, 0)).toEqual([1]) const later = document.createElement("div") later.innerHTML = ` Goodbye ` document.body.append(later) index.scan(later) expect(index.branchesFor(FILE, 0)).toEqual([1, 2]) expect(index.materialize(FILE, "0:2")?.textContent).toBe("Goodbye") }) test("drops what an older version of a template parked", () => { const index = new Slots() document.body.innerHTML = withStatics index.scan(document.body) const next = document.createElement("div") next.innerHTML = ` Goodbye ` document.body.append(next) index.scan(next) expect(index.parkedKeys(FILE)).toEqual(["0:2"]) expect(index.materialize(FILE, "0:1")).toBeNull() }) test("keeps statics when the rendering that carried them leaves the page", () => { const index = new Slots() document.body.innerHTML = withStatics index.scan(document.body) document.body.innerHTML = "" index.prune() expect(index.regionsFor(FILE)).toEqual([]) expect(index.renderModeFor(FILE)).toBe("client") index.clear() expect(index.renderModeFor(FILE)).toBe("server") }) }) describe("statics parked once for the page", () => { beforeEach(() => { document.body.innerHTML = "" }) const OTHER = "app/views/posts/_row.html.erb" const bundle = ` Hello Goodbye admin ` const rendering = `` test("belongs to the template it names rather than to where it sits", () => { const index = new Slots() document.body.innerHTML = rendering + nth(rendering, 1) + nth(rendering, 2) + bundle index.scan(document.body) expect(index.regionsFor(FILE)).toHaveLength(3) expect(index.parkedKeys(FILE)).toEqual(["0:1", "0:2", "7:1"]) expect(document.querySelector("template")).toBeNull() }) test("takes the keys from the payload, so nothing can disagree with it", () => { const index = new Slots() document.body.innerHTML = bundle index.scan(document.body) expect(index.branchesFor(FILE, 0)).toEqual([1, 2]) expect(index.branchesFor(FILE, 7)).toEqual([1]) expect(index.materialize(FILE, "0:2")?.textContent?.trim()).toBe("Goodbye") }) test("carries the branch marker into what it builds, so the slot knows its branch", () => { const index = new Slots() document.body.innerHTML = rendering + bundle index.scan(document.body) const built = index.materialize(FILE, "0:1", { 3: "Marco" })! index.rangeOf(index.slot(FILE, 0)!).insertNode(built) index.scan(document.body) expect(document.querySelector("b")?.textContent).toBe("Hello Marco") expect(index.slot(FILE, 0)?.branch).toBe(1) }) test("is indexed without any rendering of its template on the page", () => { const index = new Slots() document.body.innerHTML = bundle index.scan(document.body) expect(index.regionsFor(FILE)).toEqual([]) expect(index.renderModeFor(FILE)).toBe("client") expect(index.renderModeFor(OTHER)).toBe("server") }) test("is ignored when it names nothing that can be read as a region", () => { const index = new Slots() document.body.innerHTML = `x` index.scan(document.body) expect(index.parkedKeys("no-version-here")).toEqual([]) expect(document.querySelector("template"), "an unreadable one is left alone").not.toBeNull() }) }) describe("output the compiler actually emits", () => { beforeEach(() => { document.body.innerHTML = "" }) const VIEW = "app/views/test.html.erb" test("an untaken if branch, with the else that rendered not parked beside it", () => { const index = new Slots() document.body.innerHTML = `deep