Jestjs 如何使用jest测试减速器
我有这个减速机:Jestjs 如何使用jest测试减速器,jestjs,redux-saga,Jestjs,Redux Saga,我有这个减速机: 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)
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)
我如何才能将测试添加到这些减缩器中,因为这是redux sagas,我之所以关注它,是因为我找到了更符合我需要的文档。您的减缩器期望它接收的
状态是一个不可变的对象
但是在第二次测试中,您通过了一个普通javascript对象,它没有您尝试调用的.set
方法
it('应该处理源请求',()=>{
expect(减速器)(fromJS({
加载:正确
}), {
类型:types.SOURCES\u请求
})).toEqual(fromJS)({
加载:正确
}));
});
或者在这种特殊情况下,您可以传递它undefined
,减速器将使用初始状态