Skip to content
This repository was archived by the owner on Aug 23, 2022. It is now read-only.

Commit 9a278ce

Browse files
committed
Adding actions.omit() + unit tests
1 parent 5dcb843 commit 9a278ce

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

src/actions/model-actions.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,24 @@ const merge = (model, values) => (dispatch, getState) => {
131131
});
132132
};
133133

134+
const omit = (model, props = []) => (dispatch, getState) => {
135+
const value = _get(getState(), model, {});
136+
137+
const propsArray = typeof props === 'string'
138+
? [props]
139+
: props;
140+
141+
const newValue = propsArray.reduce(
142+
(acc, prop) => icepick.dissoc(acc, prop),
143+
value);
144+
145+
dispatch({
146+
type: actionTypes.CHANGE,
147+
model,
148+
value: newValue,
149+
});
150+
};
151+
134152
const load = (model, values) => change(model, values, {
135153
silent: true,
136154
});
@@ -147,4 +165,5 @@ export default {
147165
toggle,
148166
xor,
149167
load,
168+
omit,
150169
};

test/model-actions-spec.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,18 @@ describe('model actions', () => {
167167
expected: { foo: { bar: 'new', one: 'two', untouched: 'intact' } },
168168
},
169169
],
170+
omit: [
171+
{
172+
init: { one: 1, two: 2, three: 3 },
173+
params: ['test', 'two'],
174+
expected: { one: 1, three: 3 },
175+
},
176+
{
177+
init: { one: 1, two: 2, three: 3 },
178+
params: ['test', ['one', 'three']],
179+
expected: { two: 2 },
180+
},
181+
],
170182
};
171183

172184
/* eslint-disable array-callback-return */
@@ -242,4 +254,25 @@ describe('model actions', () => {
242254
actions.filter('test.items', (n) => n % 2)(dispatch, getState);
243255
});
244256
});
257+
258+
describe('omit()', () => {
259+
it('should dissociate props from the form state', (done) => {
260+
const initialState = { one: 1, two: 2, three: 3 };
261+
const reducer = formReducer('test', initialState);
262+
263+
assert.property(reducer(undefined, { type: '' }).fields, 'one');
264+
assert.property(reducer(undefined, { type: '' }).fields, 'two');
265+
assert.property(reducer(undefined, { type: '' }).fields, 'three');
266+
267+
const dispatch = action => {
268+
assert.notProperty(reducer(undefined, action).fields, 'one');
269+
assert.notProperty(reducer(undefined, action).fields, 'three');
270+
done();
271+
};
272+
273+
const getState = () => ({ test: initialState });
274+
275+
actions.omit('test', ['one', 'three'])(dispatch, getState);
276+
});
277+
});
245278
});

0 commit comments

Comments
 (0)