-
Notifications
You must be signed in to change notification settings - Fork 0
/
PromiseMap.spec.js
94 lines (77 loc) · 3.04 KB
/
PromiseMap.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import chai from 'chai'
import chaiAsPromised from 'chai-as-promised'
import PromiseMap from './PromiseMap.js'
chai.use(chaiAsPromised)
const { expect } = chai
describe('PromiseMap', () => {
const promiseMap = new PromiseMap()
it('should create a new promise', () => {
const promise = promiseMap.create('test')
expect(promise).to.be.an.instanceof(Promise)
})
it('should test if the promise exists', () => {
expect(promiseMap.has('test')).to.be.true
})
it('should resolve that promise', (done) => {
expect(promiseMap.get('test')).to.eventually.be.equal('Oh! Hi, Mark!').and.notify(done)
expect(promiseMap.resolve('test', 'Oh! Hi, Mark!')).to.be.undefined
})
it('should throw if we try to resolve the promise again', () => {
expect(() => promiseMap.resolve('test', 'Hello'))
.to.throw('Promise "test" doesn\'t exists')
})
it('should throw if we try to reject the promise again', () => {
expect(() => promiseMap.reject('test', 'Hello'))
.to.throw('Promise "test" doesn\'t exists')
})
it('should create a new promise (with a time out)', async () => {
const promise = promiseMap.create('test-timed', 1000)
promiseMap.resolve('test-timed', 'Oh! Hi, Mark!')
await expect(promise).to.eventually.be.equal('Oh! Hi, Mark!')
})
it('should create a new promise that times out', async () => {
const promise = promiseMap.create('test-timed', 100)
await expect(promise).to.eventually.be.rejectedWith('Timed out')
})
it('should throw if we try to resolve the promise again', () => {
expect(() => promiseMap.resolve('test-timed', 'Hello'))
.to.throw('Promise "test-timed" doesn\'t exists')
})
it('should throw if we try to reject the promise again', () => {
expect(() => promiseMap.reject('test-timed', 'Hello'))
.to.throw('Promise "test-timed" doesn\'t exists')
})
it('should create a new promise and cancel it', () => {
const promise = promiseMap.create('test-cancel')
promiseMap.cancel('test-cancel')
})
it('should throw if we try to resolve the promise again', () => {
expect(() => promiseMap.resolve('test-cancel', 'Hello'))
.to.throw('Promise "test-cancel" doesn\'t exists')
})
it('should throw if we try to reject the promise again', () => {
expect(() => promiseMap.reject('test-cancel', 'Hello'))
.to.throw('Promise "test-cancel" doesn\'t exists')
})
it('should create multiple promises and cancel all', () => {
for (let i = 0; i < 10; i++) {
promiseMap.create('test' + i)
}
promiseMap.cancelAll()
expect(promiseMap.size).to.be.equal(0)
})
it('should create multiple promises and resolve all', () => {
for (let i = 0; i < 10; i++) {
promiseMap.create('test' + i)
}
promiseMap.resolveAll('Oh! Hi, Mark!')
expect(promiseMap.size).to.be.equal(0)
})
it('should create multiple promises and reject all', () => {
for (let i = 0; i < 10; i++) {
promiseMap.create('test' + i)
}
promiseMap.rejectAll(new Error('Meeck!'))
expect(promiseMap.size).to.be.equal(0)
})
})