41 lines
1.3 KiB
TypeScript
41 lines
1.3 KiB
TypeScript
import { describe, expect, it, vi } from "vitest"
|
|
import * as Refreshable from "../src/Refreshable.js"
|
|
|
|
|
|
describe("Refreshable", () => {
|
|
it("attaches a cell and notifies subscribers after a compatible update", async () => {
|
|
const first = {}
|
|
const cell = Refreshable.makeCell(first, "hooks", false)
|
|
const listener = vi.fn()
|
|
|
|
expect(Refreshable.isRefreshable(first)).toBe(false)
|
|
const attached = Refreshable.attach(first, cell)
|
|
expect(attached).toBe(first)
|
|
expect(Refreshable.isRefreshable(first)).toBe(true)
|
|
expect(attached[Refreshable.RefreshableTypeId]).toBe(cell)
|
|
|
|
cell.subscribe(listener)
|
|
const second = {}
|
|
cell.update(second, "hooks", false)
|
|
|
|
expect(cell.current).toBe(second)
|
|
expect(cell.snapshot).toEqual({
|
|
revision: 1,
|
|
resetRevision: 0,
|
|
})
|
|
|
|
await Promise.resolve()
|
|
expect(listener).toHaveBeenCalledTimes(1)
|
|
})
|
|
|
|
it("requests a remount when a signature changes or reset is forced", () => {
|
|
const cell = Refreshable.makeCell({}, "one", false)
|
|
|
|
cell.update({}, "two", false)
|
|
expect(cell.snapshot.resetRevision).toBe(1)
|
|
|
|
cell.update({}, "two", true)
|
|
expect(cell.snapshot.resetRevision).toBe(2)
|
|
})
|
|
})
|