This repository has been archived by the owner on Dec 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
370 lines (331 loc) · 11.8 KB
/
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
'use strict';
var chai = require('chai'),
expect = chai.expect,
cache = require('./index')();
describe('call-cache', function() {
describe('get', function(){
beforeEach(function() {
cache.clear();
});
it('should throw an error if no parameter is given', function(){
expect(function() {
cache.get();
}).to.throw();
});
it('should throw an error if no key is not a string', function(){
expect(function() {
cache.get(null, function(){}, function(){});
}).to.throw();
expect(function() {
cache.get(0, function(){}, function(){});
}).to.throw();
expect(function() {
cache.get('key', function(){}, function(){});
}).to.not.throw();
});
it('should throw an error if generator is not defined or a function', function(){
expect(function() {
cache.get('key');
}).to.throw();
expect(function() {
cache.get('key', null);
}).to.throw();
expect(function() {
cache.get('key', 0);
}).to.throw();
expect(function() {
cache.get('key', function(){});
}).to.not.throw();
});
it('should throw an error if callback is defined and not a function while options is defined', function(){
expect(function() {
cache.get('key', function(){}, 0);
}).to.not.throw();
expect(function() {
cache.get('key', function(){}, 0, 1000);
}).to.throw();
expect(function() {
cache.get('key', function(){}, null);
}).to.not.throw();
expect(function() {
cache.get('key', function(){}, null, 1000);
}).to.throw();
expect(function() {
cache.get('key', function(){}, "string");
}).to.not.throw();
expect(function() {
cache.get('key', function(){}, "string", 1000);
}).to.throw();
expect(function() {
cache.get('key', function(){}, function(){});
}).to.not.throw();
expect(function() {
cache.get('key', function(){}, function(){}, 1000);
}).to.not.throw();
});
it('should generate arguments and return them to callback directly', function(done){
var obj = {foo: 10};
var tests = [
{val: 10, expected: 10},
{val: "string", expected: "string"},
{val: obj, expected: obj},
];
var doneN = 0;
tests.forEach(function(test, i){
cache.get('key'+i, function(){
return test.val;
}, function(val){
try{
expect(val).to.equal(test.expected);
}catch(e){
return done(e);
}
});
if(++doneN === tests.length){
done();
}
});
});
it('should generate arguments and send them to given callback', function(done){
var obj = {foo: 10};
var tests = [
{args: [10], expected: [10]},
{args: ["string"], expected: ["string"]},
{args: [ obj], expected: [ obj]},
{args: [10, "string", obj], expected: [10, "string", obj]}
];
var doneN = 0;
tests.forEach(function(test, i){
cache.get('key'+i, function(callback){
callback.apply(null, test.args);
}, function(){
for(var j = 0; j < test.expected.length; ++j){
try{
expect(arguments[j]).to.equal(test.expected[j]);
}catch(e){
return done(e);
}
}
if(++doneN === tests.length){
done();
}
});
});
});
it('should regenerate only after timeout', function(done){
var i = 0;
var exec = function(assertion){
cache.get('key', function(callback){
++i;
callback(i);
}, function(val){
assertion(val)
}, 25);
}
exec(function(val){expect(val).to.equal(1);});
setTimeout(function () {
exec(function(val){expect(val).to.equal(1);});
}, 12);
setTimeout(function () {
exec(function(val){
expect(val).to.equal(2);
done();
});
}, 50);
});
});
describe('del', function(){
beforeEach(function() {
cache.clear();
});
it('should return false for a key to an empty cache', function(){
expect(cache.del('empty')).to.be.false;
});
it('should return false given a key not in a non-empty cache', function(){
cache.get('key', function(){}, function(){
expect(cache.del('miss')).to.be.false;
});
});
it('should return true given a key for an existing cache', function(){
cache.get('key', function(){}, function(){
expect(cache.del('key')).to.be.true;
});
});
it('should force cache to regenerate', function(){
var i = 0;
var exec = function(assertion){
cache.get('key', function(callback){
++i;
callback(i);
}, function(val){
assertion(val)
}, 10);
}
exec(function(val){
expect(val).to.equal(1);
cache.del('key');
exec(function(val){
expect(val).to.equal(2);
cache.del('key');
});
});
});
});
describe('clear', function(){
it('should clear all existing cache', function(){
cache.get('key1', function(){}, function(){
cache.get('key2', function(){}, function(){
cache.clear();
expect(cache.del('key1')).to.be.false;
expect(cache.del('key2')).to.be.false;
});
});
});
});
describe('promise', function(){
beforeEach(function() {
cache.clear();
});
it('should return a Promise', function(){
var p = cache.get('key', function(){});
expect(p).to.be.an('Promise');
});
it('should return a Promise with value from direct return', function(done){
var obj = {foo: 10};
var tests = [
{val: 10, expected: 10},
{val: "string", expected: "string"},
{val: obj, expected: obj},
];
var doneN = 0;
tests.forEach(function(test, i){
var p = cache.get('key'+i, function(){
return test.val;
});
p.then(function(val){
try{
expect(val).to.equal(test.expected);
}catch(e){
return done(e);
}
if(++doneN === tests.length){
done();
}
});
});
});
it('should return a Promise using the cb with the first argument', function(done){
var obj = {foo: 10};
var tests = [
{args: [10], expected: [10]},
{args: ["string"], expected: ["string"]},
{args: [ obj], expected: [ obj]},
{args: [10, "string", obj], expected: [10, "string", obj]}
];
var doneN = 0;
tests.forEach(function(test, i){
var p = cache.get('key'+i, function(callback){
callback.apply(null, test.args);
});
p.then(function(val){
try{
expect(val).to.equal(test.expected[0]);
}catch(e){
return done(e);
}
if(++doneN === tests.length){
done();
}
});
});
});
it('should return a Promise with value from returned Promise', function(done){
var obj = {foo: 10};
var tests = [
{args: 10, expected: 10},
{args: "string", expected: "string"},
{args: obj, expected: obj}
];
var doneN = 0;
tests.forEach(function(test, i){
var p = cache.get('key'+i, function(){
return Promise.resolve(test.args);
});
p.then(function(val){
try{
expect(val).to.equal(test.expected);
}catch(e){
return done(e);
}
if(++doneN === tests.length){
done();
}
});
});
});
it('should be able to reuse a Promise', function(done){
var val = 'hello';
var exec = function(){
return cache.get('key', function(){
return Promise.resolve(val);
});
};
exec().then(function(out){
try{
expect(out).to.equal(val);
exec().then(function(out){
try{
expect(out).to.equal(val);
exec().then(function(out){
try{
expect(out).to.equal(val);
done();
}catch(e){
done(e);
}
});
}catch(e){
done(e);
}
});
}catch(e){
done(e);
}
});
});
it('should be able to catch a rejected Promise', function(done){
var exec = function(){
return cache.get('key', function(){
return Promise.reject(new Error());
});
};
exec().then(function(){
done(new Error('Should not complete'));
}).catch(function(e){
try{
expect(e).to.be.a('Error');
done();
}catch(e){
done(e);
}
});
});
it('should be able to catch an uncaught error', function(done){
var exec = function(){
return cache.get('key', function(){
throw new Error()
});
};
exec().then(function(){
done(new Error('Should not complete'));
}).catch(function(e){
try{
expect(e).to.be.a('Error');
done();
}catch(e){
done(e);
}
});
});
});
});