feat(object-reducers): replace will now shallow merge with initial state

This commit is contained in:
Kai Moseley 2017-10-17 17:28:25 +01:00
parent 751ccdd629
commit 4ff81b4035
2 changed files with 7 additions and 4 deletions

View File

@ -17,8 +17,8 @@ describe('ObjectReducer', () => {
it('reducer should return the state if the payload is undefined', () => {
expect(replace.reducer({ foo: 1 }, undefined)).toEqual({ foo: 1 });
});
it('reducer should return the new state', () => {
expect(replace.reducer({ foo: 1 }, { foo: 2 })).toEqual({ foo: 2 });
it('reducer should return the new state combined with the initial state', () => {
expect(replace.reducer({ foo: 1, bar: 3 }, { foo: 2 }, { foo: 1, bar: 5 })).toEqual({ foo: 2, bar: 5 });
});
});

View File

@ -73,9 +73,12 @@ export const DEFAULT_SHAPE_BEHAVIORS: ShapeReducerBehaviorsConfig = {
validate: false,
},
replace: {
reducer(state, payload) {
reducer(state, payload, initialState) {
if (!payload) return state;
return payload;
return {
...initialState,
...payload,
};
},
validate: true,
}