Skip to content

Commit b8c41c5

Browse files
authored
Merge pull request #1423 from rvsia/fixParse
fix(renderer): pass field property to when in sequence and not
2 parents d3ef5f7 + 31bb391 commit b8c41c5

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export const parseCondition = (condition, values, field) => {
7171
if (condition.sequence) {
7272
return condition.sequence.reduce(
7373
(acc, curr) => {
74-
const result = parseCondition(curr, values);
74+
const result = parseCondition(curr, values, field);
7575

7676
return {
7777
sets: [...acc.sets, ...(result.set ? [result.set] : [])],
@@ -90,7 +90,7 @@ export const parseCondition = (condition, values, field) => {
9090
}
9191

9292
if (condition.not) {
93-
return !parseCondition(condition.not, values).result ? positiveResult : negativeResult;
93+
return !parseCondition(condition.not, values, field).result ? positiveResult : negativeResult;
9494
}
9595

9696
const finalWhen = typeof condition.when === 'function' ? condition.when(field) : condition.when;

packages/react-form-renderer/src/tests/form-renderer/parse-condition.test.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,46 @@ describe('parseCondition', () => {
542542
expect(whenSpyX).toHaveBeenCalledWith(field);
543543
expect(whenSpyY).toHaveBeenCalledWith(field);
544544
});
545+
546+
it('when is function in sequence', () => {
547+
const whenSpy = jest.fn().mockImplementation(() => 'x');
548+
549+
condition = {
550+
sequence: [
551+
{
552+
when: whenSpy,
553+
is: 'yes',
554+
},
555+
],
556+
};
557+
558+
values = {
559+
x: 'yes',
560+
};
561+
562+
expect(parseCondition(condition, values, field)).toEqual({ ...positiveResult, sets: [] });
563+
expect(whenSpy).toHaveBeenCalledWith(field);
564+
});
565+
566+
it('when is function in not', () => {
567+
const whenSpy = jest.fn().mockImplementation(() => 'x');
568+
569+
condition = {
570+
not: [
571+
{
572+
when: whenSpy,
573+
is: 'yes',
574+
},
575+
],
576+
};
577+
578+
values = {
579+
x: 'yes',
580+
};
581+
582+
expect(parseCondition(condition, values, field)).toEqual(negativeResult);
583+
expect(whenSpy).toHaveBeenCalledWith(field);
584+
});
545585
});
546586

547587
it('simple condition - custom function', () => {

0 commit comments

Comments
 (0)