Skip to content

Commit

Permalink
add tests for getFieldsWithRules
Browse files Browse the repository at this point in the history
  • Loading branch information
vashjs committed Feb 6, 2025
1 parent e094a89 commit d101276
Showing 1 changed file with 158 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ import {
getExceptionalOptionsLength,
getExtraActions,
getFilteredFields,
getNormalizedFieldsRules, isAddButtonShown,
getFieldsWithRules,
getNormalizedFieldsRules,
isAddButtonShown,
isContentUpdatesFormValid,
} from './helpers';

Expand Down Expand Up @@ -1653,5 +1655,160 @@ describe('ContentUpdatesForm helpers', () => {
});
});
});

describe('getFieldsWithRules', () => {
it('should return fields unchanged if option is not STATISTICAL_CODE', () => {
const fields = [
{
option: OPTIONS.STATISTICAL_CODE,
options: [
{ value: OPTIONS.STATISTICAL_CODE, hidden: false },
{ value: OPTIONS.ADMINISTRATIVE_NOTE, hidden: false },
],
},
{
option: OPTIONS.ADMINISTRATIVE_NOTE,
options: [
{ value: OPTIONS.STATISTICAL_CODE, hidden: false },
{ value: OPTIONS.ANOTHER_OPTION, hidden: false },
],
},
];

// When the passed option is not STATISTICAL_CODE,
// the function should return the original fields array unchanged.
const result = getFieldsWithRules({
fields,
option: OPTIONS.ADMINISTRATIVE_NOTE,
value: ACTIONS.REMOVE_ALL, // value is irrelevant in this case
rowIndex: 0,
});

expect(result).toEqual(fields);
});

it('should update the hidden property on each option when value is not REMOVE_ALL', () => {
const fields = [
{
option: OPTIONS.STATISTICAL_CODE,
options: [
{ value: OPTIONS.STATISTICAL_CODE, hidden: true },
{ value: OPTIONS.ADMINISTRATIVE_NOTE, hidden: true },
],
},
{
option: OPTIONS.ADMINISTRATIVE_NOTE,
options: [
{ value: OPTIONS.STATISTICAL_CODE, hidden: true },
{ value: OPTIONS.ADMINISTRATIVE_NOTE, hidden: true },
],
},
];

// Using a value that is not REMOVE_ALL should not trigger row removal.
// However, note that the mapping updates each option:
// For options with value STATISTICAL_CODE, the new hidden value becomes (value === REMOVE_ALL), which is false.
// For other options, the original hidden value remains.
const result = getFieldsWithRules({
fields,
option: OPTIONS.STATISTICAL_CODE,
value: 'SOME_OTHER_ACTION', // not equal to ACTIONS.REMOVE_ALL
rowIndex: 0,
});

const expected = [
{
...fields[0],
options: [
{ value: OPTIONS.STATISTICAL_CODE, hidden: false }, // updated because (false)
{ value: OPTIONS.ADMINISTRATIVE_NOTE, hidden: true },
],
},
{
...fields[1],
options: [
{ value: OPTIONS.STATISTICAL_CODE, hidden: false },
{ value: OPTIONS.ADMINISTRATIVE_NOTE, hidden: true },
],
},
];

expect(result).toEqual(expected);
});

it('should remove non-current rows with STATISTICAL_CODE when value equals REMOVE_ALL', () => {
const fields = [
{
option: OPTIONS.STATISTICAL_CODE,
options: [
{ value: OPTIONS.STATISTICAL_CODE, hidden: false },
{ value: OPTIONS.ADMINISTRATIVE_NOTE, hidden: false },
],
id: 1,
},
{
option: OPTIONS.STATISTICAL_CODE,
options: [
{ value: OPTIONS.STATISTICAL_CODE, hidden: false },
{ value: OPTIONS.ADMINISTRATIVE_NOTE, hidden: false },
],
id: 2,
},
{
option: OPTIONS.ADMINISTRATIVE_NOTE,
options: [
{ value: OPTIONS.STATISTICAL_CODE, hidden: false },
{ value: OPTIONS.ADMINISTRATIVE_NOTE, hidden: false },
],
id: 3,
},
];

// When value equals REMOVE_ALL and the passed option is STATISTICAL_CODE:
// - Any field with option STATISTICAL_CODE that is not in the current row (rowIndex) should be removed.
// - The current row (index 1) is kept.
// - Also, in the remaining fields, for each option with value STATISTICAL_CODE, hidden is set to true.
const result = getFieldsWithRules({
fields,
option: OPTIONS.STATISTICAL_CODE,
value: ACTIONS.REMOVE_ALL,
rowIndex: 1,
});

// Field at index 0 is removed because it is STATISTICAL_CODE and not the current row.
// Field at index 1 is kept and updated.
// Field at index 2 is kept (its option is not STATISTICAL_CODE) but its inner options are updated:
// any option with value STATISTICAL_CODE becomes hidden.
const expected = [
{
...fields[1],
options: [
{ value: OPTIONS.STATISTICAL_CODE, hidden: true },
{ value: OPTIONS.ADMINISTRATIVE_NOTE, hidden: false },
],
},
{
...fields[2],
options: [
{ value: OPTIONS.STATISTICAL_CODE, hidden: true },
{ value: OPTIONS.ADMINISTRATIVE_NOTE, hidden: false },
],
},
];

expect(result).toEqual(expected);
});

it('should return an empty array when fields array is empty', () => {
const result = getFieldsWithRules({
fields: [],
option: OPTIONS.STATISTICAL_CODE,
value: ACTIONS.REMOVE_ALL,
rowIndex: 0,
});

expect(result).toEqual([]);
});
});
});
});

0 comments on commit d101276

Please sign in to comment.