簡體   English   中英

'TypeError: store.dispatch is not a function' redux action testing with jest

[英]'TypeError: store.dispatch is not a function' redux action testing with jest

我正在嘗試為此 redux 操作編寫測試:

export const clearError = () => (dispatch: Dispatch<IStoreState>) => dispatch({ type: actionTypes.CLEAR_ACTIVATION_ERRORS });

不明白為什么會發生這種情況,因為我一步一步地從示例中完成了所有操作。 這是我的測試:

import configureMockStore from 'redux-mock-store';
import * as actionTypes from '@const/actions';

import './__mocks__/reactNativeConfig';

import * as actions from '../index';

const middlewares = [thunk];
const mockStore = configureMockStore(middlewares);

describe('index.ts -> clearError', async () => {
  it('clearError', () => {
    const expectedAction = {
      type: actionTypes.CLEAR_ACTIVATION_ERRORS,
    };

    const store = mockStore;

    return store.dispatch(actions.clearError)
      .then(() => {
        expect(store.getActions()).toEqual(expectedAction);
      }
    );
  });
});

當我運行我的測試時,我有一個錯誤:


      18 |     const store = mockStore;
      19 | 
    > 20 |     return store.dispatch(actions.clearError)
         |                  ^
      21 |       .then(() => {
      22 |         expect(store.getActions()).toEqual(expectedAction);
      23 |       }

請你幫我弄清楚我做錯了什么。 謝謝!

嘗試提供模擬商店數據,如下例所示

import configureMockStore from 'redux-mock-store';
import * as actionTypes from '@const/actions';

import './__mocks__/reactNativeConfig';

import * as actions from '../index';

const middlewares = [thunk];
const mockStore = configureMockStore(middlewares);

describe('index.ts -> clearError', async () => {
  it('clearError', () => {
    const expectedAction = {
      type: actionTypes.CLEAR_ACTIVATION_ERRORS,
    };

    const store = mockStore("mock store data should be given here({loading: false})");

    return store.dispatch(actions.clearError)
      .then(() => {
        expect(store.getActions()).toEqual(expectedAction);
      }
    );
  });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM