vendredi 4 janvier 2019

Testing Redux async redux action within another async action

as title says I'm trying to test one async action within another..

I have a login action that in success dispatches login_success(just switches loading to false), saves the token in localStorage and also calls get_current_user which makes another network request to get user based on id.

test('login dispatches login, login success and get current user if everything is okay', () => {

    let expectedActions = [
        SIGN_IN,
        SIGN_IN_SUCCESS,
        GET_CURRENT_USER,
        GET_CURRENT_USER_SUCCESS
    ]

    window.fetch = jest.fn().mockImplementationOnce(() =>
        Promise.resolve({ status: 200, ok: true, json: () => Promise.resolve({ user: { user: { _id: "123" }, token: "faketoken", message: "Logged in" } }) }))

    return store.dispatch(signIn({ username: "fakeone", password: "anotherfake" })).then(() => {

        const actualActions = store.getActions().map(actions => actions.type)

        expect(actualActions).toEqual(expectedActions)

    })
})

Currently it returns failed test since it doesn't dispatch get_current_user_success, there is no mocked fetch request. I can't figure out where to mock that request, tried after first request, in then block but it doesn't work.

Aucun commentaire:

Enregistrer un commentaire