Created
March 22, 2019 21:40
-
-
Save heat/f5067ffafbf5318d70b360db6720894b to your computer and use it in GitHub Desktop.
rxjs operations / subject
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const expect = require('chai').expect; | |
const EventEmitter = require('events'); | |
const rxjs = require('rxjs'); | |
const { ajax } = require('rxjs/ajax'); | |
const { debounce, debounceTime, distinctUntilChanged, switchMap, tap } = require('rxjs/operators'); | |
describe('A emitter', function () { | |
it('should emit something', async function (done) { | |
const emitter = new EventEmitter(); | |
const expected = 'a data'; | |
emitter.on('data', (d) => { | |
expect(d).to.be.equal(expected); | |
done(); | |
}); | |
emitter.emit('data', expected); | |
}); | |
}) | |
describe('A Subject', function () { | |
it('should be easy to instantiate', function () { | |
const subject = new rxjs.Subject(); | |
expect(subject.closed).to.be.false; | |
subject.unsubscribe(); | |
expect(subject.closed).to.be.true; | |
}) | |
it('should notify some observers', function (done) { | |
const subject = new rxjs.Subject(); | |
const expected = 'notification'; | |
const observers = subject.subscribe((val) => { | |
expect(val).to.equal(expected); | |
done(); | |
}); | |
subject.next(expected); | |
}) | |
it('shouldn\'t debounce over time', function (done) { | |
const subject = new rxjs.Subject(); | |
const debouncer = subject.pipe(debounce(() => rxjs.timer(100))) | |
const subscriber = debouncer.subscribe(() => { | |
done('you never been here'); | |
}); | |
setTimeout(() => { | |
done() | |
}, 200); | |
}) | |
it('should debounce over time with last one', function (done) { | |
const subject = new rxjs.Subject(); | |
const debouncer = subject.pipe(debounce(() => rxjs.timer(100))) | |
const expected = 'Aracaju'; | |
const subscriber = debouncer.subscribe(word => { | |
expect(word).to.be.equal(expected); | |
done(); | |
}); | |
['A', 'Ar', 'Ara', 'Arac', 'Araca', 'Aracaj', 'Aracaju'].forEach(typing => { | |
subject.next(typing); | |
}); | |
}) | |
it('should pipe everything', function (done) { | |
const typing = rxjs.of('T', 'Te', 'Tet', 'Tetr', 'Tetris'); | |
const getGames = keys => [ | |
'tetris', | |
'pitfall', | |
'pong', | |
'corona', | |
].filter(e => e.indexOf(keys.toLowerCase()) > -1); | |
let totalFakeGamesCall = 0; | |
const fakeGamesRequest = keys => rxjs.of(getGames(keys)) | |
.pipe( | |
tap(_ => { | |
//registra o total de chamadas | |
totalFakeGamesCall++; | |
}) | |
); | |
const request = typing.pipe( | |
debounceTime(100), | |
//verificar se valor é igual | |
distinctUntilChanged(), | |
//switchMap para request (cancela outra request em andamento) | |
switchMap(word => fakeGamesRequest(word)) | |
//switch usando o ajax | |
// switchMap(query => ajax('https://api.github.com/search/repositories?q=' + key)) | |
) | |
request.subscribe( games => { | |
//deve retornar apens um jogo | |
expect(games.length).to.be.equal(1); | |
//somente uma chamada para api | |
expect(totalFakeGamesCall).to.be.equal(1); | |
done(); | |
}) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment