我有许多想要测试的 React/Redux 操作和减速器。我已经成功编写了一些测试,但是我在减速器测试中遇到了一个问题,其中一个值undefined
在第一个测试中被保留,但在第二个测试中正确初始化。
文件Reducers.test.js
import reducer from './filesReducers';
import { filesActions } from '../actions/actionTypes';
import filesInitialState from '../../components/Files/initialState';
describe('files reducers', () => {
describe('media library reducers', () => {
it('should return the initial state', () => {
const testInitialState = reducer(undefined, {});
console.log({ testInitialState }); /* eslint-disable-line no-console */
expect(testInitialState.multiMediaMediaLibraryItems).toEqual([]);
// expect(testInitialState.logosMediaLibraryItems).toEqual([]); /* value is undefined */
expect(testInitialState.logosMLibraryItems).toEqual([]);
});
it('should update the multiMedia list', () => {
const updatedState = reducer(filesInitialState, { type : filesActions.STORE_MEDIA_LIBRARY_MULTIMEDIA_ITEMS, payload : ['1000002'] });
console.log(updatedState); /* eslint-disable-line no-console */
expect(updatedState.multiMediaMediaLibraryItems).toEqual(['1000002']);
});
});
});
initialState.js(这里大约有 20 个变量)
const filesInitialState = {
...
logosMediaLibraryItems : [],
logosMLibraryItems : [],
...
第一个控制台输出片段
logosMediaLibraryItems: undefined, <=== one variable only undefined
logosMLibraryItems: [], <=== this and all other variables OK
第二个控制台输出片段
logosMediaLibraryItems: [], <=== Now this is defined
logosMLibraryItems: [],
这到底是怎么回事?不久前我看到 reduxForm 有类似的问题,但没有这样的问题。