N
N
Ninja Mate2017-09-10 20:46:40
JavaScript
Ninja Mate, 2017-09-10 20:46:40

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} />,
          };
...

action
// 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);

test
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'})
    }) // не проходит

The task so far is to test the reducers. How to make it all work?
PS The code all works.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman Alexandrovich, 2017-09-10
@RomReed

redux.js.org/docs/recipes/WritingTests.html
There are some decent examples here. should help you

M
Maxim, 2017-09-10
@maxfarseer

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'})
    })

correct the error in this test, if it "passes", then I think everything will work out further. The essence of unit tests is that you write: it was A, applied some function (in your case, a reducer), it became B. Compare with C.
And you describe "C" yourself.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question