EditViewDataManagerProvider: Fix and improve tests

This commit is contained in:
Gustav Hansen 2022-09-13 09:32:35 +02:00
parent 1a1e95c928
commit e1c4310bc5
2 changed files with 73 additions and 6 deletions

View File

@ -122,7 +122,7 @@ const reducer = (state, action) =>
break;
}
case 'INIT_FORM': {
const { initialValues, relationalFields } = action;
const { initialValues, relationalFields = [] } = action;
draftState.formErrors = {};
@ -142,10 +142,12 @@ const reducer = (state, action) =>
*/
...relationalFields.reduce((acc, name) => {
return (acc[name] = {
...(initialValues?.[name] ?? {}),
acc[name] = {
...(state.initialData?.[name] ?? {}),
});
...(initialValues?.[name] ?? {}),
};
return acc;
}, {}),
};
@ -171,10 +173,12 @@ const reducer = (state, action) =>
...relationalFields.reduce((acc, name) => {
const { connect, disconnect, ...currentState } = state.modifiedData?.[name] ?? {};
return (acc[name] = {
acc[name] = {
...(initialValues?.[name] ?? {}),
...(currentState ?? {}),
});
};
return acc;
}, {}),
};

View File

@ -517,6 +517,69 @@ describe('CONTENT MANAGER | COMPONENTS | EditViewDataManagerProvider | reducer',
expect(reducer(state, action)).toEqual(expected);
});
it('should take relational fields into account, when setting the state', () => {
const state = {
...initialState,
formErrors: true,
initialData: {
relation: {
something: true,
},
},
modifiedData: true,
modifiedDZName: true,
shouldCheckErrors: true,
};
const expected = {
...initialState,
formErrors: {},
initialData: { ok: true, relation: { something: true, count: 10 } },
modifiedData: { ok: true, relation: { count: 10 } },
modifiedDZName: null,
shouldCheckErrors: false,
};
const action = {
type: 'INIT_FORM',
initialValues: { ok: true, relation: { count: 10 } },
relationalFields: ['relation'],
};
expect(reducer(state, action)).toEqual(expected);
});
it('should reset connect/ disconnect for relational fields', () => {
const state = {
...initialState,
formErrors: true,
initialData: {},
modifiedData: {
relation: {
connect: [{ id: 1 }],
disconnect: [{ id: 2 }],
},
},
modifiedDZName: true,
shouldCheckErrors: true,
};
const expected = {
...initialState,
formErrors: {},
initialData: { ok: true, relation: { count: 10 } },
modifiedData: { ok: true, relation: { count: 10 } },
modifiedDZName: null,
shouldCheckErrors: false,
};
const action = {
type: 'INIT_FORM',
initialValues: { ok: true, relation: { count: 10 } },
relationalFields: ['relation'],
};
expect(reducer(state, action)).toEqual(expected);
});
});
describe('MOVE_COMPONENT_FIELD', () => {