feat(object-reducer): object reducer reset payloads can be provided

The payload for an object reset action will be applied on top of the default
state of an object.
This commit is contained in:
Kai Moseley 2017-10-17 17:19:22 +01:00
parent 877d3646d5
commit 89ca4bf8fa
2 changed files with 10 additions and 1 deletions

View File

@ -27,6 +27,14 @@ describe('ObjectReducer', () => {
it('reducer should return the initial state', () => { it('reducer should return the initial state', () => {
expect(reset.reducer({ foo: 1 }, undefined, { foo: 2 })).toEqual({ foo: 2 }); expect(reset.reducer({ foo: 1 }, undefined, { foo: 2 })).toEqual({ foo: 2 });
}); });
it('reducer should apply the payload (if an object) to the store over the default state', () => {
expect(reset.reducer({ foo: 23, bar: 4 }, { foo: 1 }, { foo: 4, bar: 5 })).toEqual({foo: 1, bar: 5});
});
it('reducer should return initial state if the payload is not an object', () => {
expect(reset.reducer({ foo: 23, bar: 4 }, 'nope!', { foo: 4, bar: 5 })).toEqual({foo: 4, bar: 5});
});
}); });
describe('update', () => { describe('update', () => {

View File

@ -67,7 +67,8 @@ export const DEFAULT_SHAPE_BEHAVIORS: ShapeReducerBehaviorsConfig = {
}, },
reset: { reset: {
reducer(state, payload, initialState) { reducer(state, payload, initialState) {
return initialState; if (!isObject(payload)) return initialState;
return { ...initialState, ...payload };
}, },
validate: false, validate: false,
}, },