reactjs - 使用 Jest 和 Enzyme 在 Redux 中测试存储文件
问题描述
我们如何使用 Jest 和 Enzyme 编写商店创建文件的测试?
import {applyMiddleware, createStore} from 'redux';
import thunk from 'redux-thunk';
import rootReducer from '../reducers';
const store = createStore(rootReducer, applyMiddleware(thunk))
export default store;
我试过这个,是否需要。
import store from "..";
describe("store", () => {
it("should create a store", () => {
expect(store).toEqual(
expect.objectContaining({
dispatch: expect.any(Function),
getState: expect.any(Function),
})
);
});
});
解决方案
import configureMockStore from 'redux-mock-store'
import thunk from 'redux-thunk'
import fetchMock from 'fetch-mock'
const middlewares = [thunk]
// mocking store
const mockStore = configureMockStore(middlewares)
describe('some async actions', () => {
afterEach(() => {
fetchMock.restore()
})
})
推荐阅读
- git - 如何安全地将 master 中的更改重新设置为分离的 HEAD
- java - JPanel 上的文本页面
- javascript - 迭代并显示现有表中的json数据 - jquery
- c# - 单个xml对象作为sql中的json数组
- scala - Spark UDF 找不到参数 num 的隐式值:Numeric [Nothing]
- python - 具有公钥身份验证的 JupyterHub
- php - 使用 array_unshift 后“试图获取非对象的属性”
- tensorflow - 为什么 DeepLabV3+ 生成的所有图像都只变成黑色?
- python - opencv python中每个通道的HSV,YCrCb和LAB颜色空间中的像素值范围是多少
- javascript - React.js 项目中的 D3.js 问题