From 78e16db1c95d1ad4ad0e940d868247715ec63892 Mon Sep 17 00:00:00 2001 From: t-moe Date: Thu, 27 Jun 2019 09:57:27 +0200 Subject: [PATCH] Fixed schema type not being deduced correctly in compute defaults (#1334) --- src/utils.js | 2 +- test/anyOf_test.js | 30 ++++++++++++++++++++++++++++++ test/oneOf_test.js | 30 ++++++++++++++++++++++++++++++ test/utils_test.js | 17 +++++++++++++++++ 4 files changed, 78 insertions(+), 1 deletion(-) diff --git a/src/utils.js b/src/utils.js index 0b943949bf..2099c0412e 100644 --- a/src/utils.js +++ b/src/utils.js @@ -184,7 +184,7 @@ function computeDefaults( defaults = schema.default; } - switch (schema.type) { + switch (getSchemaType(schema)) { // We need to recur for object schema inner default values. case "object": return Object.keys(schema.properties || {}).reduce((acc, key) => { diff --git a/test/anyOf_test.js b/test/anyOf_test.js index b5aba8d839..aea2ca9a5b 100644 --- a/test/anyOf_test.js +++ b/test/anyOf_test.js @@ -85,6 +85,36 @@ describe("anyOf", () => { expect(comp.state.formData).eql({ foo: "defaultbar" }); }); + it("should assign a default value and set defaults on option change with 'type': 'object' missing", () => { + const { comp, node } = createFormComponent({ + schema: { + type: "object", + anyOf: [ + { + properties: { + foo: { type: "string", default: "defaultfoo" }, + }, + }, + { + properties: { + foo: { type: "string", default: "defaultbar" }, + }, + }, + ], + }, + }); + + expect(comp.state.formData).eql({ foo: "defaultfoo" }); + + const $select = node.querySelector("select"); + + Simulate.change($select, { + target: { value: $select.options[1].value }, + }); + + expect(comp.state.formData).eql({ foo: "defaultbar" }); + }); + it("should render a custom widget", () => { const schema = { type: "object", diff --git a/test/oneOf_test.js b/test/oneOf_test.js index 386ae59b10..14cf77049d 100644 --- a/test/oneOf_test.js +++ b/test/oneOf_test.js @@ -85,6 +85,36 @@ describe("oneOf", () => { expect(comp.state.formData).eql({ foo: "defaultbar" }); }); + it("should assign a default value and set defaults on option change with 'type': 'object' missing", () => { + const { comp, node } = createFormComponent({ + schema: { + type: "object", + oneOf: [ + { + properties: { + foo: { type: "string", default: "defaultfoo" }, + }, + }, + { + properties: { + foo: { type: "string", default: "defaultbar" }, + }, + }, + ], + }, + }); + + expect(comp.state.formData).eql({ foo: "defaultfoo" }); + + const $select = node.querySelector("select"); + + Simulate.change($select, { + target: { value: $select.options[1].value }, + }); + + expect(comp.state.formData).eql({ foo: "defaultbar" }); + }); + it("should render a custom widget", () => { const schema = { type: "object", diff --git a/test/utils_test.js b/test/utils_test.js index 0da762e490..56ed93ad03 100644 --- a/test/utils_test.js +++ b/test/utils_test.js @@ -359,6 +359,23 @@ describe("utils", () => { }); }); + it("should populate defaults for oneOf when 'type': 'object' is missing", () => { + const schema = { + type: "object", + oneOf: [ + { + properties: { name: { type: "string", default: "a" } }, + }, + { + properties: { id: { type: "number", default: 13 } }, + }, + ], + }; + expect(getDefaultFormState(schema, {})).eql({ + name: "a", + }); + }); + it("should populate nested default values for oneOf", () => { const schema = { type: "object",