import { describe, test, expect, beforeEach } from "vitest"
import { Slots } from "../src/slots/slots"
import type { Payload } from "../src/types"
const FILE = "app/views/posts/index.html.erb"
const CARD = "app/views/posts/_card.html.erb"
const COND_FALSE = `
`
const COND_TRUE = `yes
`
const PARKED = `no`
const ITEMS = ``
const NAMED_ITEMS = ``
const EMPTY_ITEMS = ``
const PARKED_ITEM =
`` +
``
const NESTED =
``
function mounted(html: string): Slots {
document.body.innerHTML = html
const index = new Slots()
index.scan(document.body)
return index
}
function payload(template: string, slots: Payload["slots"], version = "aaaaaaaa", occurrence = 0): Payload {
return { template, version, occurrence, slots }
}
describe("a claimed conditional", () => {
test("still hands nested values to the branch the state chose", () => {
const slots = mounted(
``
)
const slot = slots.slot(FILE, 0)!
slots.claim(slot)
const report = slots.apply(payload(FILE, { 0: { branch: 0, slots: { 1: "fresh" } } }))
expect(report.applied).toBe(1)
expect(document.body.textContent).toContain("fresh")
})
test("remembers a disagreeing payload's values for the branch a later switch shows", () => {
const slots = mounted(
`` +
``
)
const slot = slots.slot(FILE, 0)!
slots.claim(slot)
slots.apply(payload(FILE, { 0: { branch: 1, slots: { 2: "Today at 4:24 PM" } } }))
expect(document.body.textContent).toContain("sending")
expect(slots.switchBranch(slot, 1)).toBe(true)
expect(document.body.textContent).toContain("Today at 4:24 PM")
})
test("keeps the state's branch when the payload disagrees, and applies nothing inside", () => {
const slots = mounted(
``
)
const slot = slots.slot(FILE, 0)!
slots.claim(slot)
const report = slots.apply(payload(FILE, { 0: { branch: 1, slots: { 2: "theirs" } } }))
expect(report.applied).toBe(0)
expect(slot.branch).toBe(0)
expect(document.body.textContent).toContain("mine")
expect(document.body.textContent).not.toContain("theirs")
})
})
describe("applying values to a conditional", () => {
beforeEach(() => {
document.body.innerHTML = ""
})
test("fills the branch that is already on the page", () => {
const index = mounted(COND_TRUE)
const report = index.apply(payload(FILE, { 0: { branch: 0, slots: { 1: "still yes" } } }))
expect(report.applied).toBe(1)
expect(index.rangeOf(index.slot(FILE, 1)!).toString()).toBe("still yes")
})
test("empties the position when the payload says no branch ran", () => {
const index = mounted(COND_TRUE)
const report = index.apply(payload(FILE, { 0: { branch: null } }))
expect(report.applied).toBe(1)
expect(index.slot(FILE, 0)?.branch).toBeNull()
expect(index.rangeOf(index.slot(FILE, 0)!).toString()).toBe("")
})
test("builds a branch the page never had when its markup was parked", () => {
const index = mounted(COND_FALSE + PARKED)
const report = index.apply(payload(FILE, { 0: { branch: 1, slots: { 2: "built" } } }))
expect(report.applied).toBe(1)
expect(report.deferred).toEqual([])
expect(index.slot(FILE, 0)?.branch).toBe(1)
expect(index.rangeOf(index.slot(FILE, 2)!).toString()).toBe("built")
})
test("can put back the branch it replaced, without the server parking it", () => {
const index = mounted(COND_TRUE + PARKED)
index.apply(payload(FILE, { 0: { branch: 1, slots: { 2: "built" } } }))
expect(index.slot(FILE, 0)?.branch).toBe(1)
const report = index.apply(payload(FILE, { 0: { branch: 0, slots: { 1: "back" } } }))
expect(report.deferred).toEqual([])
expect(index.slot(FILE, 0)?.branch).toBe(0)
expect(index.rangeOf(index.slot(FILE, 1)!).toString()).toBe("back")
})
test("defers a branch it has no markup for, rather than guessing", () => {
const index = mounted(COND_FALSE)
const report = index.apply(payload(FILE, { 0: { branch: 1, slots: { 2: "built" } } }))
expect(report.applied).toBe(0)
expect(report.deferred).toEqual([{ file: FILE, occurrence: 0, index: 0, reason: "branch" }])
expect(index.rangeOf(index.slot(FILE, 0)!).toString()).toBe("")
})
})
describe("applying values to a collection", () => {
beforeEach(() => {
document.body.innerHTML = ""
})
test("writes into the items the page already has", () => {
const index = mounted(ITEMS)
const report = index.apply(
payload(FILE, { 0: { items: { 1: { 1: "ONE" }, 2: { 1: "TWO" } } } }, "bbbbbbbb"),
)
expect(report.applied).toBe(2)
expect(report.deferred).toEqual([])
expect(document.querySelectorAll("li")[0].textContent).toBe("ONE")
expect(document.querySelectorAll("li")[1].textContent).toBe("TWO")
})
const keys = () => [...document.querySelectorAll("li")].map((li) => li.textContent)
test("drops an item the payload no longer has", () => {
const index = mounted(ITEMS)
const report = index.apply(payload(FILE, { 0: { items: { 2: { 1: "two" } } } }, "bbbbbbbb"))
expect(report.deferred).toEqual([])
expect(keys()).toEqual(["two"])
})
test("builds an item it has never seen from one it has", () => {
const index = mounted(ITEMS)
const report = index.apply(
payload(FILE, { 0: { items: { 1: { 1: "one" }, 2: { 1: "two" }, 3: { 1: "three" } } } }, "bbbbbbbb"),
)
expect(report.deferred).toEqual([])
expect(keys()).toEqual(["one", "two", "three"])
expect(index.slotInItem(FILE, 0, "3", 1)).not.toBeNull()
})
test("puts the items in the order the payload asked for", () => {
const index = mounted(NAMED_ITEMS)
index.apply(payload(FILE, { 0: { items: { grace: { 1: "Grace" }, ada: { 1: "Ada" } } } }, "bbbbbbbb"))
expect(keys()).toEqual(["Grace", "Ada"])
})
test("adds, removes and reorders in one go", () => {
const index = mounted(NAMED_ITEMS)
const report = index.apply(
payload(FILE, { 0: { items: { yuki: { 1: "Yukihiro" }, ada: { 1: "Ada" } } } }, "bbbbbbbb"),
)
expect(report.deferred).toEqual([])
expect(keys()).toEqual(["Yukihiro", "Ada"])
})
test("orders items keyed by a number the way the payload's order names", () => {
const index = mounted(ITEMS)
index.apply(payload(FILE, { 0: { items: { 2: { 1: "two" }, 1: { 1: "one" } }, order: ["2", "1"] } }, "bbbbbbbb"))
expect(keys()).toEqual(["two", "one"])
})
test("builds into an empty collection from the item the server parked", () => {
const index = mounted(EMPTY_ITEMS + PARKED_ITEM)
const report = index.apply(payload(FILE, { 0: { items: { 1: { 1: "one" } } } }, "bbbbbbbb"))
expect(report.deferred).toEqual([])
expect(keys()).toEqual(["one"])
expect(index.slotInItem(FILE, 0, "1", 1)).not.toBeNull()
})
test("builds again after every item has been deleted", () => {
const index = mounted(ITEMS)
const empty = payload(FILE, { 0: { items: {} } }, "bbbbbbbb")
expect(index.apply(empty).deferred).toEqual([])
expect(keys()).toEqual([])
const report = index.apply(payload(FILE, { 0: { items: { 9: { 1: "again" } } } }, "bbbbbbbb"))
expect(report.deferred).toEqual([])
expect(keys()).toEqual(["again"])
})
test("asks for an item when the collection is empty and nothing was parked", () => {
const index = mounted(EMPTY_ITEMS)
const report = index.apply(payload(FILE, { 0: { items: { 1: { 1: "one" } } } }, "bbbbbbbb"))
expect(report.deferred).toEqual([
{ file: FILE, occurrence: 0, index: 0, reason: "items", keys: ["1"] },
])
})
})
const BLOCK =
`` +
`after`
const BARE_BLOCK = ``
describe("applying values to a block", () => {
test("writes the interior the page can address and leaves the helper's markup alone", () => {
const index = mounted(BLOCK)
const report = index.apply(payload(FILE, { 0: "", 1: "Other", 2: "later" }, "eeeeeeee"))
expect(report.deferred).toEqual([])
expect(document.querySelector("form")!.innerHTML).toBe("Other")
expect(index.slot(FILE, 1)).not.toBeNull()
})
test("says the helper's own markup went unwritten when the interior does not account for it", () => {
const index = mounted(BLOCK)
const report = index.apply(payload(FILE, { 0: ``, 1: "Other", 2: "later" }, "eeeeeeee"))
expect(report.deferred).toEqual([{ file: FILE, occurrence: 0, index: 0, reason: "block" }])
expect(document.querySelector("form")!.innerHTML).toBe("Other")
})
test("applies a partial inside a block and leaves its markup in place", () => {
const index = mounted(
``
)
const paragraph = document.querySelector("p")
const report = index.apply(payload(FILE, { 0: "", 1: payload(CARD, { 0: "new" }, "dddddddd") }, "cccccccc"))
expect(report.applied).toBe(1)
expect(report.deferred).toEqual([{ file: FILE, occurrence: 0, index: 0, reason: "block" }])
expect(document.querySelector("p")).toBe(paragraph)
expect(paragraph!.textContent).toBe("new")
})
test("falls back to the whole block when its interior is not addressable", () => {
const index = mounted(BARE_BLOCK)
const report = index.apply(payload(FILE, { 0: "" }, "eeeeeeee"))
expect(report.applied).toBe(1)
expect(document.querySelector("form")!.innerHTML).toBe("Other")
})
})
describe("applying values that came from more than one template", () => {
beforeEach(() => {
document.body.innerHTML = ""
})
test("hands a partial's values to the partial's own region", () => {
const index = mounted(NESTED)
const report = index.apply(
payload(FILE, { 0: payload(CARD, { 0: "replaced" }, "dddddddd") }, "cccccccc"),
)
expect(report.applied).toBe(1)
expect(index.rangeOf(index.slot(CARD, 0)!).toString()).toBe("replaced")
})
test("defers a partial whose version the page no longer carries", () => {
const index = mounted(NESTED)
const report = index.apply(
payload(FILE, { 0: payload(CARD, { 0: "replaced" }, "eeeeeeee") }, "cccccccc"),
)
expect(report.applied).toBe(0)
expect(report.deferred).toEqual([{ file: CARD, occurrence: 0, index: null, reason: "stale-version" }])
expect(index.rangeOf(index.slot(CARD, 0)!).toString()).toBe("inner")
})
})
describe("an attribute a template only partly wrote", () => {
const PARTIAL = ``
beforeEach(() => {
document.body.innerHTML = ""
})
test("is refused rather than written over", () => {
const index = mounted(PARTIAL)
const report = index.apply(payload(FILE, { 0: "" }))
expect(report.applied).toBe(0)
expect(report.deferred).toEqual([{ file: FILE, occurrence: 0, index: 0, reason: "partial-attribute" }])
expect(document.querySelector("div")?.getAttribute("class")).toBe("card active")
})
})
describe("applying values the page has no place for", () => {
beforeEach(() => {
document.body.innerHTML = ""
})
test("names the slot it could not find and keeps going", () => {
const index = mounted(COND_TRUE)
const report = index.apply(payload(FILE, { 1: "written", 9: "nowhere" }))
expect(report.applied).toBe(1)
expect(report.deferred).toEqual([{ file: FILE, occurrence: 0, index: 9, reason: "no-slot" }])
})
})
describe("applying a value that carries entities", () => {
const ATTRS = `old`
const INTERPOLATED =
`x` +
`${JSON.stringify({ [`${FILE}:ffffffff`]: { file: FILE, identifier: FILE, version: "ffffffff", names: {}, parts: { 0: ["row-", ""] }, states: null } })}` +
``
test("writes an attribute the way the server's markup reads, not the bytes it sent", () => {
const index = mounted(ATTRS)
index.apply(payload(FILE, { 0: "Tom & <b>Jerry</b>", 1: "Tom & Jerry" }, "eeeeeeee"))
const element = document.querySelector("li")!
expect(element.getAttribute("title")).toBe("Tom & Jerry")
expect(element.textContent).toBe("Tom & Jerry")
})
test("joins an interpolated attribute's parts with what the server would have written", () => {
const index = mounted(INTERPOLATED)
index.apply(payload(FILE, { 0: ["a & b"] }, "ffffffff"))
expect(document.querySelector("li")!.getAttribute("class")).toBe("row-a & b")
})
test("leaves an entity the value only looks like alone", () => {
const index = mounted(ATTRS)
index.apply(payload(FILE, { 0: "100% & rising  " }, "eeeeeeee"))
expect(document.querySelector("li")!.getAttribute("title")).toBe("100% & rising  ")
})
})