-
Notifications
You must be signed in to change notification settings - Fork 149
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 don't always return Promise #97
Comments
You need to return a promise in your action |
@arnaudbenard Hmm, I think I'v tried, I will try ASAP. Thanks for you answer :) |
@DCKT you can actually define your export const logout = () => (dispatch) => {
dispatch(resetItems(['credentials']))
return dispatch({ type: types.REMOVE });
} or even: export const logout = () => (dispatch) =>
dispatch(resetItems(['credentials'])).then(() => dispatch({ type: types.REMOVE })); |
@DCKT Any luck with this? Having the same issue. |
Hello @arnaudbenard Sorry for the late reply, I've tried all the suggested answer but I got the same, it looks like dispatch is not a Promise (but it is). |
@DCKT In your original implementation: // action creator returns undefined
export const logout = () => (dispatch) => {
dispatch(resetItems(['credentials']))
dispatch({ type: types.REMOVE })
} Your test is trying to run Similarly for the implementation that returns // incorrect: returns { type: types.REMOVE }
export const logout = () => (dispatch) => {
dispatch(resetItems(['credentials']))
return dispatch({ type: types.REMOVE });
} Your test would be trying to run You need to explicitly return a promise. // returns Promise.resolve(), which is thenable
export const logout = () => (dispatch) => {
dispatch(resetItems(['credentials']));
return Promise.resolve(removeItem());
}; In this instance, your test would be trying to read On a side note, you could have also rewritten your test to not expect a promise: // test file
test('action should logout the user', () => {
const mockedStore = mockStore({})
mockedStore.dispatch(logout());
expect(mockedStore.getActions()).toEqual([
{ type: types.RESET_ITEMS, items: ['credentials'] },
{ type: types.REMOVE }
]);
}) I think this would be the correct fix to your problem. You shouldn't have to fit your source code into your tests. |
Hello,
I'm using this module for testing my React Native app and I have a little problem with one async actions which not work. The other works properly but not the last one :
Here is the test code :
And the async action :
Do you have an idea about this ? If I put a
setTimeout
around thegetActions
function, the test pass properly.Thanks for your help !
The text was updated successfully, but these errors were encountered: