0

I have a reducer with a default initial state defined like this:

const webReducer = (state = {
    fetched: false,
    data: {}
}, action) => {
    switch (action.type) {
        case "FETCH_DATA":
            state = {
                ...state,
                fetched: action.payload.fetched
            };

            state.data.push( action.payload.data );

            return state;
        break;
    }

    return state;
};

When I run a test to check for the initial state, it pass:

const initialState = {
    fetched: false,
    data: {}
};

it("Should have a default initial state", () => {
    expect( webReducer( undefined, {}) ).toEqual( initialState );
})

However, when I try to test adding data to the state, it return this error:

TypeError: state.data.push is not a function

This is how I'm doing the test:

const object = {
    fetched: true,
    data: {
        config: {size: 10}
    }
};

expect(
    webReducer( undefined, {
        type: "FETCH_DATA", 
        payload: object
    })
).toEqual( object );

Why am i getting this error now, if before i could pass the test just fine? I also tried to pass a default state, like this:

expect(
    webReducer( initialState, {
        type: "FETCH_DATA", 
        payload: object
    })
).toEqual( object );

But I'm getting the same error.

1 Answer 1

1

You have initialised data as an empty object, which doesn't have a push function on it. Try replacing it with the below, and the test should pass alright.

state = {
fetched: false,
data: []
Sign up to request clarification or add additional context in comments.

1 Comment

Had to made some adjustments to test with multiple dispatchs but I got the point! Thanks =D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.