-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.test.js
229 lines (207 loc) · 9.39 KB
/
index.test.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
'use strict';
const chai = require('chai');
const rewire = require('rewire');
const sinon = require('sinon');
const util = require('util');
chai.use(require('chai-as-promised'));
chai.use(require('sinon-chai'));
const { expect } = chai;
const recycle = rewire('./index');
function testFunctionBuilder(name, {
isResolved = true,
result = 'Why hello there',
delay = 10,
beforeResolving = () => {},
afterResolving = () => {}
} = {}) {
const obj = {
[name]: () => {
return new Promise((resolve, reject) => {
function execute() {
beforeResolving();
isResolved ? resolve(result) : reject(result);
afterResolving();
}
if (!delay) {
return execute();
}
setTimeout(execute, delay);
});
}
};
return obj[name];
}
function getRegistry() {
return recycle.__get__('registry');
}
describe('pending-promise-recycler', () => {
afterEach(() => {
sinon.restore();
});
describe('Basic usage', () => {
it('Executes a promise function once', async () => {
function beforeResolving() {
const registry = getRegistry();
expect(registry).to.have.keys('a-91f967512ad54d194006a3cacf3a94d7f9c4ded44bb194c1e9e0fb1c21cb9a37');
const p = registry.get('a-91f967512ad54d194006a3cacf3a94d7f9c4ded44bb194c1e9e0fb1c21cb9a37');
expect(p).to.be.a('promise');
expect(util.format('%s', p)).to.be.equal('Promise { <pending> }');
}
function afterResolving() {
const registry = getRegistry();
expect(registry).to.have.keys('a-91f967512ad54d194006a3cacf3a94d7f9c4ded44bb194c1e9e0fb1c21cb9a37');
const p = registry.get('a-91f967512ad54d194006a3cacf3a94d7f9c4ded44bb194c1e9e0fb1c21cb9a37');
expect(p).to.be.a('promise');
expect(util.format('%s', p)).to.be.equal('Promise { \'Why hello there\' }');
}
const spy = sinon.spy(testFunctionBuilder('a', { beforeResolving, afterResolving }));
const cachedFunc = recycle(spy);
const result = await cachedFunc('lorem', 'ipsum', 'dolor sit amet');
expect(getRegistry()).to.be.empty;
expect(spy).to.be.calledOnceWithExactly('lorem', 'ipsum', 'dolor sit amet');
expect(result).to.equal('Why hello there');
});
it('Executes a fulfilled promise function twice, recycling the promise itself', async () => {
const registry = getRegistry();
const registryGetSpy = sinon.spy(registry, 'get');
const func = testFunctionBuilder('a');
const funcSpy = sinon.spy(func);
const cachedFunc = recycle(funcSpy);
const [promiseA, promiseB] = await Promise.all([
cachedFunc('lorem', 'ipsum', 'dolor sit amet'),
cachedFunc('lorem', 'ipsum', 'dolor sit amet'),
]);
expect(registryGetSpy).to.be.calledOnce;
expect(funcSpy).to.be.calledOnce;
expect(registry).to.be.empty;
expect(promiseA).to.equal('Why hello there');
expect(promiseB).to.equal('Why hello there');
});
it('Executes a rejected promise function twice, recycling the promise itself', async () => {
const registry = getRegistry();
const registryGetSpy = sinon.spy(registry, 'get');
const func = testFunctionBuilder('a', { isResolved: false });
const funcSpy = sinon.spy(func);
const cachedFunc = recycle(funcSpy);
const [promiseA, promiseB] = await Promise.allSettled([
cachedFunc('lorem', 'ipsum', 'dolor sit amet'),
cachedFunc('lorem', 'ipsum', 'dolor sit amet'),
]);
expect(registryGetSpy).to.be.calledOnce;
expect(funcSpy).to.be.calledOnce;
expect(registry).to.be.empty;
expect(promiseA).to.have.property('status', 'rejected');
expect(promiseA).to.have.property('reason', 'Why hello there');
expect(promiseB).to.have.property('status', 'rejected');
expect(promiseB).to.have.property('reason', 'Why hello there');
});
});
describe('Error handling', () => {
it('Handles rejected promises, making sure the registry stays clean', async () => {
function beforeResolving() {
const registry = getRegistry();
expect(registry).to.have.keys('a-91f967512ad54d194006a3cacf3a94d7f9c4ded44bb194c1e9e0fb1c21cb9a37');
const p = registry.get('a-91f967512ad54d194006a3cacf3a94d7f9c4ded44bb194c1e9e0fb1c21cb9a37');
expect(p).to.be.a('promise');
expect(util.format('%s', p)).to.be.equal('Promise { <pending> }');
}
function afterResolving() {
const registry = getRegistry();
expect(registry).to.have.keys('a-91f967512ad54d194006a3cacf3a94d7f9c4ded44bb194c1e9e0fb1c21cb9a37');
const p = registry.get('a-91f967512ad54d194006a3cacf3a94d7f9c4ded44bb194c1e9e0fb1c21cb9a37');
expect(p).to.be.a('promise');
}
const spy = sinon.spy(testFunctionBuilder('a', {
isResolved: false,
result: new Error('Ruh-roh'),
beforeResolving,
afterResolving
}));
const cachedFunc = recycle(spy);
const error = await expect(cachedFunc('lorem', 'ipsum', 'dolor sit amet')).to.be.rejected;
expect(getRegistry()).to.be.empty;
expect(spy).to.be.calledOnceWithExactly('lorem', 'ipsum', 'dolor sit amet');
expect(error).to.be.an('error').and.have.property('message', 'Ruh-roh');
});
});
describe('Key builders', () => {
it('Supports custom key builder functions', async () => {
function beforeResolving() {
const registry = getRegistry();
expect(registry).to.have.keys('lorem');
}
const spy = sinon.spy(testFunctionBuilder('a', { beforeResolving }));
const cachedFunc = recycle(spy, {
keyBuilder: (func, ...args) => {
return args[0];
}
});
const result = await cachedFunc('lorem', 'ipsum', 'dolor sit amet');
expect(getRegistry()).to.be.empty;
expect(spy).to.be.calledOnceWithExactly('lorem', 'ipsum', 'dolor sit amet');
expect(result).to.equal('Why hello there');
});
it('Supports a fixed key value', async () => {
function beforeResolving() {
const registry = getRegistry();
expect(registry).to.have.keys('toothbrush');
}
const spy = sinon.spy(testFunctionBuilder('a', { beforeResolving }));
const cachedFunc = recycle(spy, { keyBuilder: 'toothbrush' });
const result = await cachedFunc('lorem', 'ipsum', 'dolor sit amet');
expect(getRegistry()).to.be.empty;
expect(spy).to.be.calledOnceWithExactly('lorem', 'ipsum', 'dolor sit amet');
expect(result).to.equal('Why hello there');
});
it('Works with anonymous functions', async () => {
function beforeResolving() {
const registry = getRegistry();
expect(registry).to.have.keys(
'anonymous-0d0491105dd08721e0911939ca184e9e5a6f924b00dce27a4163ca333049bf20'
);
}
const spy = sinon.spy(testFunctionBuilder('', { beforeResolving }));
const cachedFunc = recycle(spy);
const result = await cachedFunc('lorem');
expect(getRegistry()).to.be.empty;
expect(spy).to.be.calledOnceWithExactly('lorem');
expect(result).to.equal('Why hello there');
});
});
describe('Error handling', () => {
it('Handles promises that cause a TypeError', async () => {
const func = () => {
return new Promise(resolve => {
const hmmm = ''[0][0];
resolve(hmmm);
});
};
const spy = sinon.spy(func);
const cachedFunc = recycle(spy);
await expect(cachedFunc()).to.be.rejected;
expect(getRegistry()).to.be.empty;
});
it('Handles promises that throw an unhandled rejection error', async () => {
const err = new Error('Something went wrong!');
const func = () => {
return new Promise(() => {
throw err;
});
};
const spy = sinon.spy(func);
const cachedFunc = recycle(spy);
await expect(cachedFunc()).to.be.rejectedWith(err);
expect(getRegistry()).to.be.empty;
});
it('Handles async functions that throw an unhandled rejection error', async () => {
const err = new Error('Something went wrong!');
const func = async () => {
throw err;
};
const spy = sinon.spy(func);
const cachedFunc = recycle(spy);
await expect(cachedFunc()).to.be.rejectedWith(err);
expect(getRegistry()).to.be.empty;
});
});
});