Skip to content

Commit

Permalink
fix: populate defaults for nested dependencies when formData is undef…
Browse files Browse the repository at this point in the history
…ined (#1313)

* fix: populate defaults for nested dependencies when formData is undefined

* test: add test for null

* fix: handle null case as per @jrose-carecloud's suggestion
  • Loading branch information
epicfaace authored and glasserc committed Jun 8, 2019
1 parent 20511b9 commit 8c81cc7
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,13 @@ export function hasWidget(schema, widget, registeredWidgets = {}) {
}
}

function computeDefaults(schema, parentDefaults, definitions, formData) {
function computeDefaults(
schema,
parentDefaults,
definitions,
rawFormData = {}
) {
const formData = isObject(rawFormData) ? rawFormData : {};
// Compute the defaults recursively: give highest priority to deepest nodes.
let defaults = parentDefaults;
if (isObject(defaults) && isObject(schema.default)) {
Expand Down
90 changes: 90 additions & 0 deletions test/utils_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,96 @@ describe("utils", () => {
},
});
});

it("should populate defaults for nested dependencies when formData passed to computeDefaults is undefined", () => {
const schema = {
type: "object",
properties: {
can_1: {
type: "object",
properties: {
phy: {
title: "Physical",
description: "XYZ",
type: "object",
properties: {
bit_rate_cfg_mode: {
title: "Sub title",
description: "XYZ",
type: "integer",
default: 0,
},
},
dependencies: {
bit_rate_cfg_mode: {
oneOf: [
{
properties: {
bit_rate_cfg_mode: {
enum: [0],
},
},
},
],
},
},
},
},
},
},
};
expect(getDefaultFormState(schema, undefined)).eql({
can_1: {
phy: {
bit_rate_cfg_mode: 0,
},
},
});
});

it("should not crash for defaults for nested dependencies when formData passed to computeDefaults is null", () => {
const schema = {
type: "object",
properties: {
can_1: {
type: "object",
properties: {
phy: {
title: "Physical",
description: "XYZ",
type: "object",
properties: {
bit_rate_cfg_mode: {
title: "Sub title",
description: "XYZ",
type: "integer",
default: 0,
},
},
dependencies: {
bit_rate_cfg_mode: {
oneOf: [
{
properties: {
bit_rate_cfg_mode: {
enum: [0],
},
},
},
],
},
},
},
},
},
},
};
expect(getDefaultFormState(schema, { can_1: { phy: null } })).eql({
can_1: {
phy: null,
},
});
});
});
});

Expand Down

0 comments on commit 8c81cc7

Please sign in to comment.