-
Notifications
You must be signed in to change notification settings - Fork 0
/
nibelung.test.js
176 lines (143 loc) · 4.38 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
'use strict';
var expect = chai.expect;
describe('Hoard', function () {
var hoard;
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 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);
});
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() {
hoard = new nibelung.Hoard({
namespace: 'unitTest',
persistent: false,
maxRecords: 4,
ttlMilliseconds: 100,
clock: mockClock,
reentrancyProtector: mockReentrancyProtector
});
hoard.clear();
}
});