0

So I'm still very new to rxjs.

Currently using tape js for unit tests in my code base. I've looked into marble diagram testing, but it's really not clicking for me.

My question : How to unit test a search call that makes use of rxjs observable? I'm interested in asserting that the _handleSearchResults function is called with the observable completes. Similarly how to test if the observable fails, that the _handleSearchError is called.

this.searchSubject is a rx subject

searchChanged(query) {
    this.searchSubject.next(query);
}

Search subject is defined with the switchmap which is really what I want to unit test when calling searchChanged.

 this.searchSubject.switchMap(searchQuery => {
        return Rx.Observable.fromPromise(this._queryAutoComplete(searchQuery));
    })
    .subscribe(this._handleSearchResults.bind(this), this._handleSearchError.bind(this));
Owen Sikes
  • 11
  • 2
  • Without a description of precisely what your test is to do, this is too broad. Also, the `Rx.Observable.fromPromise` is redundant, as the projection function passed to `switchMap` can return a promise. – cartant Jun 07 '17 at 01:15
  • Sorry, I've updated the question. I'm mostly interested in how to setup a unit test to watch for the subscribe calls. The _handleSearchResults and the _handleSearchError on error. – Owen Sikes Jun 07 '17 at 01:19
  • @OwenSikes Have a look at this https://stackoverflow.com/questions/42732988/how-do-i-test-a-function-that-returns-an-observable-using-timed-intervals-in-rxj/42734681. Observables are just asynchronous calls so you can test them the same way as any async code. For example in mocha https://mochajs.org/#asynchronous-code – martin Jun 07 '17 at 06:31
  • It depends on what _queryAutoComplete does. You need to provide details. Given this code, you may just mock _queryAutoComplete and come up with a useless test that would check that switchMap works. switchMap is already tested in rx. – Federico Galassi Jun 07 '17 at 06:49

0 Answers0