Answer the question
In order to leave comments, you need to log in
What is the correct way to test Reducer in Jest?
I'm trying to put into practice Jest tests that are built into create react app
reducer
function reducer(state = initialState, action) {
switch (action.type) {
// унивирсальный onChange
case patientActions.ON_CHANGE_PATIENT:
return {
...state,
[action.payload.field]: action.payload.value,
};
// если нашли, то открываем модал с информацией
case patientActions.ON_SEARCH_PATIENT_FOUND:
return {
...state,
showModal: true,
modalBodyContainer: <PatientDetails {...action.payload} />,
};
...
// Universal onChange
export const onChangePatient = createAction(patientActions.ON_CHANGE_PATIENT, (value, field) => ({
value,
field,
}));
// Search Patient
export const onSearchPatient = (ur, token) => dispatch => (
callToApiWithToken(`patient/by/ur/${ur}`, token)
.then(response =>
(response.data === null) ? dispatch(onSearchPatientNotFound(ur)) : dispatch(onSearchPatientFound(response.data))
)
.catch((error) => ({type: patientActions.ON_SUBMIT_PATIENT_ERROR}) )
);
export const onSearchPatientFound = createAction(patientActions.ON_SEARCH_PATIENT_FOUND);
export const onSearchPatientNotFound = createAction(patientActions.ON_SEARCH_PATIENT_NOT_FOUND);
describe('Request reducer', ()=>{
it('has a default states', ()=>{
expect(reducer(undefined, {type: 'unexpected'})).toEqual(defaultStates)
}); // Проходит этот тест
it(`handle action ${patientActions.ON_SEARCH_PATIENT_FOUND}`, ()=>{
expect(reducer(undefined,
{
type: patientActions.ON_SEARCH_PATIENT_FOUND,
payload: {
showModal: true,
modalBodyContainer: {} // Нужно ли сюда писать <PatientDetails {...action.payload} />?
}
})
)
.toEqual({...defaultStates,
showModal: true,
modalBodyContainer: ???
})
}) // не проходит
it(`handle action ${patientActions.ON_CHANGE_PATIENT}`, ()=>{
expect(reducer(undefined, {
type: patientActions.PATIENT_INIT_STATE,
payload: {
value: '11122',
field: 'patient_ur'
}
})).toEqual({...defaultStates, patient_ur: '11122'})
}) // не проходит
Answer the question
In order to leave comments, you need to log in
redux.js.org/docs/recipes/WritingTests.html
There are some decent examples here. should help you
it(`handle action ${patientActions.ON_CHANGE_PATIENT}`, ()=>{
expect(reducer(undefined, {
type: patientActions.PATIENT_INIT_STATE, // тут должно быть patientActions.ON_CHANGE_PATIENT
payload: {
value: '11122',
field: 'patient_ur'
}
})).toEqual({...defaultStates, patient_ur: '11122'})
})
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question