Skip to content

Commit 23288ae

Browse files
committed
Add tests to cover error cases
1 parent 75665ca commit 23288ae

File tree

1 file changed

+134
-0
lines changed

1 file changed

+134
-0
lines changed

packages/core/test/ifthenelse_test.js

+134
Original file line numberDiff line numberDiff line change
@@ -287,4 +287,138 @@ describe("conditional items", () => {
287287
expect(node.querySelector("input[label=postcode]")).not.eql(null);
288288
});
289289
});
290+
291+
it("handles nested if then else", () => {
292+
const schemaWithNested = {
293+
$schema: "http://json-schema.org/draft-07/schema#",
294+
type: "object",
295+
properties: {
296+
country: {
297+
enum: ["USA"],
298+
},
299+
},
300+
required: ["country"],
301+
if: {
302+
properties: {
303+
country: {
304+
const: "USA",
305+
},
306+
},
307+
required: ["country"],
308+
},
309+
then: {
310+
properties: {
311+
state: {
312+
type: "string",
313+
anyOf: [
314+
{
315+
const: "California",
316+
},
317+
{
318+
const: "New York",
319+
},
320+
],
321+
},
322+
},
323+
required: ["state"],
324+
if: {
325+
properties: {
326+
state: {
327+
const: "New York",
328+
},
329+
},
330+
required: ["state"],
331+
},
332+
then: {
333+
properties: {
334+
city: {
335+
type: "string",
336+
anyOf: [
337+
{
338+
const: "New York City",
339+
},
340+
{
341+
const: "Buffalo",
342+
},
343+
{
344+
const: "Rochester",
345+
},
346+
],
347+
},
348+
},
349+
},
350+
else: {
351+
if: {
352+
properties: {
353+
state: {
354+
const: "California",
355+
},
356+
},
357+
required: ["state"],
358+
},
359+
then: {
360+
properties: {
361+
city: {
362+
type: "string",
363+
anyOf: [
364+
{
365+
const: "Los Angeles",
366+
},
367+
{
368+
const: "San Diego",
369+
},
370+
{
371+
const: "San Jose",
372+
},
373+
],
374+
},
375+
},
376+
},
377+
},
378+
},
379+
};
380+
381+
const formData = {
382+
country: "USA",
383+
state: "California",
384+
};
385+
const { node } = createFormComponent({
386+
schema: schemaWithNested,
387+
formData,
388+
});
389+
expect(node.querySelector("select[id=root_country]")).not.eql(null);
390+
expect(node.querySelector("select[id=root_state]")).not.eql(null);
391+
expect(node.querySelector("select[id=root_city]")).not.eql(null);
392+
});
393+
394+
it("handles additionalProperties with if then else", () => {
395+
/**
396+
* Ensures that fields defined in "then" or "else" (e.g. zipcode) are handled
397+
* with regular form fields, not as additional properties
398+
*/
399+
400+
const formData = {
401+
country: "United States of America",
402+
zipcode: "12345",
403+
otherKey: "otherValue",
404+
};
405+
const { node } = createFormComponent({
406+
schema: {
407+
...schema,
408+
additionalProperties: true,
409+
},
410+
formData,
411+
});
412+
413+
// The zipcode field exists, but not as an additional property
414+
expect(node.querySelector("input[label=zipcode]")).not.eql(null);
415+
expect(
416+
node.querySelector("div.form-additional input[label=zipcode]")
417+
).to.eql(null);
418+
419+
// The "otherKey" field exists as an additional property
420+
expect(
421+
node.querySelector("div.form-additional input[label=otherKey]")
422+
).not.eql(null);
423+
});
290424
});

0 commit comments

Comments
 (0)