Search code examples
reactjstestingreduxreducereact-testing-library

Test of reducer returns the results which are not equal to expected


I try to test my reducer with react testing library and I don't understand why I get results that are not equal to the expected. Please could you explain to me?

my reducer

export const myReducer = (state = initialState, { type, payload }) => {
    switch (type) {
        case 'GET_DATA':
            return [...payload];
        default:
            return state;
    }
};

my test

import { mockedState } from 'mocks';

import {
    myReducer,
    GET_DATA,
} from 'store';

const initialState = []

describe('reducer', () => {

  it('should get data', () => {
    const getAction = {
      type: GET_DATA,
      payload: mockedState.data,
    };
    expect(myReducer(initialState, getAction )).toEqual([...mockedState.data]);
  });
})

Solution

  • In your reducer, you are using a string "GET_DATA" in your switch case. But from the look of your test your action type is a variable GET_DATA. Try changing your reducer's first case to be

    case GET_DATA: //remove the quotes
    

    where GET_DATA is from 'store'.

    Actually, I just remembered case for a switch statement must be a constant. So just make sure your GET_DATA from store is actually equal to "GET_DATA".