// @vitest-environment jsdom import type { ReactNode } from "react" import { cleanup, render, screen } from "@testing-library/react" import { afterEach, describe, expect, it } from "vitest" import { OptionalRecordProvider, RecordProvider, RecordsIterator, useFieldValue, useRecord, } from "@workspace/record" interface Book { author: { name: string } id: number title: string } afterEach(cleanup) function RecordProbe({ record }: { record?: Book }) { const current = useRecord({ record }) return {current?.title ?? "No record"} } function FieldProbe() { const author = useFieldValue({ defaultValue: "Anonymous", source: "author.name", }) return {author} } function PrimitiveProbe() { const record = useRecord() return {String(record)} } function Wrapper({ children }: { children: ReactNode }) { return ( value={{ author: { name: "Ursula Le Guin" }, id: 1, title: "Earthsea" }} > {children} ) } describe("record context", () => { it("reads context records and prefers an explicit override", () => { render( ) expect(screen.getByText("Earthsea")).toBeTruthy() expect(screen.getByText("Parable of the Sower")).toBeTruthy() }) it("keeps the parent context when the optional record is undefined", () => { render( ) expect(screen.getByText("Earthsea")).toBeTruthy() }) it("accepts every non-nullish value as a record", () => { render( <> /> /> /> ) expect(screen.getByText("0")).toBeTruthy() expect(screen.getByText("draft")).toBeTruthy() expect(screen.getByText("false")).toBeTruthy() }) it("reads nested field values and applies a default without a record", () => { const { rerender } = render( ) expect(screen.getByText("Ursula Le Guin")).toBeTruthy() rerender() expect(screen.getByText("Anonymous")).toBeTruthy() }) }) describe("RecordsIterator", () => { const books: Book[] = [ { author: { name: "N. K. Jemisin" }, id: 1, title: "The Fifth Season" }, { author: { name: "Ted Chiang" }, id: 2, title: "Exhalation" }, ] it("provides each record to shared children", () => { render( ) expect(screen.getByText("The Fifth Season")).toBeTruthy() expect(screen.getByText("Exhalation")).toBeTruthy() }) it("supports iterator functions and custom record keys", () => { render( book)} iterator={(book, index) => ( {index + 1}. {book.title} )} recordKey={(book) => book.title} /> ) expect(screen.getByText("1. The Fifth Season")).toBeTruthy() expect(screen.getByText("2. Exhalation")).toBeTruthy() }) it("iterates primitive records", () => { render( /> ) expect(screen.getByText("draft")).toBeTruthy() expect(screen.getByText("published")).toBeTruthy() }) it("renders separators between records", () => { render(
{book.title}} separator={(index) => |{index}|} />
) expect(screen.getByTestId("book-list").textContent).toBe( "The Fifth Season|1|Exhalation" ) expect(screen.getAllByText("|1|")).toHaveLength(1) }) it("renders nothing for missing data or content", () => { const { container, rerender } = render( unused} /> ) expect(container.childElementCount).toBe(0) rerender() expect(container.childElementCount).toBe(0) }) })