有一个测试:"state.get is not a function"
Jest test: "state.get is not a function"
我想开玩笑地测试 Counter reducer,但是在调度 INCREMENT
时我得到了 TypeError: state.get is not a function
。
这是我的代码...
// 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>
错误是因为传递给 reducer 的 store 是 0,所以在 reducer 函数内部尝试做 0.get(..)
。
传递给 reducer 的第一个 arg 应该是初始存储:
it(`should handle ${INCREMENT}`, () => {
const initialState = fromJS({
value: 0,
});
expect(reducer(initialState, { type: INCREMENT })).toEqual(fromJS({
value: 1,
}));
});
我想开玩笑地测试 Counter reducer,但是在调度 INCREMENT
时我得到了 TypeError: state.get is not a function
。
这是我的代码...
// 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>
错误是因为传递给 reducer 的 store 是 0,所以在 reducer 函数内部尝试做 0.get(..)
。
传递给 reducer 的第一个 arg 应该是初始存储:
it(`should handle ${INCREMENT}`, () => {
const initialState = fromJS({
value: 0,
});
expect(reducer(initialState, { type: INCREMENT })).toEqual(fromJS({
value: 1,
}));
});