-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Question about appropriate testing strategy for dispatch returns #14
Comments
What specifically do you want to test in it? Control flow? Would you mock network request or let them happen? |
The network request would get mocked. And I suppose I'm looking to test control flow; when the action is called, does it a: return the expected function and b: does that function do what is expected of it. I'm wondering now if that's an acceptable test or not; it seems like it could be brittle. |
I ended up (crudely) coming up with the following solution: https://gist.github.com/kwhitaker/f0d8624af9061f3bd8a9 The |
You don't need |
@kwhitaker How did you mock network requests in this case? Did you dispatch an action yourself, simulating async action dispatch which has the response object? |
@salmanm I used Jasmine AJAX to handle that:
|
I am testing my react-component and check that it calls dispatch in an appropriate place. To check that my action creator returned the proper function, I use named function and check its name: //action creator
export function fetchData(query) {
return function fetchDataAction(dispatch) {/*some loading logic here*/}
} //dispatch
onLoadClick() {
this.props.dispatch(fetchData(this.props.query));
} //test
const dispatch = jasmine.createSpy();
const component = renderIntoDocument(<Component dispatch={dispatch} />);
Simulate.click(findRenderedDOMComponentWithClass(component, 'load-button'));
expect(dispatch).toHaveBeenCalledWith(jasmine.objectContaining({name: 'fetchDataAction'}))
|
@just-boris For example, I had one lib containing all util functions which take care of ajax calls, I Testing against function name doesn't look very useful. Because the test would pass as long as the function name is same even if the the function behaviour has changed, which should ideally fail the test. |
@salmanm I did it because it is easier to test isolated store and its action handling logic, including ajax calls. My point was in that it is nice to have mocked UPD: there is an example of my tests: |
This is probably my limited understanding of async JS unit testing coming through, but I have a question about the appropriate strategy for testing actions dispatched by
redux-thunk
.I have an action to log users out of a system:
I'm using Jasmine as my testing framework, but I'm less interested in framework specifics as I am about testing the various parts of this function.
I can easily assert that
logout()
returns a function, but should I then execute that function and test it as well, and so on down the chain? If this isn't the right place for this, I'll drop it on Stack or somewhere where it belongs :)Thanks so much
The text was updated successfully, but these errors were encountered: