/
index.spec.js
142 lines (123 loc) · 3.53 KB
/
index.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
const fakePromise = require('faked-promise')
const pSingleton = require('./index')
const tick = () => new Promise(resolve => setImmediate(resolve))
describe('p-singleton', () => {
it('returns a promise', () => {
const promise = new Promise(() => {})
const p = pSingleton(() => promise)
expect(p() instanceof Promise).toBe(true)
})
it('can be called multiple times', () => {
const p = pSingleton(() => new Promise(() => {}))
expect(p() instanceof Promise).toBe(true)
expect(p() instanceof Promise).toBe(true)
expect(p() instanceof Promise).toBe(true)
})
it('resolves the promise', async () => {
const [promise, resolve] = fakePromise()
const p = pSingleton(() => promise)
const spy = jest.fn()
p().then(spy)
expect(spy).not.toHaveBeenCalled()
resolve('hey')
await tick()
expect(spy).toHaveBeenCalledWith('hey')
})
it('rejects the promise', async () => {
const [promise, , reject] = fakePromise()
const p = pSingleton(() => promise)
const spy = jest.fn()
p().catch(spy)
expect(spy).not.toHaveBeenCalled()
reject('hey')
await tick()
expect(spy).toHaveBeenCalledWith('hey')
})
it('resolves all the chained then', async () => {
const [promise, resolve] = fakePromise()
const p = pSingleton(() => promise)
const spy = jest.fn()
p().then(spy)
p().then(spy)
p().then(spy)
expect(spy).not.toHaveBeenCalled()
resolve('hey')
await tick()
expect(spy).toHaveBeenCalledWith('hey')
expect(spy).toHaveBeenCalledTimes(3)
})
it('consecutive calls return the same promise', () => {
const p = pSingleton(() => new Promise(() => {}))
expect(p()).toBe(p())
})
it('consecutive calls with different parameters return the same promise', () => {
const p = pSingleton(() => new Promise(() => {}))
expect(p()).toBe(p())
expect(p(2)).toBe(p(2))
expect(p(2, 3)).toBe(p(2, 3))
expect(p(2, 3)).not.toBe(p(2))
expect(p(2)).not.toBe(p(3))
})
it('returns a new promise if rejected', async () => {
let resolve
const p = pSingleton(() => {
const fake = fakePromise()
resolve = fake[1]
return fake[0]
})
const p1 = p()
resolve()
await tick()
const p2 = p()
expect(p1).not.toBe(p2)
})
it('returns a new promise if rejected', async () => {
let reject
const p = pSingleton(() => {
const fake = fakePromise()
reject = fake[2]
return fake[0]
})
const spy = jest.fn()
const p1 = p().catch(spy)
expect(spy).not.toHaveBeenCalled()
reject()
await tick()
expect(spy).toHaveBeenCalled()
const p2 = p()
expect(p1).not.toBe(p2)
})
it('supports a custom serializer', () => {
class A {
constructor (a) {
this.a = a
}
// override the JSON method on purpose
toJSON () {
return 'a'
}
}
const pNoSerializer = pSingleton(() => new Promise(() => {}))
expect(pNoSerializer(new A('a'))).toBe(pNoSerializer(new A('b')))
const p = pSingleton(
() => new Promise(() => {}),
([a]) => a.a,
)
expect(p(new A('a'))).not.toBe(p(new A('b')))
})
it('calls with the context', async () => {
const obj = {
name: 'foo',
p: pSingleton(function () {
return Promise.resolve(this.name)
}),
}
expect(await obj.p()).toBe('foo')
})
it('can be called with multiple arguments', async () => {
const p = pSingleton(function (a, b, c) {
return Promise.resolve(a + b + c)
})
expect(await p(1, 2, 3)).toBe(6)
})
})