【发布时间】:2022-01-04 18:43:54
【问题描述】:
有没有人能够在测试之间清除 Redux?你做了什么?
我有一个使用 Redux 的 React Native Expo 应用程序。
我有一个单独运行的测试,但是当我运行所有测试时,它失败了,因为其他测试在 redux 中留下了状态更改。
如何在测试之间“刷新”redux 存储。
我尝试了 mock-redux-store 包,但是当我将 combineReducers() 传递给它时,它什么也没返回。
我可以在那里手动重建状态切片,并将它们传递给模拟存储,但这很难维护。
testing-library-utils.js:
//This file adds a Redux Provider to a component's context when running Jest tests from test files.
//This first import enables jest native, which enables some state-centric matchers
import '@testing-library/jest-native/extend-expect';
import React from 'react';
import { render } from '@testing-library/react-native';
import { Provider, combineReducers, applyMiddleware, compose } from 'redux';
import ReduxThunk from 'redux-thunk';
import { createMockStore } from 'redux-test-utils';
// import { store } from '../App';
import logBooksReducer from '../store/reducers/logBooks-reducer';
import authReducer from '../store/reducers/auth-reducer';
const composedMiddleWare = compose(applyMiddleware(ReduxThunk));
const rootReducer = combineReducers({
logBooks: logBooksReducer,
auth: authReducer,
});
const store = createMockStore(rootReducer, composedMiddleWare);
console.log('STORE ', JSON.stringify(store.getState()));
const AllTheProviders = ({ children }) => {
console.log('Store.getState() from UTILS ', store.getState());
return <Provider store={store}>{children}</Provider>;
};
const customRender = (ui, options) =>
render(ui, { wrapper: AllTheProviders, ...options });
// re-export everything
export * from '@testing-library/react-native';
// override render method
export { customRender as render };
【问题讨论】:
标签: javascript reactjs react-native redux expo