mirror of
https://github.com/strapi/strapi.git
synced 2025-12-28 07:33:17 +00:00
EditViewDataManagerProvider: Fix and improve tests
This commit is contained in:
parent
1a1e95c928
commit
e1c4310bc5
@ -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;
|
||||
}, {}),
|
||||
};
|
||||
|
||||
|
||||
@ -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', () => {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user