0
votes

i am new in ngrx, we have angular proj which is usig ngrx storing/state the data. and now i have to write a test case for the all action, reducer and other code. but i am stuck in writing test cases for the actions. looked in google but no help found.

export enum ProjAction {
   INSERT_RECORD = "[ProjAction] Insert a record."
}

export class ProjActionCreator(private store: Store<ProjState>) {
   insertRecord(data: any) {
        this.store.dispatch(<Action>{
           type: ProjAction.INSERT_RECORD,
           payload: data
        })
   }

}

i tried to make a spy object and dispatch but it won't help for me. any help will appreciated.

1
ngrx.io/guide/store/testing => You can test components using mock or actual store implementation. ultimatecourses.com/blog/ngrx-store-testing-actions => Testing actions ultimatecourses.com/blog/ngrx-store-testing-reducers => Testing reducersAliF50
@AliF50 thank you for sharing the link. i visited the link earlier also but in my case it won't work. i have written the test case for the rest of others component link ng comp, service, store and others. just stuck in writing test case for Action as mentioned above.Nik Varma

1 Answers

0
votes

For this you need simply to assert that this.store.dispatch has been called with desired arguments.

it('test', () => {
  // preparation
  const store = TestBed.get(Store);
  spyOn(store, 'dispatch');

  // action
  component.insertRecord({test: '123'});

  // assertion
  expect(store.dispatch).toHaveBeenCalledWith({
    type: ProjAction.INSERT_RECORD,
    payload: {
      test: '123',
    },
  });
});