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

116 lines
4.0 KiB
TypeScript
Raw Normal View History

2019-06-20 00:20:09 +02:00
import { assert, expect, spy, use } from 'chai'
2019-11-11 12:27:18 +01:00
import spies from 'chai-spies'
2019-04-24 02:37:37 +02:00
2020-01-30 22:08:18 +01:00
import { SubscribablePromise } from '../../../src/utils/SubscribablePromise'
2019-04-24 02:37:37 +02:00
use(spies)
2019-06-20 00:20:09 +02:00
describe('SubscribablePromise', () => {
it('should work', async () => {
2019-12-06 10:20:55 +01:00
const subscribable = new SubscribablePromise(() => null)
2019-04-24 02:37:37 +02:00
2019-12-06 10:20:55 +01:00
assert.isDefined(subscribable)
2019-04-24 02:37:37 +02:00
})
2019-06-20 00:20:09 +02:00
describe('#subscribe()', () => {
it('should return a subscription', async () => {
2019-12-06 10:20:55 +01:00
const subscribable = new SubscribablePromise(() => null)
const subscription = subscribable.subscribe(() => null)
2019-04-24 02:37:37 +02:00
assert.isDefined(subscription)
assert.isDefined(subscription.unsubscribe)
2019-06-20 00:20:09 +02:00
assert.typeOf(subscription.unsubscribe, 'function')
2019-04-24 02:37:37 +02:00
})
2019-06-20 00:20:09 +02:00
it('should listen the next values', done => {
2019-04-24 02:37:37 +02:00
const onNextSpy = spy()
2019-12-06 10:20:55 +01:00
const subscribable = new SubscribablePromise(observer => {
2019-06-20 00:20:09 +02:00
setTimeout(() => observer.next('test'), 10)
setTimeout(() => observer.next('test'), 20)
2019-04-24 02:37:37 +02:00
})
2019-12-06 10:20:55 +01:00
subscribable.subscribe(onNextSpy)
2019-04-24 02:37:37 +02:00
setTimeout(() => {
2019-06-20 00:20:09 +02:00
expect(onNextSpy).to.has.been.called.with('test')
2019-04-24 02:37:37 +02:00
expect(onNextSpy).to.has.been.called.exactly(2)
done()
}, 100)
})
})
2019-06-20 00:20:09 +02:00
describe('#then()', () => {
it('should resolve', done => {
2019-04-24 02:37:37 +02:00
const onCompleteSpy = spy()
const onFinallySpy = spy()
2019-12-06 10:20:55 +01:00
const subscribable = new SubscribablePromise(observer => {
2019-06-20 00:20:09 +02:00
setTimeout(() => observer.next('test'), 10)
setTimeout(() => observer.complete('test'), 20)
2019-04-24 02:37:37 +02:00
})
2019-12-06 10:20:55 +01:00
subscribable.then(onCompleteSpy).finally(onFinallySpy)
2019-04-24 02:37:37 +02:00
setTimeout(() => {
2019-06-20 00:20:09 +02:00
expect(onCompleteSpy).to.has.been.called.with('test')
2019-04-24 02:37:37 +02:00
expect(onCompleteSpy).to.has.been.called.exactly(1)
expect(onFinallySpy).to.has.been.called.exactly(1)
done()
}, 100)
})
})
2019-06-20 00:20:09 +02:00
describe('#error()', () => {
it('should catch the error', done => {
2019-04-24 02:37:37 +02:00
const onErrorSpy = spy()
const onFinallySpy = spy()
2019-12-06 10:20:55 +01:00
const subscribable = new SubscribablePromise(observer => {
2019-06-20 00:20:09 +02:00
setTimeout(() => observer.next('test'), 10)
setTimeout(() => observer.error('test'), 20)
2019-04-24 02:37:37 +02:00
})
2019-12-06 10:20:55 +01:00
subscribable.catch(onErrorSpy).finally(onFinallySpy)
2019-04-24 02:37:37 +02:00
setTimeout(() => {
2019-06-20 00:20:09 +02:00
expect(onErrorSpy).to.has.been.called.with('test')
2019-04-24 02:37:37 +02:00
expect(onErrorSpy).to.has.been.called.exactly(1)
expect(onFinallySpy).to.has.been.called.exactly(1)
done()
}, 100)
})
})
2019-06-20 00:20:09 +02:00
it('should be able to subscribe and wait for a promise', async () => {
2019-04-24 02:37:37 +02:00
const onNextSpy = spy()
2019-12-06 10:20:55 +01:00
const subscribable = new SubscribablePromise(observer => {
2019-06-20 00:20:09 +02:00
setTimeout(() => observer.next('test'), 10)
setTimeout(() => observer.next('test'), 20)
setTimeout(() => observer.complete('completed'), 30)
2019-04-24 02:37:37 +02:00
})
2019-12-06 10:20:55 +01:00
const result = await subscribable.next(onNextSpy)
2019-04-24 02:37:37 +02:00
2019-06-20 00:20:09 +02:00
expect(onNextSpy).to.has.been.called.with('test')
2019-04-24 02:37:37 +02:00
expect(onNextSpy).to.has.been.called.exactly(2)
2019-06-20 00:20:09 +02:00
assert.equal(result, 'completed')
2019-04-24 02:37:37 +02:00
})
2019-06-20 00:20:09 +02:00
it('should use the result of a the promise as executor to complete the observer', async () => {
2019-04-24 02:37:37 +02:00
const onNextSpy = spy()
2019-12-06 10:20:55 +01:00
const subscribable = new SubscribablePromise(async observer => {
2019-06-20 00:20:09 +02:00
await new Promise(resolve => setTimeout(resolve, 10))
observer.next('test')
await new Promise(resolve => setTimeout(resolve, 10))
observer.next('test')
await new Promise(resolve => setTimeout(resolve, 10))
return 'completed'
2019-04-24 02:37:37 +02:00
})
2019-12-06 10:20:55 +01:00
const result = await subscribable.next(onNextSpy)
2019-04-24 02:37:37 +02:00
2019-06-20 00:20:09 +02:00
expect(onNextSpy).to.has.been.called.with('test')
2019-04-24 02:37:37 +02:00
expect(onNextSpy).to.has.been.called.exactly(2)
2019-06-20 00:20:09 +02:00
assert.equal(result, 'completed')
2019-04-24 02:37:37 +02:00
})
})