Skip to content
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

Merged
merged 3 commits into from Jan 8, 2016

Conversation

davidlgj
Copy link
Contributor

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.

If a function is supplied instead of an expected action
then that function is then called with the incoming action
as an argument.
@arnaudbenard
Copy link
Contributor

That looks good! I would love to know how it would test multiple expected actions with a custom validator.

@tshelburne
Copy link

@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?

@arnaudbenard
Copy link
Contributor

@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

@scags9876
Copy link

+1 ... I needed the same thing and came up with the exact code before finding this PR. 😄

@davidlgj
Copy link
Contributor Author

davidlgj commented Jan 7, 2016

@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.

@chibicode
Copy link

@davidlgj great work, just wanted to point out that there seems to be a conflict w/ the master branch.

@davidlgj
Copy link
Contributor Author

davidlgj commented Jan 8, 2016

@chibicode Yeah, another PR got pulled in into master before this. I'll see if i can fix it by merging in master again.

@arnaudbenard
Copy link
Contributor

Very nice addition, I am merging that in. Thanks everyone for the help.

arnaudbenard pushed a commit that referenced this pull request Jan 8, 2016
Custom test function instead of expected action.
@arnaudbenard arnaudbenard merged commit d297d40 into reduxjs:master Jan 8, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants