tl;dr is it possible to unit test this code without rewriting it?
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)));
};
keyUpObserver in the above code is heavily based on the RxJS autocomplete example, and uses a debounce to prevent hammering the server.
I'm trying to unit test this function, but Sinon's useFakeTimers doesn't appear to be working.
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);
// Enough time should have elapsed to make this a new event
$input.val('some more text').trigger('keyup');
I'm guessing this isn't sinon-related either, rather that RxJS uses some internal clock which must be unaffected by an external fake clock.
Given that, is there anyway to unit test my keyUpObserver code without rewriting it to also take a scheduler (default in production, test in unit tests)?
Aucun commentaire:
Enregistrer un commentaire