New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom test function instead of expected action. #6
Conversation
If a function is supplied instead of an expected action then that function is then called with the incoming action as an argument.
That looks good! I would love to know how it would test multiple expected actions with a custom validator. |
@davidlgj +1, needed this and didn't realize it a couple weeks ago @arnaudbenard Wouldn't it just take passing the function in the number of times you expect it to run? |
@tshelburne Yeah, I will put that in. I want to fix #7 as well at the same time. I will keep you in the loop when I push a new version |
+1 ... I needed the same thing and came up with the exact code before finding this PR. 😄 |
@arnaudbenard, as @tshelburne said you can pass a function several times, once for each action to match. You can also mix and match functions with ordinary action object. Something like this: // Test in mocha
it('should dispatch actions', (done) => {
const getState = {}; // initial state of the store
const action = { type: 'ADD_TODO' };
const action2 = { type: 'SET_TIMESTAMP', time: Date.now() };
const expectedActions = [
action,
(incomingAction) => {
if (incomingAction.type !== 'SET_TIMESTAMP') {
throw Error('Expected action of type SET_TIMESTAMP');
}
}
];
const store = mockStore(getState, expectedActions, done);
store.dispatch(action);
store.dispatch(action2);
}) I updated the test as well. |
@davidlgj great work, just wanted to point out that there seems to be a conflict w/ the master branch. |
@chibicode Yeah, another PR got pulled in into master before this. I'll see if i can fix it by merging in master again. |
Very nice addition, I am merging that in. Thanks everyone for the help. |
Custom test function instead of expected action.
Hi,
this is a PR for a small tweak that I needed, and I was
hoping you might find it useful.
If a function is supplied instead of an expected action
then that function is called with the incoming action
as an argument. This is useful when your action creators
adds a dynamic part to the action object, like a time stamp
or some such.
The supplied "test function" should throw an error if the
action doesn't match what it expects.