|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { z } from "zod"; |
| 3 | +import { schemaIssues } from "./schemaIssues"; |
| 4 | + |
| 5 | +const leaf = z.object({ id: z.string(), x: z.number(), y: z.number(), rotation: z.number(), props: z.object({ content: z.string() }) }); |
| 6 | +const schema = z.object({ |
| 7 | + label: z.object({ widthMm: z.number(), heightMm: z.number(), dpmm: z.number() }), |
| 8 | + pages: z.array(z.object({ objects: z.array(z.union([leaf, z.object({ id: z.string(), children: z.array(leaf) })])) })), |
| 9 | +}); |
| 10 | + |
| 11 | +const label = { widthMm: 1, heightMm: 1, dpmm: 1 }; |
| 12 | +/** Four issues: id, x, y, props.content. */ |
| 13 | +const broken = { id: 1, x: "a", y: "b", rotation: 0, props: { content: 1 } }; |
| 14 | + |
| 15 | +const issuesOf = (value: unknown) => { |
| 16 | + const parsed = schema.safeParse(value); |
| 17 | + if (parsed.success) throw new Error("fixture parses"); |
| 18 | + return schemaIssues(parsed.error, "design"); |
| 19 | +}; |
| 20 | +const paths = (issues: string[]) => issues.map((line) => line.split(":")[0]); |
| 21 | + |
| 22 | +describe("schemaIssues", () => { |
| 23 | + it("lists one cause per broken object first and admits what it cut", () => { |
| 24 | + const issues = issuesOf({ label: { ...label, widthMm: "wide" }, pages: [{ objects: [broken, broken, broken, broken, broken, broken] }] }); |
| 25 | + expect(paths(issues)).toEqual([ |
| 26 | + "label.widthMm", |
| 27 | + "pages.0.objects.0.id", |
| 28 | + "pages.0.objects.1.id", |
| 29 | + "pages.0.objects.2.id", |
| 30 | + "pages.0.objects.3.id", |
| 31 | + "and 20 more issues", |
| 32 | + ]); |
| 33 | + }); |
| 34 | + |
| 35 | + it("names every broken object before any object's second field", () => { |
| 36 | + const issues = issuesOf({ label: { widthMm: "a", heightMm: "b", dpmm: "c" }, pages: [{ objects: [broken, broken] }] }); |
| 37 | + expect(paths(issues)).toEqual([ |
| 38 | + "label.widthMm", |
| 39 | + "pages.0.objects.0.id", |
| 40 | + "pages.0.objects.1.id", |
| 41 | + "label.heightMm", |
| 42 | + "label.dpmm", |
| 43 | + "and 6 more issues", |
| 44 | + ]); |
| 45 | + }); |
| 46 | + |
| 47 | + it("keys a group's children as elements of their own", () => { |
| 48 | + const issues = issuesOf({ label, pages: [{ objects: [{ id: "g", children: [broken, broken] }] }] }); |
| 49 | + expect(paths(issues)).toEqual([ |
| 50 | + "pages.0.objects.0.children.0.id", |
| 51 | + "pages.0.objects.0.children.1.id", |
| 52 | + "pages.0.objects.0.children.0.x", |
| 53 | + "pages.0.objects.0.children.0.y", |
| 54 | + "pages.0.objects.0.children.0.props.content", |
| 55 | + "and 3 more issues", |
| 56 | + ]); |
| 57 | + }); |
| 58 | + |
| 59 | + it("counts a single hidden issue in the singular", () => { |
| 60 | + const issues = issuesOf({ label: { ...label, widthMm: "a", heightMm: "b" }, pages: [{ objects: [broken] }] }); |
| 61 | + expect(issues[5]).toBe("and 1 more issue"); |
| 62 | + }); |
| 63 | + |
| 64 | + it("fills the free places with an object's other fields, without a marker", () => { |
| 65 | + const issues = issuesOf({ label, pages: [{ objects: [broken] }] }); |
| 66 | + expect(paths(issues)).toEqual(["pages.0.objects.0.id", "pages.0.objects.0.x", "pages.0.objects.0.y", "pages.0.objects.0.props.content"]); |
| 67 | + }); |
| 68 | + |
| 69 | + it("names a top-level failure by its root", () => { |
| 70 | + const parsed = z.object({ a: z.number() }).safeParse(5); |
| 71 | + if (parsed.success) throw new Error("fixture parses"); |
| 72 | + expect(schemaIssues(parsed.error, "design")).toEqual([expect.stringMatching(/^design: /)]); |
| 73 | + }); |
| 74 | +}); |
0 commit comments