我有这个减速器:
import { fromJS } from 'immutable';
import { DEFAULT_ACTION, SOURCES_LOADED, SOURCES_REQUEST } from './constants';
export const initialState = fromJS({
sources: null,
loading: false
});
function appReducer(state = initialState, action) {
switch (action.type) {
case SOURCES_REQUEST:
return state
.set('loading', true);
case SOURCES_LOADED:
return state
.set('sources', action.payload.sources)
.set('loading', false);
case DEFAULT_ACTION:
return state;
default:
return state;
}
}
export default appReducer;这个测试:
import { fromJS } from 'immutable';
import reducer from '../reducer';
import * as types from '../constants';
describe('application reducers', () => {
it('should return the initial state', () => {
expect(reducer(undefined, {})).toEqual(fromJS(
{
sources: null,
loading: false
}
));
});
it('should handle the sources request', () => {
expect(reducer({ loading: true }, {
type: types.SOURCES_REQUEST
})).toEqual(fromJS({ loading: true }));
});
});第二个测试失败了:
TypeError: state.set is not a function
11 | case SOURCES_REQUEST:
12 | return state
> 13 | .set('loading', true);
| ^
14 | case SOURCES_LOADED:
15 | return state
16 | .set('sources', action.payload.sources)我如何才能将测试添加到这些reducers中,因为这是redux传奇,而且我正在遵循这个https://redux.js.org/recipes/writing-tests,因为它是我找到的更接近我需求的文档。
发布于 2020-05-07 00:52:50
您的reducer期望它接收的state是一个immutable对象。
但是在您的第二个测试中,您向它传递了一个普通的javascript对象,该对象没有您试图调用的.set方法。
it('should handle the sources request', () => {
expect(reducer(fromJS({
loading: true
}), {
type: types.SOURCES_REQUEST
})).toEqual(fromJS({
loading: true
}));
});或者,在本例中,您可以向其传递undefined,而reducer将使用initialState
https://stackoverflow.com/questions/61640576
复制相似问题