在调度操作后,redux-mock-store存储的状态不会更新

时间:2022-08-22 21:55:52

I have a logger middleware that shows actions about to be dispatched and next state.

我有一个记录器中间件,显示要调度的动作和下一个状态。

I am writing tests for my action and in the mock store I am dispatching the actions. These successfully dispatch however the mock store state is not being updated (as shown by the aforementioned logger).

我正在为我的行动编写测试,在模拟商店我正在调度行动。这些成功调度但是模拟存储状态没有被更新(如上述记录器所示)。

please note I am using redux-mock-store.

请注意我使用的是redux-mock-store。

//authActionTest.js

it('creates LOGIN_SUCCESS when successful login has occured', ()=>{

//array of expected actions to be dispatched.
const expectedActions = [
  { type: constants.LOGIN_REQUEST },
  { type: 'LOGIN_SUCCESS',
            payload: {
                uid: '123abc',
                role: 'clinician'
    }}
]
const store = mockStore({ auth : {} })

return store.dispatch(actions.loginUser('abc@123.com', 'password123'))
expect(store.getActions()).toEqual(expectedActions)
})

The logger shows the following:

记录器显示以下内容:

//logger
dispatching({ type: 'LOGIN_REQUEST })
next state { auth: {} }
dispatching({ type: 'LOGIN_SUCCESS',
   payload: { uid: 123,
              role: 'clinician'
          })
next state { auth: {} }

1 个解决方案

#1


0  

You are trying to test an async action.

您正在尝试测试异步操作。

So to answer this question, we can follow what the redux docs are suggesting.

所以为了回答这个问题,我们可以按照redux文档提出的建议。

  describe('async actions', () => {
    afterEach(() => {
      nock.cleanAll();
    });
    it('creates LOGIN_SUCCESS on successful login', () => {
      nock('http://example.com/')
        .get('/login')
        .reply(200, payload: { uid: 123, role: 'clinician' });

      const expectedActions = [
        { type: types.LOGIN_REQUEST },
        { type: types.LOGIN_SUCCESS, payload: { uid: 123, role: 'clinician' } },
      ];
      const store = mockStore({ auth: {} });

      return store.dispatch(actions.loginUser())
        .then(() => { // return of async actions
          expect(store.getActions()).toEqual(expectedActions);
        });
    });
  });

You may need to do some modifications to the above code to make it working.

您可能需要对上面的代码进行一些修改才能使其正常工作。

If you are using axios, I would suggest to use moxios package instead of nock.

如果你正在使用axios,我建议使用moxios包而不是nock。

#1


0  

You are trying to test an async action.

您正在尝试测试异步操作。

So to answer this question, we can follow what the redux docs are suggesting.

所以为了回答这个问题,我们可以按照redux文档提出的建议。

  describe('async actions', () => {
    afterEach(() => {
      nock.cleanAll();
    });
    it('creates LOGIN_SUCCESS on successful login', () => {
      nock('http://example.com/')
        .get('/login')
        .reply(200, payload: { uid: 123, role: 'clinician' });

      const expectedActions = [
        { type: types.LOGIN_REQUEST },
        { type: types.LOGIN_SUCCESS, payload: { uid: 123, role: 'clinician' } },
      ];
      const store = mockStore({ auth: {} });

      return store.dispatch(actions.loginUser())
        .then(() => { // return of async actions
          expect(store.getActions()).toEqual(expectedActions);
        });
    });
  });

You may need to do some modifications to the above code to make it working.

您可能需要对上面的代码进行一些修改才能使其正常工作。

If you are using axios, I would suggest to use moxios package instead of nock.

如果你正在使用axios,我建议使用moxios包而不是nock。