Redux 开玩笑的测试;state.get不是一个函数;
我试图用jest测试计数器减速机,但我得到Redux 开玩笑的测试;state.get不是一个函数;,redux,jestjs,immutable.js,Redux,Jestjs,Immutable.js,我试图用jest测试计数器减速机,但我得到TypeError:state.get在调度INCREMENT时不是一个函数。 这是我的密码 // module.js import { fromJS } from 'immutable'; ... const initialState = fromJS({ value: 0, }); export default function reducer(state = initialState, action) { switch (action.
TypeError:state.get在调度INCREMENT
时不是一个函数。
这是我的密码
// module.js
import { fromJS } from 'immutable';
...
const initialState = fromJS({
value: 0,
});
export default function reducer(state = initialState, action) {
switch (action.type) {
case INCREMENT:
return state.set('value', state.get('value') + 1);
case DECREMENT:
return state.set('value', state.get('value') - 1);
case INCREMENT_IF_ODD:
return (state % 2 !== 0) ? state.set('value', state.get('value') + 1) : state;
default:
return state;
}
}
// module.test.js
import { fromJS } from 'immutable';
import reducer, { types } from './module';
const { INCREMENT, DECREMENT, INCREMENT_IF_ODD } = types;
describe('Counter reducer', () => {
it('should return the initial state', () => {
expect(reducer(undefined, {})).toEqual(fromJS({
value: 0,
}));
});
it(`should handle ${INCREMENT}`, () => {
expect(reducer(0, { type: INCREMENT })).toEqual(fromJS({
value: 1,
}));
});
...
});
我不明白我的代码有什么问题,因为它在浏览器上运行得很好
这里是错误
FAIL src/containers/Counter/module.test.js
● Counter reducer › should handle Counter/INCREMENT
TypeError: state.get is not a function
at reducer (src/containers/Counter/module.js:34:50)
at Object.<anonymous> (src/containers/Counter/module.test.js:25:33)
at new Promise (<anonymous>)
at <anonymous>
src/containers/Counter/module.test.js失败
● 计数器减速机›应处理计数器/增量
TypeError:state.get不是函数
at减速器(src/containers/Counter/module.js:34:50)
反对。(src/containers/Counter/module.test.js:25:33)
在新的承诺()
在
错误是因为传递给reducer的存储是0,所以reducer函数内部尝试执行0。获取(…)
。
传递给reducer的第一个参数应该是初始存储:
it(`should handle ${INCREMENT}`, () => {
const initialState = fromJS({
value: 0,
});
expect(reducer(initialState, { type: INCREMENT })).toEqual(fromJS({
value: 1,
}));
});