tl; dr is unit test possible with this code without rewriting it?
http://jsbin.com/jezosegopo/edit?js,console
const keyUpObserver = ($input, fetchResults) => {
const source = Rx.Observable.fromEvent($input, 'keyup')
.map(e => e.target.value)
.filter(text => text.length > 2)
.debounce(300)
.distinctUntilChanged();
return source.flatMapLatest(text => Rx.Observable.fromPromise(fetchResults(text)));
};
keyUpObserverthe code above is heavily based on the RxJS autocomplete example and uses debounce to prevent server clogging.
I am trying to execute a unit test this function, but Sinon useFakeTimers does not work.
const clock = sinon.useFakeTimers();
const $input = $('<input>');
const fetchResults = (text) => new Promise(resolve => resolve(text + ' done!'));
keyUpObserver($input, fetchResults).subscribe(text => console.log(text));
$input.val('some text').trigger('keyup');
clock.tick(500);
$input.val('some more text').trigger('keyup');
I guess this is not related to the Sinons either, rather that RxJS uses some internal clocks that should not be affected by an external fake clock.
Given that, in any case, there is unit test my keyUpObserver code without rewriting it to accept the scheduler as well (default in production, test in unit tests)?