Skip to content

Commit

Permalink
Fixed schema type not being deduced correctly in compute defaults (#1334
Browse files Browse the repository at this point in the history
) (#1338)
  • Loading branch information
t-moe authored and epicfaace committed Jul 9, 2019
1 parent 7827ab0 commit 750f5cb
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
30 changes: 30 additions & 0 deletions test/anyOf_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
30 changes: 30 additions & 0 deletions test/oneOf_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
17 changes: 17 additions & 0 deletions test/utils_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 750f5cb

Please sign in to comment.