ReactJS/Redux: Testing reducer state with array of objects inside receives undefined array












0















I am working for reducer test, when i try to test 1 of my actions types the expected and received values does not match.



eventReducer.test.js



import reducer from './eventReducer';
import { GET_EVENTS } from '../actions/types';

describe('event reducer', () => {
it('should return initial state', () => {
expect(reducer(undefined, {})).toEqual({
eventDetails: ,
loading: false
});
})

it('should store data from db upon mounting component', () =>{
expect(reducer({}, {
type: GET_EVENTS,
eventDetails: [
{
firstName: 'Damian',
lastName: 'Wasilewski'
}
]
})
).toEqual({
eventDetails: [
{
firstName: 'Damian',
lastName: 'Wasilewski'
}
],
loading: false
})
});
})


eventReducer.js



import { GET_EVENTS, ADD_EVENT, DELETE_EVENT, EVENTS_LOADING } from '../actions/types';

const initialState = {
eventDetails: ,
loading: false
}

export default function (state = initialState, action) {
switch(action.type) {
case GET_EVENTS:
return {
...state,
eventDetails: action.payload,
loading: false
};
case DELETE_EVENT:
return {
...state,
eventDetails: state.eventDetails.filter(event => event._id !==
action.payload)
};
case ADD_EVENT:
return {
...state,
eventDetails: [action.payload, ...state.eventDetails]
};
case EVENTS_LOADING:
return {
...state,
loading: true
}
default:
return state;
}
}


My first test is running properly, but when i try to execute second one, it doesn't match received and expected values, problem is with eventDetails array, it keeps outputting that it's value is undefined.



Test output



FAIL src/reducers/eventReducer.test.js
● event reducer › should store data from db upon mounting component

expect(received).toEqual(expected)

Expected value to equal:
{"eventDetails": [{"firstName": "Damian", "lastName": "Wasilewski"}], "loading": false}
Received:
{"eventDetails": undefined, "loading": false}

Difference:

- Expected
+ Received

Object {
- "eventDetails": Array [
- Object {
- "firstName": "Damian",
- "lastName": "Wasilewski",
- },
- ],
+ "eventDetails": undefined,
"loading": false,
}

21 | ]
22 | })
> 23 | ).toEqual({
| ^
24 | eventDetails: [
25 | {
26 | firstName: 'Damian',

at Object.toEqual (src/reducers/eventReducer.test.js:23:5)


What could cause problem of undefined array?










share|improve this question





























    0















    I am working for reducer test, when i try to test 1 of my actions types the expected and received values does not match.



    eventReducer.test.js



    import reducer from './eventReducer';
    import { GET_EVENTS } from '../actions/types';

    describe('event reducer', () => {
    it('should return initial state', () => {
    expect(reducer(undefined, {})).toEqual({
    eventDetails: ,
    loading: false
    });
    })

    it('should store data from db upon mounting component', () =>{
    expect(reducer({}, {
    type: GET_EVENTS,
    eventDetails: [
    {
    firstName: 'Damian',
    lastName: 'Wasilewski'
    }
    ]
    })
    ).toEqual({
    eventDetails: [
    {
    firstName: 'Damian',
    lastName: 'Wasilewski'
    }
    ],
    loading: false
    })
    });
    })


    eventReducer.js



    import { GET_EVENTS, ADD_EVENT, DELETE_EVENT, EVENTS_LOADING } from '../actions/types';

    const initialState = {
    eventDetails: ,
    loading: false
    }

    export default function (state = initialState, action) {
    switch(action.type) {
    case GET_EVENTS:
    return {
    ...state,
    eventDetails: action.payload,
    loading: false
    };
    case DELETE_EVENT:
    return {
    ...state,
    eventDetails: state.eventDetails.filter(event => event._id !==
    action.payload)
    };
    case ADD_EVENT:
    return {
    ...state,
    eventDetails: [action.payload, ...state.eventDetails]
    };
    case EVENTS_LOADING:
    return {
    ...state,
    loading: true
    }
    default:
    return state;
    }
    }


    My first test is running properly, but when i try to execute second one, it doesn't match received and expected values, problem is with eventDetails array, it keeps outputting that it's value is undefined.



    Test output



    FAIL src/reducers/eventReducer.test.js
    ● event reducer › should store data from db upon mounting component

    expect(received).toEqual(expected)

    Expected value to equal:
    {"eventDetails": [{"firstName": "Damian", "lastName": "Wasilewski"}], "loading": false}
    Received:
    {"eventDetails": undefined, "loading": false}

    Difference:

    - Expected
    + Received

    Object {
    - "eventDetails": Array [
    - Object {
    - "firstName": "Damian",
    - "lastName": "Wasilewski",
    - },
    - ],
    + "eventDetails": undefined,
    "loading": false,
    }

    21 | ]
    22 | })
    > 23 | ).toEqual({
    | ^
    24 | eventDetails: [
    25 | {
    26 | firstName: 'Damian',

    at Object.toEqual (src/reducers/eventReducer.test.js:23:5)


    What could cause problem of undefined array?










    share|improve this question



























      0












      0








      0








      I am working for reducer test, when i try to test 1 of my actions types the expected and received values does not match.



      eventReducer.test.js



      import reducer from './eventReducer';
      import { GET_EVENTS } from '../actions/types';

      describe('event reducer', () => {
      it('should return initial state', () => {
      expect(reducer(undefined, {})).toEqual({
      eventDetails: ,
      loading: false
      });
      })

      it('should store data from db upon mounting component', () =>{
      expect(reducer({}, {
      type: GET_EVENTS,
      eventDetails: [
      {
      firstName: 'Damian',
      lastName: 'Wasilewski'
      }
      ]
      })
      ).toEqual({
      eventDetails: [
      {
      firstName: 'Damian',
      lastName: 'Wasilewski'
      }
      ],
      loading: false
      })
      });
      })


      eventReducer.js



      import { GET_EVENTS, ADD_EVENT, DELETE_EVENT, EVENTS_LOADING } from '../actions/types';

      const initialState = {
      eventDetails: ,
      loading: false
      }

      export default function (state = initialState, action) {
      switch(action.type) {
      case GET_EVENTS:
      return {
      ...state,
      eventDetails: action.payload,
      loading: false
      };
      case DELETE_EVENT:
      return {
      ...state,
      eventDetails: state.eventDetails.filter(event => event._id !==
      action.payload)
      };
      case ADD_EVENT:
      return {
      ...state,
      eventDetails: [action.payload, ...state.eventDetails]
      };
      case EVENTS_LOADING:
      return {
      ...state,
      loading: true
      }
      default:
      return state;
      }
      }


      My first test is running properly, but when i try to execute second one, it doesn't match received and expected values, problem is with eventDetails array, it keeps outputting that it's value is undefined.



      Test output



      FAIL src/reducers/eventReducer.test.js
      ● event reducer › should store data from db upon mounting component

      expect(received).toEqual(expected)

      Expected value to equal:
      {"eventDetails": [{"firstName": "Damian", "lastName": "Wasilewski"}], "loading": false}
      Received:
      {"eventDetails": undefined, "loading": false}

      Difference:

      - Expected
      + Received

      Object {
      - "eventDetails": Array [
      - Object {
      - "firstName": "Damian",
      - "lastName": "Wasilewski",
      - },
      - ],
      + "eventDetails": undefined,
      "loading": false,
      }

      21 | ]
      22 | })
      > 23 | ).toEqual({
      | ^
      24 | eventDetails: [
      25 | {
      26 | firstName: 'Damian',

      at Object.toEqual (src/reducers/eventReducer.test.js:23:5)


      What could cause problem of undefined array?










      share|improve this question
















      I am working for reducer test, when i try to test 1 of my actions types the expected and received values does not match.



      eventReducer.test.js



      import reducer from './eventReducer';
      import { GET_EVENTS } from '../actions/types';

      describe('event reducer', () => {
      it('should return initial state', () => {
      expect(reducer(undefined, {})).toEqual({
      eventDetails: ,
      loading: false
      });
      })

      it('should store data from db upon mounting component', () =>{
      expect(reducer({}, {
      type: GET_EVENTS,
      eventDetails: [
      {
      firstName: 'Damian',
      lastName: 'Wasilewski'
      }
      ]
      })
      ).toEqual({
      eventDetails: [
      {
      firstName: 'Damian',
      lastName: 'Wasilewski'
      }
      ],
      loading: false
      })
      });
      })


      eventReducer.js



      import { GET_EVENTS, ADD_EVENT, DELETE_EVENT, EVENTS_LOADING } from '../actions/types';

      const initialState = {
      eventDetails: ,
      loading: false
      }

      export default function (state = initialState, action) {
      switch(action.type) {
      case GET_EVENTS:
      return {
      ...state,
      eventDetails: action.payload,
      loading: false
      };
      case DELETE_EVENT:
      return {
      ...state,
      eventDetails: state.eventDetails.filter(event => event._id !==
      action.payload)
      };
      case ADD_EVENT:
      return {
      ...state,
      eventDetails: [action.payload, ...state.eventDetails]
      };
      case EVENTS_LOADING:
      return {
      ...state,
      loading: true
      }
      default:
      return state;
      }
      }


      My first test is running properly, but when i try to execute second one, it doesn't match received and expected values, problem is with eventDetails array, it keeps outputting that it's value is undefined.



      Test output



      FAIL src/reducers/eventReducer.test.js
      ● event reducer › should store data from db upon mounting component

      expect(received).toEqual(expected)

      Expected value to equal:
      {"eventDetails": [{"firstName": "Damian", "lastName": "Wasilewski"}], "loading": false}
      Received:
      {"eventDetails": undefined, "loading": false}

      Difference:

      - Expected
      + Received

      Object {
      - "eventDetails": Array [
      - Object {
      - "firstName": "Damian",
      - "lastName": "Wasilewski",
      - },
      - ],
      + "eventDetails": undefined,
      "loading": false,
      }

      21 | ]
      22 | })
      > 23 | ).toEqual({
      | ^
      24 | eventDetails: [
      25 | {
      26 | firstName: 'Damian',

      at Object.toEqual (src/reducers/eventReducer.test.js:23:5)


      What could cause problem of undefined array?







      reactjs redux jestjs






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 '18 at 4:13









      iagowp

      1,40411325




      1,40411325










      asked Nov 21 '18 at 20:51









      D.WasilewskiD.Wasilewski

      226




      226
























          1 Answer
          1






          active

          oldest

          votes


















          0














          You are setting event.details to action.payload, but on your test you are not sending a payload property.



          it('should store data from db upon mounting component', () =>{
          expect(reducer({}, {
          type: GET_EVENTS,
          payload: [
          {
          firstName: 'Damian',
          lastName: 'Wasilewski'
          }
          ]
          })
          ).toEqual({
          eventDetails: [
          {
          firstName: 'Damian',
          lastName: 'Wasilewski'
          }
          ],
          loading: false
          })
          });
          })


          This should solve your problem






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53420294%2freactjs-redux-testing-reducer-state-with-array-of-objects-inside-receives-undef%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            You are setting event.details to action.payload, but on your test you are not sending a payload property.



            it('should store data from db upon mounting component', () =>{
            expect(reducer({}, {
            type: GET_EVENTS,
            payload: [
            {
            firstName: 'Damian',
            lastName: 'Wasilewski'
            }
            ]
            })
            ).toEqual({
            eventDetails: [
            {
            firstName: 'Damian',
            lastName: 'Wasilewski'
            }
            ],
            loading: false
            })
            });
            })


            This should solve your problem






            share|improve this answer




























              0














              You are setting event.details to action.payload, but on your test you are not sending a payload property.



              it('should store data from db upon mounting component', () =>{
              expect(reducer({}, {
              type: GET_EVENTS,
              payload: [
              {
              firstName: 'Damian',
              lastName: 'Wasilewski'
              }
              ]
              })
              ).toEqual({
              eventDetails: [
              {
              firstName: 'Damian',
              lastName: 'Wasilewski'
              }
              ],
              loading: false
              })
              });
              })


              This should solve your problem






              share|improve this answer


























                0












                0








                0







                You are setting event.details to action.payload, but on your test you are not sending a payload property.



                it('should store data from db upon mounting component', () =>{
                expect(reducer({}, {
                type: GET_EVENTS,
                payload: [
                {
                firstName: 'Damian',
                lastName: 'Wasilewski'
                }
                ]
                })
                ).toEqual({
                eventDetails: [
                {
                firstName: 'Damian',
                lastName: 'Wasilewski'
                }
                ],
                loading: false
                })
                });
                })


                This should solve your problem






                share|improve this answer













                You are setting event.details to action.payload, but on your test you are not sending a payload property.



                it('should store data from db upon mounting component', () =>{
                expect(reducer({}, {
                type: GET_EVENTS,
                payload: [
                {
                firstName: 'Damian',
                lastName: 'Wasilewski'
                }
                ]
                })
                ).toEqual({
                eventDetails: [
                {
                firstName: 'Damian',
                lastName: 'Wasilewski'
                }
                ],
                loading: false
                })
                });
                })


                This should solve your problem







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 22 '18 at 3:15









                iagowpiagowp

                1,40411325




                1,40411325
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53420294%2freactjs-redux-testing-reducer-state-with-array-of-objects-inside-receives-undef%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    How to change which sound is reproduced for terminal bell?

                    Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

                    Can I use Tabulator js library in my java Spring + Thymeleaf project?