我正在尝试对react.js中的reducer进行简单的单元测试。 我将undefined作为初始状态传递,将空操作作为reducer测试中的第二个参数传递。我期望测试通过,但我得到一个错误,如下所示。这抱怨我将什么传递给减速器。任何人都可以告诉我如何克服这个问题。感谢
TypeError: (0 , _Global2.default) is not a function
at Object.it (src/__tests__/Global.test.js:5:33)
at new Promise (<anonymous>)
at Promise.resolve.then.el (node_modules/p-map/index.js:46:16)
使用减速器继承我的全局状态
const loginVisibility = Object.freeze({
Visible: 'VISIBLE',
Hidden: 'HIDDEN'
});
const initialState = { show: false };
export const actionCreators = {
hideLogin: () => ({
type: loginVisibility.Hidden,
}),
showLogin: () => ({
type: loginVisibility.Visible,
}),
};
export const reducer = (state, action) => {
state = state || initialState;
if (action.type === loginVisibility.Visible) {
return { ...state, show: true };
}
if (action.type === loginVisibility.Hidden) {
return { ...state, show: false };
}
return state;
};
这是我的Global.test.js文件
import reducer from '../store/state/Global';
describe('Login Visibility',()=>{
it('Should return the initial state',()=>{
expect(reducer(undefined,{})).toEqual({
show: false
})
})
});
答案 0 :(得分:2)
这是你应该如何导入你的减速器:
import { reducer } from '../store/state/Global';
因为在您的reducer中,您使用命名导出