mercredi 28 octobre 2020

Testing BehaviorSubject as Observable in Angular - Jasmine

I am very newbie with testing and I am having troubles to test the following code:

ngOnInit() {
    ... some piece of code
    this.dataService.currentUserDataObservable.pipe(
      takeUntil(this.ngUnsubscribe)
    ).subscribe(
      currentUserData => {
        this.currentUser = currentUserData;
      }
    );
}

The variable this.currentUser will be used to show a button based on its properties. The variable currentUserDataObservable is a BehaviorSubject.asObservable(). When the user do the login the currentUserDataSubject inside dataService will call its next function to send the currentUserData.

I want to write a test to check if the button is being showed when the properties are true (something like this.currentUser.showButton == true) and another test to check otherwise.

What is the proper way to do that. I read some articles talking about spyOn, fakeAsync, but I could not reproduce for this specific case.

Thank you

Aucun commentaire:

Enregistrer un commentaire