|
| 1 | +import { describe, it, expect, vi } from "vitest"; |
| 2 | +import { z } from "zod"; |
| 3 | +import { buildFieldConfigFromJsonSchema } from "@workspace/ui/components/auto-form/helpers"; |
| 4 | + |
| 5 | +/** |
| 6 | + * Verifies that buildFieldConfigFromJsonSchema recurses into nested objects AND |
| 7 | + * array items.properties so that per-property metadata (label, placeholder, |
| 8 | + * fieldType, etc.) reaches the AutoForm renderer for fields nested inside |
| 9 | + * arrays-of-objects. |
| 10 | + * |
| 11 | + * The CMS auto-form pipeline relies on this for blend-style schemas like: |
| 12 | + * components: z.array(z.object({ |
| 13 | + * name: z.string(), |
| 14 | + * compoundId: z.object({ id: z.string() }).meta({ fieldType: "relation" }), |
| 15 | + * })) |
| 16 | + * |
| 17 | + * Without this recursion, AutoFormArray would render the inner items with no |
| 18 | + * field config — placeholders and custom field components on nested array |
| 19 | + * properties would silently disappear. |
| 20 | + */ |
| 21 | + |
| 22 | +type ConfigMap = Record<string, Record<string, unknown> | undefined>; |
| 23 | + |
| 24 | +function getConfig(map: ConfigMap, key: string): Record<string, unknown> { |
| 25 | + const value = map[key]; |
| 26 | + if (!value) { |
| 27 | + throw new Error(`Expected config entry for "${key}" but got undefined`); |
| 28 | + } |
| 29 | + return value; |
| 30 | +} |
| 31 | + |
| 32 | +describe("buildFieldConfigFromJsonSchema — nested + array recursion", () => { |
| 33 | + it("propagates per-item placeholders into the array's field config", () => { |
| 34 | + const schema = z.object({ |
| 35 | + components: z |
| 36 | + .array( |
| 37 | + z.object({ |
| 38 | + name: z.string().meta({ placeholder: "e.g. GHK-Cu" }), |
| 39 | + doseLow: z.coerce.number().meta({ placeholder: "1" }), |
| 40 | + }), |
| 41 | + ) |
| 42 | + .default([]) |
| 43 | + .meta({ description: "Blend components" }), |
| 44 | + }); |
| 45 | + |
| 46 | + const jsonSchema = z.toJSONSchema(schema) as Record<string, unknown>; |
| 47 | + const config = buildFieldConfigFromJsonSchema(jsonSchema) as ConfigMap; |
| 48 | + const components = getConfig(config, "components") as ConfigMap & { |
| 49 | + description?: string; |
| 50 | + }; |
| 51 | + |
| 52 | + expect(components.description).toBe("Blend components"); |
| 53 | + |
| 54 | + // Per-item configs must live as keys on the array's FieldConfigObject so |
| 55 | + // that AutoFormObject (rendered per array item) can look them up by name. |
| 56 | + const nameConfig = getConfig(components, "name") as { |
| 57 | + inputProps?: { placeholder?: string }; |
| 58 | + }; |
| 59 | + expect(nameConfig.inputProps?.placeholder).toBe("e.g. GHK-Cu"); |
| 60 | + |
| 61 | + const doseLowConfig = getConfig(components, "doseLow") as { |
| 62 | + inputProps?: { placeholder?: string }; |
| 63 | + }; |
| 64 | + expect(doseLowConfig.inputProps?.placeholder).toBe("1"); |
| 65 | + }); |
| 66 | + |
| 67 | + it("propagates fieldType (textarea) into array items", () => { |
| 68 | + const schema = z.object({ |
| 69 | + components: z |
| 70 | + .array( |
| 71 | + z.object({ |
| 72 | + notes: z.string().meta({ fieldType: "textarea" }), |
| 73 | + }), |
| 74 | + ) |
| 75 | + .default([]), |
| 76 | + }); |
| 77 | + |
| 78 | + const jsonSchema = z.toJSONSchema(schema) as Record<string, unknown>; |
| 79 | + const config = buildFieldConfigFromJsonSchema(jsonSchema) as ConfigMap; |
| 80 | + const components = getConfig(config, "components") as ConfigMap; |
| 81 | + const notesConfig = getConfig(components, "notes") as { |
| 82 | + fieldType?: string; |
| 83 | + }; |
| 84 | + |
| 85 | + expect(notesConfig.fieldType).toBe("textarea"); |
| 86 | + }); |
| 87 | + |
| 88 | + it("invokes a custom fieldComponent for nested array fields", () => { |
| 89 | + const schema = z.object({ |
| 90 | + components: z |
| 91 | + .array( |
| 92 | + z.object({ |
| 93 | + compoundId: z |
| 94 | + .object({ id: z.string() }) |
| 95 | + .meta({ fieldType: "relation" }), |
| 96 | + }), |
| 97 | + ) |
| 98 | + .default([]), |
| 99 | + }); |
| 100 | + |
| 101 | + const jsonSchema = z.toJSONSchema(schema) as Record<string, unknown>; |
| 102 | + |
| 103 | + const RelationStub = () => null; |
| 104 | + const config = buildFieldConfigFromJsonSchema(jsonSchema, { |
| 105 | + relation: RelationStub, |
| 106 | + }) as ConfigMap; |
| 107 | + |
| 108 | + const components = getConfig(config, "components") as ConfigMap; |
| 109 | + const compoundIdConfig = getConfig(components, "compoundId") as { |
| 110 | + fieldType?: unknown; |
| 111 | + }; |
| 112 | + |
| 113 | + // fieldComponents["relation"] should be wired up for the nested array |
| 114 | + // element field, not just top-level fields. |
| 115 | + expect(typeof compoundIdConfig.fieldType).toBe("function"); |
| 116 | + }); |
| 117 | + |
| 118 | + it("still propagates per-property configs into nested objects (regression)", () => { |
| 119 | + const schema = z.object({ |
| 120 | + seo: z.object({ |
| 121 | + title: z.string().meta({ placeholder: "Page title" }), |
| 122 | + }), |
| 123 | + }); |
| 124 | + |
| 125 | + const jsonSchema = z.toJSONSchema(schema) as Record<string, unknown>; |
| 126 | + const config = buildFieldConfigFromJsonSchema(jsonSchema) as ConfigMap; |
| 127 | + const seo = getConfig(config, "seo") as ConfigMap; |
| 128 | + const titleConfig = getConfig(seo, "title") as { |
| 129 | + inputProps?: { placeholder?: string }; |
| 130 | + }; |
| 131 | + |
| 132 | + expect(titleConfig.inputProps?.placeholder).toBe("Page title"); |
| 133 | + }); |
| 134 | + |
| 135 | + it("warns and skips item properties whose names collide with reserved FieldConfigItem keys", () => { |
| 136 | + const warn = vi.spyOn(console, "warn").mockImplementation(() => {}); |
| 137 | + |
| 138 | + const schema = z.object({ |
| 139 | + items: z |
| 140 | + .array( |
| 141 | + z.object({ |
| 142 | + // "label" is a reserved key on FieldConfigItem and would |
| 143 | + // silently overwrite the array's own label if not guarded. |
| 144 | + label: z.string().meta({ placeholder: "Item label" }), |
| 145 | + value: z.string().meta({ placeholder: "Item value" }), |
| 146 | + }), |
| 147 | + ) |
| 148 | + .default([]) |
| 149 | + .meta({ description: "An array" }), |
| 150 | + }); |
| 151 | + |
| 152 | + const jsonSchema = z.toJSONSchema(schema) as Record<string, unknown>; |
| 153 | + const config = buildFieldConfigFromJsonSchema(jsonSchema) as ConfigMap; |
| 154 | + const items = getConfig(config, "items") as ConfigMap & { |
| 155 | + description?: string; |
| 156 | + }; |
| 157 | + |
| 158 | + // The array's own description should remain intact (not overwritten by |
| 159 | + // a nested item field also named "description"). |
| 160 | + expect(items.description).toBe("An array"); |
| 161 | + |
| 162 | + // Non-reserved item properties propagate normally. |
| 163 | + const valueConfig = getConfig(items, "value") as { |
| 164 | + inputProps?: { placeholder?: string }; |
| 165 | + }; |
| 166 | + expect(valueConfig.inputProps?.placeholder).toBe("Item value"); |
| 167 | + |
| 168 | + // The "label" item property collides with a reserved FieldConfigItem |
| 169 | + // key and must be skipped + warned about. |
| 170 | + expect(warn).toHaveBeenCalledWith( |
| 171 | + expect.stringContaining( |
| 172 | + 'Array field "items" has an item property named "label"', |
| 173 | + ), |
| 174 | + ); |
| 175 | + warn.mockRestore(); |
| 176 | + }); |
| 177 | +}); |
0 commit comments