如何解决某些值在第一次测试中未初始化
我有很多要测试的React / Redux动作和reducer。我已经成功编写了一些测试,但是在化简测试中遇到了一个问题,在第一个测试中,一个值保留为undefined
,但在第二个测试中已正确初始化。
filesReducers.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存在类似的问题,但是没有这样的问题。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。