forked from arafeek/nibelung
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnibelung.test.js
314 lines (260 loc) · 8.33 KB
/
nibelung.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
'use strict';
var expect = chai.expect;
describe('Hoard', function () {
var hoard;
var hoardLogger;
var hoardInFallbackMode;
var fallbackModeLogger;
var testItems;
var testItems2;
var mockClock;
var mockReentrancyProtector;
beforeEach(initMocks);
beforeEach(initHoard);
it('is constructed correctly', function () {
expect(hoard).to.be.an('Object');
});
it('can save and load some items', function () {
hoard.put(testItems, 'id');
expect(hoard.get(['a', 'b'])).to.eql(testItems);
// Also test that results are returned in the requested order.
expect(hoard.get(['b', 'a'])).to.eql(testItems.reverse());
});
it('can save load, and remove a single item', function () {
var putHandler = sinon.spy(function () {});
var deleteHandler = sinon.spy(function () {});
hoard.on('PUT', putHandler);
hoard.putOne(testItems[0].id, testItems[0]);
expect(putHandler.calledOnce).to.eql(true);
expect(putHandler.calledWith(testItems[0]));
expect(hoard.getOne(testItems[0].id)).to.eql(testItems[0]);
hoard.on('REMOVE', deleteHandler);
hoard.removeOne(testItems[0].id);
expect(putHandler.calledOnce).to.eql(true);
expect(putHandler.calledWith(testItems[0].id));
expect(hoard.getOne(testItems[0].id)).to.eql(undefined);
});
it('can clear all its items', function () {
hoard.put(testItems, 'id');
expect(hoard.get(['a', 'b'])).to.eql(testItems);
hoard.clear();
expect(hoard.get(['a'])).to.eql([]);
expect(hoard.get(['b'])).to.eql([]);
expect(hoard.get(['a', 'b'])).to.eql([]);
});
it('can tell who\'s not in the hoard', function () {
hoard.put(testItems, 'id');
expect(hoard.excludes(['a', 'b', 'c'])).to.eql(['c']);
});
it('can get most recent items', function () {
hoard.put(testItems, 'id');
hoard.put(testItems2, 'id');
var expectedItems = testItems2.reverse();
expectedItems.push(testItems[1]);
expect(hoard.getLatest(3)).to.eql(expectedItems);
});
it('enforces a maximum number of records', function () {
hoard.put(testItems, 'id');
hoard.put(testItems2, 'id');
// The cap is 4, so the oldest item should still be in the hoard.
expect(hoard.get(['a'])).to.eql([testItems[0]]);
// But inserting one more item should kick 'a' out.
var zItem = {
'id': 'z',
foo: 'fooBarQuux'
};
hoard.put([zItem], 'id');
expect(hoard.get(['a'])).to.eql([]);
expect(hoard.get(['z'])).to.eql([zItem]);
});
it('enforces a time-to-live', function () {
hoard.put(testItems, 'id');
expect(hoard.get(['a'])).not.to.eql([]);
expect(hoard.excludes(['a'])).to.eql([]);
mockClock.advanceClock(100);
expect(hoard.get(['a'])).to.eql([]);
expect(hoard.excludes(['a'])).to.eql(['a']);
});
it('emits PUT events', function () {
var putHandler = sinon.spy(function () {});
hoard.on('PUT', putHandler);
hoard.put([testItems[0]], 'id');
expect(putHandler.calledOnce).to.eql(true);
expect(putHandler.calledWith(testItems[0]));
putHandler.reset();
hoard.off('PUT', putHandler);
hoard.put([testItems[1]], 'id');
expect(putHandler.calledOnce).to.eql(false);
});
it('emits CLEAR events', function () {
var clearHandler = sinon.spy(function () {});
hoard.on('CLEAR', clearHandler);
hoard.clear();
expect(clearHandler.calledOnce).to.eql(true);
clearHandler.reset();
hoard.off('CLEAR', clearHandler);
hoard.clear();
expect(clearHandler.calledOnce).to.eql(false);
});
it('complains if you register for a bad event', function () {
var error = null;
try {
hoard.on('GOBBLEDYGOOK', function () {});
}
catch (e) {
error = e;
}
expect(error).to.not.equal(null);
});
it('no longer has that bug where a namespace set to the prefix of a storage ' +
'function would cause getLatest() to blow up in fallback storage mode',
function () {
hoardInFallbackMode.putOne('foo', { foo: 'foo' });
expect(hoardInFallbackMode.getLatest(10))
.to.eql([{ foo: 'foo' }]);
});
it('makes a log entry when localStorage isn\'t available', function () {
expect(hoardLogger.calledOnce).to.eql(false);
expect(fallbackModeLogger.calledOnce).to.eql(true);
});
it('can still clear records in fallback mode', function () {
hoardInFallbackMode.putOne('a', 'b');
hoardInFallbackMode.clear();
expect(hoardInFallbackMode.getOne('a')).to.eql(undefined);
});
it('doesn\'t blow up if people insert bad data into localStorage manually',
function () {
window.localStorage.setItem('unitTestFoo', 'I am a baaad record.');
expect(hoard.getOne('Foo')).to.eql(undefined);
expect(hoard.getLatest(10)).to.eql([]);
});
it('doesn\'t check the version for an unversioned Hoard', function () {
var versionChangeHandler = {
onVersionChange: sinon.spy(function (h, e, a) {
return false;
})
};
var h = new nibelung.Hoard({
namespace: 'noversion',
versionChangeHandler: versionChangeHandler
});
expect(versionChangeHandler.onVersionChange.notCalled).to.eql(true);
expect(h.version()).to.eql('');
});
it('doesn\'t report a newly-versioned Hoard', function () {
var versionChangeHandler = {
onVersionChange: sinon.spy(function () {
return false;
})
};
var h = new nibelung.Hoard({
namespace: 'firstversion',
version: '1',
versionChangeHandler: versionChangeHandler
});
expect(versionChangeHandler.onVersionChange.notCalled).to.eql(true);
// But it saves the version anyway.
expect(h.version()).to.eql('1');
});
it('reports a changed-version Hoard', function () {
var versionChangeHandler = {
onVersionChange: sinon.spy(function () {
return false;
})
};
var h = new nibelung.Hoard({
namespace: 'secondversion',
version: '1',
versionChangeHandler: { onVersionChange: function () { return true; } }
});
expect(h.version()).to.eql('1');
var h = new nibelung.Hoard({
namespace: 'secondversion',
version: '2',
versionChangeHandler: versionChangeHandler
});
expect(versionChangeHandler.onVersionChange.calledOnce).to.eql(true);
expect(versionChangeHandler.onVersionChange.calledWith(
h,
'2',
'1')).to.eql(true);
expect(h.version()).to.eql('1');
});
it('clears the data on a version change if requested', function () {
var h = new nibelung.Hoard({
namespace: 'secondversion',
version: '1',
versionChangeHandler: new nibelung.ClearingVersionChangeHandler()
});
h.put(testItems);
h = new nibelung.Hoard({
namespace: 'secondversion',
version: '2',
versionChangeHandler: new nibelung.ClearingVersionChangeHandler()
});
expect(h.getOne('a')).to.be.undefined;
expect(h.version()).to.eql('2');
});
function initMocks() {
testItems = [{
id: 'a',
foo: 'bar'
}, {
id: 'b',
foo: 'quux'
}];
testItems2 = [{
id: 'q',
foo: 'bar2'
}, {
id: 'w',
foo: 'quux2'
}];
// In reality, Date.now()'s ~15ms precision is good enough for our purposes.
// In unit tests, we need a bit more control.
var tick = 0;
mockClock = {
now: function () {
return tick++;
},
advanceClock: function (ticks) {
tick += ticks;
}
};
// In reality, events are emitted using window.timeout to avoid re-entrancy
// problems. In the tests, we'd like handlers to be called immediately.
mockReentrancyProtector = {
protect: function (fn) {
fn();
}
};
}
function initHoard() {
hoardLogger = sinon.spy();
hoard = new nibelung.Hoard({
namespace: 'unitTest',
persistent: false,
maxRecords: 4,
ttlMilliseconds: 100,
clock: mockClock,
logger: hoardLogger,
reentrancyProtector: mockReentrancyProtector
});
fallbackModeLogger = sinon.spy();
hoardInFallbackMode = new nibelung.Hoard({
namespace: 'removeItem',
persistent: false,
maxRecords: 4,
ttlMilliseconds: 100,
clock: mockClock,
reentrancyProtector: mockReentrancyProtector,
logger: fallbackModeLogger,
storageAvailabilityChecker: {
assertAvailable: function () {
throw new Error('nope!');
}
}
});
hoard.clear();
}
});