【发布时间】:2021-03-03 19:52:25
【问题描述】:
我有一个像下面这样的动作
index.js
export const dataCount = () => {
return async (dispatch) => {
getDataCount().then((data) => {
dispatch({ type: 'FETCH_DATA_COUNT', payload: data});
});
};
};
这里的 getDataCount 是一个辅助函数,它返回一个带有从数据库中获取的数据的承诺。
我的测试暂时是这样的。
import configureStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import dataCount from '../index';
import getDataCount from '../helper';
const middlewares = [thunk];
const mockStore = configureStore(middlewares);
const store = mockStore();
let dataValue = '';
describe('fetchData', () => {
beforeEach(() => { // Runs before each test in the suite
dataValue = jest.fn(getDataCount);
store.clearActions();
});
it('has the correct action and payload for dataCount', async () => {
dataValue .mockReturnValue({ total : 13 });
const expectedActions = [
{
payload: dataValue(),
type: 'FETCH_DATA_COUNT'
}
];
await getDataCount().then((res) => {
store.dispatch(dataCount());
expect(store.getActions()).toEqual(expectedActions);
});
});
});
测试失败,因为 store.getActions() 返回 [] 并且我知道我在调度时做错了什么。任何建议都会有很大帮助。
【问题讨论】:
标签: reactjs unit-testing redux jestjs enzyme