1
0
mirror of https://github.com/oceanprotocol-archive/squid-js.git synced 2024-02-02 15:31:51 +01:00
squid-js/test/utils/SubscribableObserver.test.ts

72 lines
2.2 KiB
TypeScript
Raw Normal View History

2019-06-20 00:20:09 +02:00
import { assert, expect, spy, use } from 'chai'
import * as spies from 'chai-spies'
2019-04-24 13:25:37 +02:00
2019-06-20 00:20:09 +02:00
import { SubscribableObserver } from '../../src/utils/SubscribableObserver'
2019-04-24 13:25:37 +02:00
use(spies)
2019-06-20 00:20:09 +02:00
describe('SubscribableObserver', () => {
describe('#subscribe()', () => {
it('should be able to add a subcription', async () => {
2019-04-24 13:25:37 +02:00
const observer = new SubscribableObserver()
const subscription = observer.subscribe()
assert.isDefined(subscription.unsubscribe)
2019-06-20 00:20:09 +02:00
assert.typeOf(subscription.unsubscribe, 'function')
2019-04-24 13:25:37 +02:00
})
2019-06-20 00:20:09 +02:00
it('should be able to unsubscribe', async () => {
2019-04-24 13:25:37 +02:00
const observer = new SubscribableObserver()
const subscription = observer.subscribe()
subscription.unsubscribe()
})
})
2019-06-20 00:20:09 +02:00
describe('#next()', () => {
it('should be able to emit next value', async () => {
2019-04-24 13:25:37 +02:00
const onNextSpy = spy()
const observer = new SubscribableObserver()
observer.subscribe(onNextSpy)
2019-06-20 00:20:09 +02:00
observer.next('test')
expect(onNextSpy).to.has.been.called.with('test')
2019-04-24 13:25:37 +02:00
2019-06-20 00:20:09 +02:00
observer.next('test')
2019-04-24 13:25:37 +02:00
expect(onNextSpy).to.has.been.called.exactly(2)
})
})
2019-06-20 00:20:09 +02:00
describe('#complete()', () => {
it('should be able to complete', async () => {
2019-04-24 13:25:37 +02:00
const onCompleteSpy = spy()
const observer = new SubscribableObserver()
observer.subscribe(undefined, onCompleteSpy)
2019-06-20 00:20:09 +02:00
observer.complete('test')
expect(onCompleteSpy).to.has.been.called.with('test')
2019-04-24 13:25:37 +02:00
2019-06-20 00:20:09 +02:00
observer.complete('test')
2019-04-24 13:25:37 +02:00
expect(onCompleteSpy).to.has.been.called.exactly(1)
assert.isTrue(observer.completed)
})
})
2019-06-20 00:20:09 +02:00
describe('#error()', () => {
it('should be able to emit a error', async () => {
2019-04-24 13:25:37 +02:00
const onErrorSpy = spy()
const observer = new SubscribableObserver()
observer.subscribe(undefined, undefined, onErrorSpy)
2019-06-20 00:20:09 +02:00
observer.error('test')
expect(onErrorSpy).to.has.been.called.with('test')
2019-04-24 13:25:37 +02:00
2019-06-20 00:20:09 +02:00
observer.error('test')
2019-04-24 13:25:37 +02:00
expect(onErrorSpy).to.has.been.called.exactly(1)
assert.isTrue(observer.completed)
})
})
})