-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.js
305 lines (235 loc) · 9.1 KB
/
tests.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
'use strict';
var chai = require('chai');
var should = chai.should();
var actionsCreator = require('./');
var immstruct = require('immstruct');
describe('actions', function () {
describe('without immstruct', function () {
var actions;
beforeEach(function () {
actions = actionsCreator;
});
describe('api', function () {
var defaultApiMethods = [
'register', 'remove', 'invoke', 'createComposedInvoker',
'combine', 'subscribe', 'unsubscribe'
];
it('should expose an empty, clean store', function () {
actions.actions.should.eql({});
shouldHaveFunctions(actions, defaultApiMethods);
});
it('should return same API on added action', function () {
var newUnstore = actions.register(noop);
shouldHaveFunctions(newUnstore, defaultApiMethods);
});
it('should return same API on remove action', function () {
var newUnstore = actions.register('foo', noop);
shouldHaveFunctions(newUnstore, defaultApiMethods);
newUnstore = newUnstore.remove('foo');
shouldHaveFunctions(newUnstore, defaultApiMethods);
});
});
describe('register', function () {
it('should have immutable register method, returning new actions with added action', function () {
var fnName = 'fnName';
var newUnstore = actions.register(fnName, noop);
actions.actions.should.eql({});
shouldHaveFunction(newUnstore.actions, fnName);
});
it('should use function name implicitly', function () {
var fnName = 'fnName';
var newUnstore = actions.register(function fnName () { });
shouldHaveFunction(newUnstore.actions, fnName);
});
it('should use defined name if explicitly defined, even though function has name', function () {
var fnName = 'fnName';
var newUnstore = actions.register(fnName, function anotherFnName () { });
shouldHaveFunction(newUnstore.actions, fnName);
});
});
describe('invoke', function () {
it('should be able to invoke registered function', function (done) {
var newUnstore = actions.register('done', done);
newUnstore.invoke('done');
});
it('should be able to pass arguments on single invoke', function (done) {
var newUnstore = actions.register('done', function (a, b) {
a.should.equal('foo');
b.should.equal('bar');
done();
});
newUnstore.invoke('done', 'foo', 'bar');
});
it('should be able to invoke bulk functions', function () {
var numCalls = 0;
var increment = function () { numCalls++ };
var newUnstore = actions
.register('1', increment)
.register('2', increment);
newUnstore.invoke(['1', '2']);
numCalls.should.equal(2);
});
it('should be able to invoke bulk functions with arguments', function (done) {
var numCalls = 0;
var update = function (a, b) {
a.should.equal('foo');
b.should.equal('bar');
if (++numCalls === 2) done();
};
var newUnstore = actions
.register('1', update)
.register('2', update);
newUnstore.invoke(['1', '2'], 'foo', 'bar');
});
});
describe('remove', function () {
it('should return new actions on remove, not altering the existing one', function () {
var fnName = 'fnName', otherFnName = 'shouldKeep';
var newUnstore = actions.register(fnName, noop).register(otherFnName, noop);
shouldHaveFunction(newUnstore.actions, fnName);
var reducedUnstore = newUnstore.remove(fnName);
shouldHaveFunction(newUnstore.actions, fnName);
shouldNotHaveFunction(reducedUnstore.actions, fnName);
shouldHaveFunction(reducedUnstore.actions, otherFnName);
});
it('should remove multiple actions at once', function () {
var newUnstore = actions.register('1', noop).register('2', noop).register('3', noop);
shouldHaveFunctions(newUnstore.actions, ['1', '2']);
var reducedUnstore = newUnstore.remove(['1', '2']);
shouldHaveFunctions(newUnstore.actions, ['1', '2']);
shouldNotHaveFunctions(reducedUnstore.actions, ['1', '2']);
shouldHaveFunction(reducedUnstore.actions, '3');
});
});
describe('combine', function () {
it('should combine one actions with another', function () {
var newUnstore = actions.register('1', noop).register('2', noop)
var anotherUnstore = actions.register('3', noop);
var combinedUnstore = newUnstore.combine(anotherUnstore);
Object.keys(combinedUnstore.actions).length.should.equal(3);
shouldHaveFunctions(combinedUnstore.actions, ['1', '2', '3']);
shouldNotHaveFunctions(anotherUnstore.actions, ['1', '2']);
shouldNotHaveFunction(newUnstore.actions, '3');
});
it('should combine multiple actions', function () {
var newUnstore = actions.register('1', noop).register('2', noop)
var anotherUnstore = actions.register('3', noop);
var combinedUnstore = actions.combine(newUnstore, anotherUnstore);
Object.keys(combinedUnstore.actions).length.should.equal(3);
shouldHaveFunctions(combinedUnstore.actions, ['1', '2', '3']);
shouldNotHaveFunctions(anotherUnstore.actions, ['1', '2']);
shouldNotHaveFunction(newUnstore.actions, '3');
});
});
describe('createComposedInvoker', function () {
it('should create a composed invoker', function () {
var newUnstore = actions
.register('square', function (input) {
return input * input;
})
.register('timesTwo', function (input) {
return input * 2;
});
var invoker = newUnstore.createComposedInvoker('square', 'timesTwo');
invoker(4).should.equal( 8 * (4 * 2) );
});
});
});
describe('subscriber', function () {
it('should trigger subscriber', function (done) {
var myActions = actionsCreator.subscribe(done);
myActions.subscribers[0].should.equal(done);
myActions = myActions.register(function myFunction () { });
myActions.subscribers[0].should.equal(done);
myActions.invoke('myFunction');
});
it('should remember subscribers when creating new', function (done) {
var numCalls = 0;
var increment = function () { numCalls++ };
var myActions = actionsCreator
.subscribe(increment)
.subscribe(increment)
.subscribe(increment)
.subscribe(function () {
numCalls.should.equal(3);
done();
})
.register(function myFunction () { });
myActions.invoke('myFunction');
});
it('should keep subscribers when combining', function (done) {
var numCalls = 0;
var increment = function () { numCalls++ };
var myActions1 = actionsCreator
.subscribe(increment);
var myActions2 = actionsCreator
.subscribe(increment)
.subscribe(increment)
.subscribe(function () {
numCalls.should.equal(3);
done();
})
.register(function myFunction () { });
var myActions = myActions1.combine(myActions2);
myActions.invoke('myFunction');
});
it('should remove subscribers', function (done) {
var numCalls = 0;
var increment = function () { numCalls++ };
var myActions = actionsCreator
.subscribe(increment)
.unsubscribe(increment)
.subscribe(function () {
numCalls.should.equal(0);
done();
})
.register(function myFunction () { });
myActions.invoke('myFunction');
});
});
describe('immstruct cursors', function () {
var actions, structure;
beforeEach(function () {
structure = immstruct({
foo: 4
});
actions = actionsCreator;
});
it('should invoke single action', function () {
var structure = immstruct({ foo: 4 });
var newUnstore = actions
.register('square', function (cursor) {
return cursor.update(function (input) {
return input * input;
});
})
.register('timesTwo', function (cursor) {
return cursor.update(function (input) {
return input * 2;
})
});
var cursor = structure.cursor('foo');
var newCursor = newUnstore.invoke('square', cursor);
newCursor.valueOf().should.equal( 4 * 4 );
newCursor = newUnstore.invoke('timesTwo', cursor);
newCursor.valueOf().should.equal( 4 * 2 );
});
});
function shouldHaveFunction (inst, fn) {
return shouldHaveFunctions(inst, [fn]);
}
function shouldHaveFunctions (inst, functions) {
functions.forEach(function (fnName) {
inst.should.have.property(fnName).that.is.a('function');
});
}
function shouldNotHaveFunction (inst, fn) {
return shouldNotHaveFunctions(inst, [fn]);
}
function shouldNotHaveFunctions (inst, functions) {
functions.forEach(function (fnName) {
inst.should.not.have.property(fnName).that.is.a('function');
});
}
function noop () { }
});