Skip to content

Commit

Permalink
fix(renderer): pass field property to when in sequence and not
Browse files Browse the repository at this point in the history
  • Loading branch information
rvsia committed Nov 6, 2023
1 parent d3ef5f7 commit 31bb391
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export const parseCondition = (condition, values, field) => {
if (condition.sequence) {
return condition.sequence.reduce(
(acc, curr) => {
const result = parseCondition(curr, values);
const result = parseCondition(curr, values, field);

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

if (condition.not) {
return !parseCondition(condition.not, values).result ? positiveResult : negativeResult;
return !parseCondition(condition.not, values, field).result ? positiveResult : negativeResult;
}

const finalWhen = typeof condition.when === 'function' ? condition.when(field) : condition.when;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,46 @@ describe('parseCondition', () => {
expect(whenSpyX).toHaveBeenCalledWith(field);
expect(whenSpyY).toHaveBeenCalledWith(field);
});

it('when is function in sequence', () => {
const whenSpy = jest.fn().mockImplementation(() => 'x');

condition = {
sequence: [
{
when: whenSpy,
is: 'yes',
},
],
};

values = {
x: 'yes',
};

expect(parseCondition(condition, values, field)).toEqual({ ...positiveResult, sets: [] });
expect(whenSpy).toHaveBeenCalledWith(field);
});

it('when is function in not', () => {
const whenSpy = jest.fn().mockImplementation(() => 'x');

condition = {
not: [
{
when: whenSpy,
is: 'yes',
},
],
};

values = {
x: 'yes',
};

expect(parseCondition(condition, values, field)).toEqual(negativeResult);
expect(whenSpy).toHaveBeenCalledWith(field);
});
});

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

0 comments on commit 31bb391

Please sign in to comment.