-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
76 lines (66 loc) · 3.13 KB
/
test.js
File metadata and controls
76 lines (66 loc) · 3.13 KB
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
'use strict';
const chai = require('chai');
const sinon = require('sinon');
const expect = chai.expect;
const { ExpirableCache } = require('./index');
describe('test ExpirableCache', () => {
let timer;
beforeEach(() => {
timer = sinon.useFakeTimers();
});
afterEach(() => {
timer.restore();
});
it('test setting and getting values', () => {
const expirableCache = new ExpirableCache();
expirableCache.set('SAMPLE_KEY', "121241323qweerttyysdf");
expirableCache.set('SAMPLE_KEY_EXPIRY', "121241323qweerttyysdf", 10);
});
it('test setting value without expiry', () => {
const expirableCache = new ExpirableCache();
expirableCache.set('SAMPLE_KEY', "121241323qweerttyysdf");
expect(expirableCache.get('SAMPLE_KEY')).equals('121241323qweerttyysdf');
});
it('test setting value with expiry', () => {
const expirableCache = new ExpirableCache();
expirableCache.set('SAMPLE_KEY', "Sample121241323qweerttyysdf");
expirableCache.set('SAMPLE_KEY_EXPIRY', "Expiry121241323qweerttyysdf", 500);
expect(expirableCache.get('SAMPLE_KEY_EXPIRY')).equals('Expiry121241323qweerttyysdf');
timer.tick(510);
// after the cache expired.
expect(expirableCache.get('SAMPLE_KEY')).equals('Sample121241323qweerttyysdf');
expect(expirableCache.get('SAMPLE_KEY_EXPIRY')).equals(undefined);
});
it('test setting value with less expiry', () => {
const expirableCache = new ExpirableCache();
expirableCache.set('SAMPLE_KEY', "Sample121241323qweerttyysdf");
expirableCache.set('SAMPLE_KEY_EXPIRY', "Expiry121241323qweerttyysdf", 700);
expect(expirableCache.get('SAMPLE_KEY_EXPIRY')).equals('Expiry121241323qweerttyysdf');
timer.tick(510);
// after the cache expired.
expect(expirableCache.get('SAMPLE_KEY')).equals('Sample121241323qweerttyysdf');
expect(expirableCache.get('SAMPLE_KEY_EXPIRY')).equals('Expiry121241323qweerttyysdf');
timer.tick(210);
expect(expirableCache.get('SAMPLE_KEY_EXPIRY')).equals(undefined);
});
it('test expiry by setting globally', () => {
const expirableCache = new ExpirableCache({ expireTime: 500 });
expirableCache.set('SAMPLE_KEY', "Sample121241323qweerttyysdf");
expect(expirableCache.get('SAMPLE_KEY')).equals('Sample121241323qweerttyysdf');
timer.tick(510);
// after the cache expired.
expect(expirableCache.get('SAMPLE_KEY')).equals(undefined);
});
it('test calling the callback', () => {
function _callback() { };
const callbackSpy = sinon.spy(_callback)
expect(callbackSpy.calledOnce).to.be.false;
const expirableCache = new ExpirableCache({ expireTime: 500, callback: callbackSpy });
expirableCache.set('SAMPLE_KEY', "Sample121241323qweerttyysdf");
expect(expirableCache.get('SAMPLE_KEY')).equals('Sample121241323qweerttyysdf');
timer.tick(510);
// after the cache expired.
expect(expirableCache.get('SAMPLE_KEY')).equals(undefined);
expect(callbackSpy.calledOnce).to.be.true;
});
});