From d3de7cc172982f55a4ae152da552f2cbe5bcaff4 Mon Sep 17 00:00:00 2001 From: Maofeng Date: Mon, 28 Sep 2026 17:24:08 +0800 Subject: [PATCH] test(record): cover providers fields and iteration --- packages/record/src/record.test.tsx | 190 ++++++++++++++++++++++++++++ 1 file changed, 190 insertions(+) create mode 100644 packages/record/src/record.test.tsx diff --git a/packages/record/src/record.test.tsx b/packages/record/src/record.test.tsx new file mode 100644 index 0000000..ee2cf6e --- /dev/null +++ b/packages/record/src/record.test.tsx @@ -0,0 +1,190 @@ +// @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) + }) +})