NGXS: How to test if an action was dispatched?

前端 未结 3 2186
遇见更好的自我
遇见更好的自我 2021-02-14 06:57

How to unit test whether an action was dispatched?

For example, in a LogoutService, I have this simple method:

  logout(username: string) {
    store.dis         


        
3条回答
  •  花落未央
    2021-02-14 07:19

    I tried this approach to test if both actions were called:

    3. Test if actions are being called

    // ...
    it('should call actions ResetStateAction and LogoutAction', async( () => {
      let actionDispatched = false;
      zip(
        actions$.pipe(ofActionDispatched(ResetStateAction)),
        actions$.pipe(ofActionDispatched(LogoutAction))
      )
      .subscribe( () => actionDispatched = true );
    
      store.dispatch([new ResetStateAction(), new LogoutAction()])
        .subscribe(
          () => expect(actionDispatched).toBe(true)
        );
    }));
    // ...
    

提交回复
热议问题