dimanche 29 septembre 2019

Access value inside finalize pipe from test

I have component:

isLoading: boolean;
users: User[];
test: number = 1;

ngOnInit() {
  this.isLoading = true;
  this.userService.getUsers()
      .pipe(
        finalize(() => {
          // Disable loading when complete
          this.isLoading = false;
          this.test = 5;
        })
      )
  .subscribe(results => {this.users = results})
}

And I have test for it (.spec.ts)

    it('should be 5 after ngOnInit', () => {
      component.ngOnInit();
      expect(component.test).toBe(5);
    });

I am getting an error for test 'Expected 1 to be 5.' Why there is this error, if I read value after ngOnInit, .finalize is ignored? How do I get value from .finalize block. fixture.detectChanges() does not help also.

Aucun commentaire:

Enregistrer un commentaire