Skip to content

Commit 6b3c174

Browse files
committed
Add tests for condition
1 parent 69a888e commit 6b3c174

File tree

7 files changed

+467
-61
lines changed

7 files changed

+467
-61
lines changed

packages/react-form-renderer/demo/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const validatorMapper = {
3030
)
3131
};
3232

33+
// eslint-disable-next-line no-unused-vars
3334
const asyncValidatorSchema = {
3435
fields: [
3536
{

packages/react-form-renderer/src/form-renderer/condition.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,24 @@ const fieldCondition = (value, { is, isNotEmpty, isEmpty, pattern, notMatch, fla
3131
export const parseCondition = (condition, values) => {
3232
let positiveResult = {
3333
visible: true,
34-
...condition.then
34+
...condition.then,
35+
result: true
3536
};
3637

3738
let negativeResult = {
3839
visible: false,
39-
...condition.else
40+
...condition.else,
41+
result: false
4042
};
4143

4244
if (Array.isArray(condition)) {
43-
return !condition.map((condition) => parseCondition(condition, values)).some((result) => result === false) ? positiveResult : negativeResult;
45+
return !condition.map((condition) => parseCondition(condition, values)).some(({ result }) => result === false) ? positiveResult : negativeResult;
4446
}
4547

4648
if (condition.and) {
47-
return !condition.and.map((condition) => parseCondition(condition, values)).some((result) => result === false) ? positiveResult : negativeResult;
49+
return !condition.and.map((condition) => parseCondition(condition, values)).some(({ result }) => result === false)
50+
? positiveResult
51+
: negativeResult;
4852
}
4953

5054
if (condition.sequence) {
@@ -53,20 +57,21 @@ export const parseCondition = (condition, values) => {
5357
const result = parseCondition(curr, values);
5458

5559
return {
56-
sets: [...acc.sets, ...[result.set ? result.set : []]],
57-
visible: acc.visible || result.visible
60+
sets: [...acc.sets, ...(result.set ? [result.set] : [])],
61+
visible: acc.visible || result.visible,
62+
result: acc.result || result.result
5863
};
5964
},
6065
{ ...negativeResult, sets: [] }
6166
);
6267
}
6368

6469
if (condition.or) {
65-
return condition.or.map((condition) => parseCondition(condition, values)).some((result) => result === true) ? positiveResult : negativeResult;
70+
return condition.or.map((condition) => parseCondition(condition, values)).some(({ result }) => result === true) ? positiveResult : negativeResult;
6671
}
6772

6873
if (condition.not) {
69-
return !parseCondition(condition.not, values) ? positiveResult : negativeResult;
74+
return !parseCondition(condition.not, values).result ? positiveResult : negativeResult;
7075
}
7176

7277
if (typeof condition.when === 'string') {

packages/react-form-renderer/src/form-renderer/render-form.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const FormConditionWrapper = ({ condition, children }) =>
3737
);
3838

3939
FormConditionWrapper.propTypes = {
40-
condition: PropTypes.object,
40+
condition: PropTypes.oneOfType([PropTypes.object, PropTypes.array]),
4141
children: childrenPropTypes.isRequired
4242
};
4343

@@ -93,7 +93,7 @@ const SingleField = ({ component, condition, hideField, validate, ...rest }) =>
9393

9494
SingleField.propTypes = {
9595
component: PropTypes.string.isRequired,
96-
condition: PropTypes.object,
96+
condition: PropTypes.oneOfType([PropTypes.object, PropTypes.array]),
9797
hideField: PropTypes.bool,
9898
dataType: PropTypes.string,
9999
validate: PropTypes.arrayOf(PropTypes.func),

packages/react-form-renderer/src/tests/form-renderer/__snapshots__/render-form.test.js.snap

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -191,25 +191,26 @@ exports[`renderForm function #condition should render condition field only if co
191191
]
192192
}
193193
>
194-
<Condition
195-
condition={
196-
Array [
197-
Object {
198-
"is": "x",
199-
"when": Array [
200-
"a",
201-
"b",
202-
],
203-
},
204-
Object {
205-
"pattern": /fuzz/,
206-
"when": "c",
207-
},
208-
]
209-
}
210-
>
211-
<FormSpy />
212-
</Condition>
194+
<FormSpy>
195+
<Component
196+
condition={
197+
Array [
198+
Object {
199+
"is": "x",
200+
"when": Array [
201+
"a",
202+
"b",
203+
],
204+
},
205+
Object {
206+
"pattern": /fuzz/,
207+
"when": "c",
208+
},
209+
]
210+
}
211+
values={Object {}}
212+
/>
213+
</FormSpy>
213214
</FormConditionWrapper>
214215
</SingleField>
215216
</ReactFinalForm>
@@ -395,19 +396,20 @@ exports[`renderForm function #condition should render condition field only if on
395396
}
396397
}
397398
>
398-
<Condition
399-
condition={
400-
Object {
401-
"is": "x",
402-
"when": Array [
403-
"a",
404-
"b",
405-
],
399+
<FormSpy>
400+
<Component
401+
condition={
402+
Object {
403+
"is": "x",
404+
"when": Array [
405+
"a",
406+
"b",
407+
],
408+
}
406409
}
407-
}
408-
>
409-
<FormSpy />
410-
</Condition>
410+
values={Object {}}
411+
/>
412+
</FormSpy>
411413
</FormConditionWrapper>
412414
</SingleField>
413415
</ReactFinalForm>

0 commit comments

Comments
 (0)