开玩笑无法在mapStateToProps中进行分解

时间:2020-08-20 11:22:23

标签: reactjs unit-testing react-redux jestjs enzyme

我的组件正在运行,并且达到了预期的结果。但是在进行测试时,由于解构失败而失败。

错误:无法解构“ state.employeeLeave”的属性“ leaveTypes”,因为该属性未定义

function mapStateToProps(state) {
const { leaveTypes } = state.employeeLeave; // works well in browser but fails in unit test
return {
    leaveTypes
}
}
//initial state
export const leaveInitialState = {
leaveTypes: []
};

//reducer
import { actionConstants } from '../../constants/actionConstants';
import { leaveInitialState } from '../config/initialState';
export default function employeeLeaveReducer(state = leaveInitialState, action) {
switch (action.type) {
case actionConstants.LEAVE_TYPES:
return { ...state, leaveTypes: action.payload.response };
default:
return state;
}
}
//combined reducer
import { combineReducers } from 'redux';
import login from './login';
import employeeLeave from './employeeLeave';

export default combineReducers({
login,
employeeLeave
});
//my test file
test('Leaveform', () => {
const component = renderer.create(
    <MemoryRouter
        initialEntries={[{ pathname: '/', key: 'testKey', state: {} }]}
    >
        <Provider store={store}>
            <LeaveForm />
        </Provider>
    </MemoryRouter>);
let tree = component.toJSON();
expect(tree).toMatchSnapshot();
});

0 个答案:

没有答案