mardi 16 juillet 2019

How to test functions inside rxjs chain

I am trying to test callback happens after closing of mat-dialog. My code presented as a rxjs chain and I need to test some functions inside this chain

This is Angular 7 with Angular Material

Code to test


deleteProject() {
    this.dialog.open(DeleteProjectDialogComponent, {
      data: {
        project: this.project
      },
      minWidth: MIN_DIALOG_WIDTH
    })
      .afterClosed()
      .pipe(
        take(1),
        filter(Boolean),
        switchMap(() => this.apiProjects.deleteProject(this.project.id)), // <= need to test this call of this function
        withLatestFrom(this.translate.get('PROJECT.WAS_DELETED')),
        tap({
          next: ([res, translation]) => {
            this.utils.storage.clearProject();
            this.utils.navigation.goToAuthPage();
            this.snackBar.open(translation, 'OK', {
              duration: SNACK_BAR_DURATION
            });
          },
          error: res => {
            const {error} = res;
            if (res.status === 400) {
              const {name, project} = error;
              if (project) {
                this.showError(error.project);
              }
            }
          }
        })
      ).subscribe();
  }


my test


it('should call api after delete project', () => {

    const spyApi = spyOn(apiProject, 'deleteProject');
    spyOn(dialog, 'open').and.returnValue({
      afterClosed: () => of(true)
    });

    component.deleteProject();

    expect(spyApi).toHaveBeenCalled(); // <= failed with Error: Expected spy deleteProject to have been called.
  });


Aucun commentaire:

Enregistrer un commentaire