カウンターレデューサーをjestでテストしようとしていますがTypeError: state.get is not a function
、ディスパッチすると取得し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>
エラーは、reducerに渡されたストアが0であるため、reducer関数内で実行しようとするためです0.get(..)
。レデューサーに渡される最初の引数は、最初のストアである必要があります。
it(`should handle ${INCREMENT}`, () => {
const initialState = fromJS({
value: 0,
});
expect(reducer(initialState, { type: INCREMENT })).toEqual(fromJS({
value: 1,
}));
});
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加