-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1.1.0: allow passing a filter function
- Loading branch information
Showing
3 changed files
with
28 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,17 @@ | ||
import isFunction from 'lodash/lang/isFunction'; | ||
import isFunction from 'lodash/lang/isFunction' | ||
|
||
// redux-ignore higher order reducer | ||
export default function ignoreActions (reducer, actions = []) { | ||
let ignorePredicate = isFunction(actions) | ||
? actions | ||
: (action) => actions.indexOf(action.type) >= 0; | ||
: (action) => actions.indexOf(action.type) >= 0 | ||
|
||
return (state, action) => { | ||
if (!ignorePredicate(action)) { | ||
return reducer(state, action); | ||
return reducer(state, action) | ||
} | ||
|
||
return state; | ||
return state | ||
} | ||
} | ||
// /redux-ignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,51 @@ | ||
import assert from 'assert'; | ||
import should from 'should'; | ||
import ignoreActions from '../src/index'; | ||
import assert from 'assert' | ||
import ignoreActions from '../src/index' | ||
|
||
describe('ignoreActions()', () => { | ||
let reducer = (state, action) => { | ||
switch (action.type) { | ||
case 'FOO': | ||
return 'foo-state'; | ||
return 'foo-state' | ||
case 'BAR': | ||
return 'bar-state'; | ||
return 'bar-state' | ||
default: | ||
return 'default-state'; | ||
return 'default-state' | ||
} | ||
}; | ||
} | ||
|
||
it('should ignore actions with specified types in array', () => { | ||
let ignoringReducer = ignoreActions(reducer, ['BAR']); | ||
let action = { type: 'BAR' }; | ||
let ignoringReducer = ignoreActions(reducer, ['BAR']) | ||
let action = { type: 'BAR' } | ||
|
||
assert.equal( | ||
ignoringReducer('testing', action), | ||
'testing'); | ||
}); | ||
'testing') | ||
}) | ||
|
||
it('should not ignore actions that do not have types in array', () => { | ||
let ignoringReducer = ignoreActions(reducer, ['BAR']); | ||
let action = { type: 'FOO' }; | ||
let ignoringReducer = ignoreActions(reducer, ['BAR']) | ||
let action = { type: 'FOO' } | ||
|
||
assert.equal( | ||
ignoringReducer('testing', action), | ||
'foo-state'); | ||
}); | ||
'foo-state') | ||
}) | ||
|
||
it('should allow all actions when no action types array is specified', () => { | ||
let ignoringReducer = ignoreActions(reducer); | ||
let action = { type: 'BAZ' }; | ||
let ignoringReducer = ignoreActions(reducer) | ||
let action = { type: 'BAZ' } | ||
|
||
assert.equal( | ||
ignoringReducer('testing', action), | ||
'default-state'); | ||
}); | ||
'default-state') | ||
}) | ||
|
||
it('should work with a predicate function for actions', () => { | ||
let ignoringReducer = ignoreActions(reducer, (a) => a.invalid); | ||
let action = { type: 'BAR', invalid: true }; | ||
let ignoringReducer = ignoreActions(reducer, (a) => a.invalid) | ||
let action = { type: 'BAR', invalid: true } | ||
|
||
assert.equal( | ||
ignoringReducer('testing', action), | ||
'testing'); | ||
}); | ||
}); | ||
'testing') | ||
}) | ||
}) |