-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtest.js
125 lines (115 loc) · 2.87 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
const test = require('tape')
const clip = require('./dist/money-clip')
const getWaitPromise = fn =>
new Promise(resolve =>
setTimeout(() => {
resolve(fn())
}, 200)
)
const getLibStub = () => {
const cache = {}
return {
set: (key, value) =>
new Promise(resolve => {
cache[key] = value
resolve(null)
}),
get: key => Promise.resolve(cache[key]),
del: key =>
new Promise(resolve => {
delete cache[key]
resolve(null)
}),
keys: () => Promise.resolve(Object.keys(cache)),
clear: () =>
new Promise(resolve => {
for (const key in cache) {
delete cache[key]
}
resolve(null)
}),
}
}
test('exports', t => {
t.ok(clip.keyValLib, 'has Store export')
t.end()
})
test('basic get/set works', t => {
const opts = { lib: getLibStub() }
clip
.set('thing', 'value', opts)
.then(() => clip.get('thing', opts))
.then(val => {
t.equal(val, 'value')
t.end()
})
})
test('basic max age works', t => {
const opts = { lib: getLibStub(), maxAge: 100 }
clip
.set('thing', 'value', opts)
.then(() => clip.get('thing', opts))
.then(val => {
t.equal(val, 'value')
return clip.getAll(opts)
})
.then(result => {
t.deepEqual(result, { thing: 'value' })
return getWaitPromise(() => clip.get('thing', opts))
})
.then(val => {
t.equal(val, null, 'should now be null')
return clip.getAll(opts)
})
.then(result => {
t.deepEqual(result, {}, 'should now be empty object')
t.end()
})
})
test('version mismatch causes deletion', t => {
const lib = getLibStub()
clip
.set('thing', 'value', { lib, version: 1 })
.then(() => clip.get('thing', { lib, version: 2 }))
.then(res => {
t.equal(res, null, 'should have returned null')
// even using old version it should now be gone
return clip.getAll({ lib, version: 1 })
})
.then(res => {
t.deepEqual(res, {})
t.end()
})
})
test('getConfiguredCache', t => {
const lib = getLibStub()
const cache = clip.getConfiguredCache({
lib,
version: 5,
maxAge: 100,
})
cache
.set('thing', 'value')
.then(() => cache.set('thing2', 'value2'))
.then(() => cache.get('thing'))
.then(val => {
t.equal(val, 'value')
// now try with passing in version manually
// to show the options were actually used
// when using pre-configured cache
return clip.get('thing2', { lib, version: 5 })
})
.then(val => {
t.equal(val, 'value2')
return cache.del('thing2')
})
.then(() => cache.getAll())
.then(res => {
t.deepEqual(res, { thing: 'value' }, 'first should return')
return getWaitPromise(() => cache.getAll())
})
.then(res => {
t.deepEqual(res, {}, 'now it should have expired')
t.end()
})
})