-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Async actions runs partly when testing component #1584
Comments
#1581 might help how you write a test to wait for async action. |
@koba04 thanks, I looked at #1581 but haven't found solution there. What I don't understand that if I don't use What is an elegant way to wait for such async action? |
@gerhardberger it('renders the app correctly after fetch', async () => {
const config = { token: 'dummy token' };
const store = createStore(rootReducer, applyMiddleware(thunk));
window.fetch = jest.fn().mockImplementation(() => Promise.resolve({
ok: true,
json: () => config
}));
const wrapper = shallow(<App store={store} />);
// Wait the microtasks(Promises)
await new Promise(r => setTimeout(r));
wrapper.dive().update();
expect(wrapper.props().store.getState().overview.config).toEqual(config);
}); |
Thanks, although I think this is not the most elegant solution, maybe we should think of a better way to test for async actions. The other problem I have, that the |
@gerhardberger Could you create a repository or example to reproduce it? |
Current behavior
I have an async redux action that does a GET request with the Fetch API, and during the action does two action dispatches (loading and success):
I have a container component that passes this action to a presentational component that dispatches it in its
componentDidMount
:I want to test this container component, by observing that it made the appropriate state changes, after it fetched the config:
The async action starts running after the
wrapper.dive().update()
call, and returns to the test at theconst data = await response.json()
, so I cannot make the proper assertions. However the async action completes after the test exited.Expected behavior
When an async action is run it runs all the way and doesn't return after the second
await
.Your environment
API
Version
Adapter
The text was updated successfully, but these errors were encountered: