|
1 | 1 | import { buildYup } from "schema-to-yup"; |
2 | | -import { FormHydrationValidation } from "@/interfaces/FormHydration"; |
| 2 | +import { Metadata } from "@/interfaces/Dataset"; |
| 3 | +import { |
| 4 | + FormHydration, |
| 5 | + FormHydrationValidation, |
| 6 | +} from "@/interfaces/FormHydration"; |
3 | 7 | import { |
4 | 8 | formGenerateLegendItems, |
5 | 9 | formGetFieldsCompletedCount, |
6 | 10 | generateValidationRules, |
| 11 | + mapFormFieldsForSubmission, |
7 | 12 | } from "./formHydration"; |
8 | 13 |
|
9 | 14 | const buildYupSchema = (validationFields: FormHydrationValidation[]) => |
@@ -118,9 +123,9 @@ describe("generateValidationRules", () => { |
118 | 123 | it("rejects a non-URL value in an optional url-format array field", () => { |
119 | 124 | const schema = buildYupSchema(toolsValidation); |
120 | 125 |
|
121 | | - expect(() => |
122 | | - schema.validateSync({ Tools: ["das"] }) |
123 | | - ).toThrow(/must be a valid URL/); |
| 126 | + expect(() => schema.validateSync({ Tools: ["das"] })).toThrow( |
| 127 | + /must be a valid URL/ |
| 128 | + ); |
124 | 129 | }); |
125 | 130 |
|
126 | 131 | it("accepts a valid URL in an optional url-format array field", () => { |
@@ -154,9 +159,66 @@ describe("generateValidationRules", () => { |
154 | 159 | const schema = buildYupSchema(requiredEnumValidation); |
155 | 160 |
|
156 | 161 | expect(() => schema.validateSync({ Category: [] })).toThrow(); |
157 | | - expect(() => |
158 | | - schema.validateSync({ Category: ["A"] }) |
159 | | - ).not.toThrow(); |
| 162 | + expect(() => schema.validateSync({ Category: ["A"] })).not.toThrow(); |
| 163 | + }); |
| 164 | +}); |
| 165 | + |
| 166 | +describe("mapFormFieldsForSubmission", () => { |
| 167 | + // A minimal schema for a single field that lives outside the provenance |
| 168 | + // section, so a "partial draft" produces formattedFormData with no |
| 169 | + // provenance branch at all - the exact condition that used to throw. |
| 170 | + const schema = [ |
| 171 | + { |
| 172 | + title: "Title", |
| 173 | + is_array_form: false, |
| 174 | + location: "summary.title", |
| 175 | + field: { |
| 176 | + component: "TextField", |
| 177 | + name: "Title", |
| 178 | + required: true, |
| 179 | + hidden: false, |
| 180 | + }, |
| 181 | + }, |
| 182 | + ] as unknown as FormHydration[]; |
| 183 | + |
| 184 | + const submit = (formData: Record<string, unknown>) => |
| 185 | + mapFormFieldsForSubmission(formData as unknown as Metadata, schema) as { |
| 186 | + provenance?: { origin?: { datasetType?: unknown } }; |
| 187 | + }; |
| 188 | + |
| 189 | + it("does not throw when the provenance section is empty (partial draft)", () => { |
| 190 | + expect(() => submit({ Title: "A partial draft" })).not.toThrow(); |
| 191 | + }); |
| 192 | + |
| 193 | + it("defaults datasetType to an empty array when Dataset Type Array is absent", () => { |
| 194 | + const result = submit({ Title: "A partial draft" }); |
| 195 | + |
| 196 | + expect(result.provenance?.origin?.datasetType).toEqual([]); |
| 197 | + }); |
| 198 | + |
| 199 | + it("maps Dataset Type Array into provenance.origin.datasetType when provenance is otherwise empty", () => { |
| 200 | + const result = submit({ |
| 201 | + "Dataset Type Array": [ |
| 202 | + { |
| 203 | + "Dataset type": "Health and disease", |
| 204 | + "Dataset subtypes": ["Cancer"], |
| 205 | + }, |
| 206 | + ], |
| 207 | + }); |
| 208 | + |
| 209 | + expect(result.provenance?.origin?.datasetType).toEqual([ |
| 210 | + { name: "Health and disease", subTypes: ["Cancer"] }, |
| 211 | + ]); |
| 212 | + }); |
| 213 | + |
| 214 | + it("defaults subTypes to an empty array when Dataset subtypes is missing", () => { |
| 215 | + const result = submit({ |
| 216 | + "Dataset Type Array": [{ "Dataset type": "Health and disease" }], |
| 217 | + }); |
| 218 | + |
| 219 | + expect(result.provenance?.origin?.datasetType).toEqual([ |
| 220 | + { name: "Health and disease", subTypes: [] }, |
| 221 | + ]); |
160 | 222 | }); |
161 | 223 | }); |
162 | 224 |
|
|
0 commit comments