0

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

Rafael Andrade
  • 495
  • 1
  • 5
  • 22
  • https://dev.to/mustapha/angular-unit-testing-101-with-examples-6mc I think that's a good read and Dealing with HttpRequests there should help you. You will have to mock `dataService` similar to how RealApiService is mocked there. – AliF50 Oct 29 '20 at 03:02
  • dataService does not make an HttpRequest, it is just to that when the user makes some change on his profile, all system gets this change as well via currentUserDataObservable – Rafael Andrade Oct 29 '20 at 11:19
  • Yes, so you can mock it using `of`https://www.learnrxjs.io/learn-rxjs/operators/creation/of. Although it is a bit old, check Paul Samsotha's answer here.https://stackoverflow.com/a/42360338/7365461 – AliF50 Oct 29 '20 at 12:39

0 Answers0