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]);
});
})
CodePudding user response:
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".
CodePudding user response:
The question seems to revolve around when an iterable a
is not 'shallow' equal to [...a]
. If a
is an array that should always be the case but for any other iterable it's not.
I suspect mockedState.data
is not an array in the failing test.
For example:
const a = 'hello';
const b = [...a]; // ["h","e","l","l","o"]
In this case a does not equal b.