title: How to unit test rxjs debounceTime logic (Angular)?
body: <p>With my lack of knowledge about RxJs I copy->paste used some code using it, modified it and now failing to test it...</p>
  
  <p>Here is the whole component:</p>
  
  <pre><code>import { Component } from '@angular/core';
  import { Observable } from 'rxjs/Observable';
  import { map } from 'rxjs/operator/map';
  import { debounceTime } from 'rxjs/operator/debounceTime';
  import { distinctUntilChanged } from 'rxjs/operator/distinctUntilChanged';
  import { _catch } from 'rxjs/operator/catch';
  import { _do } from 'rxjs/operator/do';
  import { switchMap } from 'rxjs/operator/switchMap';
  import { of } from 'rxjs/observable/of';
  
  import { ApiService } from '../../services/api/api.service';
  
  import { ISearchSuggestion } from './search.interface';
  
  @Component({
    selector: 'msm-search',
    templateUrl: './search.component.html'
  })
  export class SearchComponent {
    public model: string;
  
    private _isSearching = false;
    get isSearching() { return this._isSearching; }
  
    constructor(
      private router: Router,
      private apiService: ApiService
    ) {}
  
    search = (text$: Observable&lt;string&gt;) =&gt; {
      return _do.call(
        switchMap.call(
          _do.call(
            distinctUntilChanged.call(
              debounceTime.call(text$, 300)),
              this.enableSearching.bind(this)
            ),
          this.performSearch.bind(this)
        ),
        () =&gt; {
          this._isSearching = false;
        }
      );
    }
  
    private enableSearching(term: string) {
      if (term.length &gt; 2) {
        this._isSearching = true;
      }
    }
  
    private performSearch(term: string) {
      if (term.length &gt; 2) {
        return _catch.call(
          _do.call(this.apiService.getSearchSuggestions(term)),
          () =&gt; of.call([])
        )
      }
    }
  }
  </code></pre>
  
  <p>I want to test search but neither setTimeout neither jasmine.tick() seems to do a trick, here is example of what I came up with:</p>
  
  <pre><code>it('Should get suggestions if search term is longer than 2 characters', (done) =&gt; {
    const searchTerm = '123test';
    const stringObservable = new Observable&lt;string&gt;(observer =&gt; {
      setTimeout(() =&gt; {
        observer.next(searchTerm);
        observer.complete();
      }, 50);
    });
    spyOn(apiService, 'getSearchSuggestions');
    component.search(stringObservable);
    setTimeout(() =&gt; {
      expect(apiService.getSearchSuggestions).toHaveBeenCalledWith(searchTerm);
      done();
    }, 500);
  });
  </code></pre>
  
  <p>What am I doing wrong?</p>