import { describe, test, expect, beforeEach } from "vitest" import { Slots } from "../src/slots/slots" const FILE = "app/views/posts/index.html.erb" const PARTIAL = "app/views/posts/_row.html.erb" const region = (file: string, body: string, occurrence = 0) => `${body}` const page = (name: string) => region(FILE, `
${name}
`) const settle = () => new Promise((resolve) => setTimeout(resolve, 0)) describe("page navigation and Turbo", () => { let index: Slots beforeEach(() => { document.body.innerHTML = "" index = new Slots() }) test("indexes the new page after the body is replaced", async () => { index.observe() document.body.innerHTML = page("before") await settle() expect(index.rangeOf(index.slot(FILE, 0)!).toString()).toBe("before") const next = document.createElement("body") next.innerHTML = page("after") document.body.replaceWith(next) await settle() expect(index.regionsFor(FILE)).toHaveLength(1) expect(index.rangeOf(index.slot(FILE, 0)!).toString()).toBe("after") index.disconnect() }) test("drops the previous page rather than accumulating regions across visits", async () => { index.observe() for (const name of ["one", "two", "three"]) { const body = document.createElement("body") body.innerHTML = page(name) document.body.replaceWith(body) await settle() } expect(index.regionsFor(FILE)).toHaveLength(1) expect(index.rangeOf(index.slot(FILE, 0)!).toString()).toBe("three") index.disconnect() }) test("indexes a partial that arrives inside a turbo-frame", async () => { document.body.innerHTML = `${page("page")}