Skip to content

How to write tests regarding Action Creators which return functions? #700

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

Closed
rui-ktei opened this issue Sep 10, 2015 · 6 comments
Closed
Labels

Comments

@rui-ktei
Copy link

It's mentioned in the documentation:

In Redux, action creators are functions which return plain objects. When testing action creators we want to test whether the correct action creator was called and also whether the right action was returned.

However, action creators can also return functions like this:

(dispatch, getState) => {...}

In this case, how do I write tests? It's not a simple checking object equality anymore.

Thank you

@por
Copy link

por commented Sep 10, 2015

The counter example app has some tests that cover this.

 it('incrementIfOdd should create increment action', () => {
    const fn = actions.incrementIfOdd();
    expect(fn).toBeA('function');
    const dispatch = expect.createSpy();
    const getState = () => ({ counter: 1 });
    fn(dispatch, getState);
    expect(dispatch).toHaveBeenCalledWith({ type: actions.INCREMENT_COUNTER });
  });

Maybe an idea to add this to the documentation?

@rui-ktei
Copy link
Author

@por
Thank you very much for the help!

@gaearon
Copy link
Contributor

gaearon commented Sep 13, 2015

Duplicate of #546.
See #546 (comment).

Maybe an idea to add this to the documentation?

I'd happily accept a pull request.

@mik01aj
Copy link

mik01aj commented Jan 12, 2016

However, action creators can also return functions like this:

(dispatch, getState) => {...}

I see it's not true anymore... Did the API change? I guess I should use https://github.com/gaearon/redux-thunk instead.

@gaearon
Copy link
Contributor

gaearon commented Jan 12, 2016

Yes, use thunk if you want to have this.

@just-boris
Copy link
Contributor

Also, there is a related issue: reduxjs/redux-thunk#14

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants