Snorlax
Snorlax

Reputation: 4765

Angular 2, ngrx, testing my Reducer

In my Reducer file:

 case PumpActionTypes.EnterLocalMode:
  return commandOne.upsertOne(
    {id: action.payload.id, changes: { local: false }},
    state
  );

In my test, I want to test to see that local is changed to false. What I wrote:

  describe('EnterLocalMode action', () => {
    it('should handle Enter Local Mode from Remote Mode', () => {


    const {initialState} = MyReducer;

    const action = new MyActions.EnterLocalMode();

    const state = MyReducer.reducer(initialState, action);

    //Test that the changes in pump adaptor change to "local" = true
    expect(MyReducer.commandOne.upsertOne(changes:{local})).toEqual(true);

      });



});

However, I'm not sure how to write my expect statement, which is right now incorrect:

expect(MyReducer.commandOne.upsertOne(changes:{local})).toEqual(true);

Upvotes: 0

Views: 304

Answers (1)

amitdigga
amitdigga

Reputation: 7168

Reducer is a pure function which returns a new state based on previousState and action. So you should test that only.

expect(PumpReducer.reducer({counter:1}, Increment).counter).toBe(2)

suppose for state = {counter:0}, action Increment changes state to {counter:1}

You should test all actions like that and also any conditional branching in each action.

Edit

case PumpActionTypes.EnterLocalMode:
  return pumpAdapter.upsertOne(
    {id: action.payload.id, changes: { local: false }},
    state
  ); 

So you are returning state based on function call. And then

expect(PumpReducer.pumpAdapter.upsertOne(changes:{local})).toEqual(true);

it looks like you are testing if upsertOne() was called with changes:{local} or like that. You can use jasmine spyon method to check and test arguments of a function call.

Upvotes: 1

Related Questions