-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
If/Then/Else - Code Review changes from #2506 and tests from #2466 #2700
Merged
epicfaace
merged 11 commits into
rjsf-team:master
from
nickgros:2506-code-review-changes
Feb 18, 2022
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f72a62c
Added tests
Juansasa 75c4d84
Changed resolve method's name
Juansasa a08c563
Added $ref tests
Juansasa 7f88468
Code review changes
nickgros 4a4c045
Merge branch 'master' of https://github.com/rjsf-team/react-jsonschem…
nickgros 76ea7ba
Remove only on test, add integration tests from #2466
nickgros 5ccf7ec
Fixed merge order
Juansasa 17816d3
Update tests method names
Juansasa fa0269a
Merge branch 'master' into if_then_else
Juansasa 5bcb924
Merge branch 'if_then_else' of https://github.com/stakater/react-json…
nickgros 106b441
Code review changes #2700
nickgros File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,321 @@ | ||
import { expect } from "chai"; | ||
|
||
import { createFormComponent, createSandbox } from "./test_utils"; | ||
|
||
describe("conditional items", () => { | ||
let sandbox; | ||
|
||
beforeEach(() => { | ||
sandbox = createSandbox(); | ||
}); | ||
|
||
afterEach(() => { | ||
sandbox.restore(); | ||
}); | ||
|
||
const schema = { | ||
type: "object", | ||
properties: { | ||
street_address: { | ||
type: "string", | ||
}, | ||
country: { | ||
enum: ["United States of America", "Canada"], | ||
}, | ||
}, | ||
if: { | ||
properties: { country: { const: "United States of America" } }, | ||
}, | ||
then: { | ||
properties: { zipcode: { type: "string" } }, | ||
}, | ||
else: { | ||
properties: { postal_code: { type: "string" } }, | ||
}, | ||
}; | ||
|
||
const schemaWithRef = { | ||
type: "object", | ||
properties: { | ||
country: { | ||
enum: ["United States of America", "Canada"], | ||
}, | ||
}, | ||
if: { | ||
properties: { | ||
country: { | ||
const: "United States of America", | ||
}, | ||
}, | ||
}, | ||
then: { | ||
$ref: "#/definitions/us", | ||
}, | ||
else: { | ||
$ref: "#/definitions/other", | ||
}, | ||
definitions: { | ||
us: { | ||
properties: { | ||
zip_code: { | ||
type: "string", | ||
}, | ||
}, | ||
}, | ||
other: { | ||
properties: { | ||
postal_code: { | ||
type: "string", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
it("should render then when condition is true", () => { | ||
const formData = { | ||
country: "United States of America", | ||
}; | ||
|
||
const { node } = createFormComponent({ | ||
schema, | ||
formData, | ||
}); | ||
|
||
expect(node.querySelector("input[label=zipcode]")).not.eql(null); | ||
expect(node.querySelector("input[label=postal_code]")).to.eql(null); | ||
}); | ||
|
||
it("should render else when condition is false", () => { | ||
const formData = { | ||
country: "France", | ||
}; | ||
|
||
const { node } = createFormComponent({ | ||
schema, | ||
formData, | ||
}); | ||
|
||
expect(node.querySelector("input[label=zipcode]")).to.eql(null); | ||
expect(node.querySelector("input[label=postal_code]")).not.eql(null); | ||
}); | ||
|
||
it("should render control when data has not been filled in", () => { | ||
const formData = {}; | ||
|
||
const { node } = createFormComponent({ | ||
schema, | ||
formData, | ||
}); | ||
|
||
// An empty formData will make the conditional evaluate to true because no properties are required in the if statement | ||
// Please see https://github.com/epoberezkin/ajv/issues/913 | ||
expect(node.querySelector("input[label=zipcode]")).not.eql(null); | ||
expect(node.querySelector("input[label=postal_code]")).to.eql(null); | ||
}); | ||
|
||
it("should render then when condition is true with reference", () => { | ||
const formData = { | ||
country: "United States of America", | ||
}; | ||
|
||
const { node } = createFormComponent({ | ||
schema: schemaWithRef, | ||
formData, | ||
}); | ||
|
||
expect(node.querySelector("input[label=zip_code]")).not.eql(null); | ||
expect(node.querySelector("input[label=postal_code]")).to.eql(null); | ||
}); | ||
|
||
it("should render else when condition is false with reference", () => { | ||
const formData = { | ||
country: "France", | ||
}; | ||
|
||
const { node } = createFormComponent({ | ||
schema: schemaWithRef, | ||
formData, | ||
}); | ||
|
||
expect(node.querySelector("input[label=zip_code]")).to.eql(null); | ||
expect(node.querySelector("input[label=postal_code]")).not.eql(null); | ||
}); | ||
|
||
describe("allOf if then else", () => { | ||
const schemaWithAllOf = { | ||
type: "object", | ||
properties: { | ||
street_address: { | ||
type: "string", | ||
}, | ||
country: { | ||
enum: [ | ||
"United States of America", | ||
"Canada", | ||
"United Kingdom", | ||
"France", | ||
], | ||
}, | ||
}, | ||
allOf: [ | ||
{ | ||
if: { | ||
properties: { country: { const: "United States of America" } }, | ||
}, | ||
then: { | ||
properties: { zipcode: { type: "string" } }, | ||
}, | ||
}, | ||
{ | ||
if: { | ||
properties: { country: { const: "United Kingdom" } }, | ||
}, | ||
then: { | ||
properties: { postcode: { type: "string" } }, | ||
}, | ||
}, | ||
{ | ||
if: { | ||
properties: { country: { const: "France" } }, | ||
}, | ||
then: { | ||
properties: { telephone: { type: "string" } }, | ||
}, | ||
}, | ||
], | ||
}; | ||
|
||
it("should render correctly when condition is true in allOf (1)", () => { | ||
const formData = { | ||
country: "United States of America", | ||
}; | ||
|
||
const { node } = createFormComponent({ | ||
schema: schemaWithAllOf, | ||
formData, | ||
}); | ||
|
||
expect(node.querySelector("input[label=zipcode]")).not.eql(null); | ||
}); | ||
|
||
it("should render correctly when condition is false in allOf (1)", () => { | ||
const formData = { | ||
country: "", | ||
}; | ||
|
||
const { node } = createFormComponent({ | ||
schema: schemaWithAllOf, | ||
formData, | ||
}); | ||
|
||
expect(node.querySelector("input[label=zipcode]")).to.eql(null); | ||
}); | ||
|
||
it("should render correctly when condition is true in allof (2)", () => { | ||
const formData = { | ||
country: "United Kingdom", | ||
}; | ||
|
||
const { node } = createFormComponent({ | ||
schema: schemaWithAllOf, | ||
formData, | ||
}); | ||
|
||
expect(node.querySelector("input[label=postcode]")).not.eql(null); | ||
expect(node.querySelector("input[label=zipcode]")).to.eql(null); | ||
expect(node.querySelector("input[label=telephone]")).to.eql(null); | ||
}); | ||
|
||
it("should render correctly when condition is true in allof (3)", () => { | ||
const formData = { | ||
country: "France", | ||
}; | ||
|
||
const { node } = createFormComponent({ | ||
schema: schemaWithAllOf, | ||
formData, | ||
}); | ||
|
||
expect(node.querySelector("input[label=postcode]")).to.eql(null); | ||
expect(node.querySelector("input[label=zipcode]")).to.eql(null); | ||
expect(node.querySelector("input[label=telephone]")).not.eql(null); | ||
}); | ||
|
||
const schemaWithAllOfRef = { | ||
type: "object", | ||
properties: { | ||
street_address: { | ||
type: "string", | ||
}, | ||
country: { | ||
enum: [ | ||
"United States of America", | ||
"Canada", | ||
"United Kingdom", | ||
"France", | ||
], | ||
}, | ||
}, | ||
definitions: { | ||
unitedkingdom: { | ||
properties: { postcode: { type: "string" } }, | ||
}, | ||
}, | ||
allOf: [ | ||
{ | ||
if: { | ||
properties: { country: { const: "United Kingdom" } }, | ||
}, | ||
then: { | ||
$ref: "#/definitions/unitedkingdom", | ||
}, | ||
}, | ||
], | ||
}; | ||
|
||
it("should render correctly when condition is true when then contains a reference", () => { | ||
const formData = { | ||
country: "United Kingdom", | ||
}; | ||
|
||
const { node } = createFormComponent({ | ||
schema: schemaWithAllOfRef, | ||
formData, | ||
}); | ||
|
||
expect(node.querySelector("input[label=postcode]")).not.eql(null); | ||
}); | ||
}); | ||
|
||
it("handles additionalProperties with if then else", () => { | ||
/** | ||
* Ensures that fields defined in "then" or "else" (e.g. zipcode) are handled | ||
* with regular form fields, not as additional properties | ||
*/ | ||
|
||
const formData = { | ||
country: "United States of America", | ||
zipcode: "12345", | ||
otherKey: "otherValue", | ||
}; | ||
const { node } = createFormComponent({ | ||
schema: { | ||
...schema, | ||
additionalProperties: true, | ||
}, | ||
formData, | ||
}); | ||
|
||
// The zipcode field exists, but not as an additional property | ||
expect(node.querySelector("input[label=zipcode]")).not.eql(null); | ||
expect( | ||
node.querySelector("div.form-additional input[label=zipcode]") | ||
).to.eql(null); | ||
|
||
// The "otherKey" field exists as an additional property | ||
expect( | ||
node.querySelector("div.form-additional input[label=otherKey]") | ||
).not.eql(null); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Document this