/** * @file keepIfSame.test.ts * @description Covers the identity contract `keepIfSame` exists for: a polled * payload that is value-identical must keep the PREVIOUS reference (so React * bails out of the re-render), and any real change must yield the new one. * @author Nguyễn Ngọc Trí Vĩ */ import { describe, it, expect } from "vitest"; import { keepIfSame } from "../keepIfSame"; describe("keepIfSame", () => { it("keeps the previous reference when the fetched payload is value-identical", () => { const prev = [{ id: 1, stage: "implement" }]; const fetched = [{ id: 1, stage: "implement" }]; expect(keepIfSame(fetched)(prev)).toBe(prev); }); it("yields the new value when anything actually changed", () => { const prev = [{ id: 1, stage: "implement" }]; const fetched = [{ id: 1, stage: "review" }]; expect(keepIfSame(fetched)(prev)).toBe(fetched); }); it("yields the new value out of the null a poller starts from", () => { const fetched = { available: false }; expect(keepIfSame(fetched)(null)).toBe(fetched); }); it("keeps null when the payload is still null", () => { expect(keepIfSame(null)(null)).toBeNull(); }); });