-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
test.js
602 lines (509 loc) · 15.6 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
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
'use strict';
require('mocha');
const assert = require('assert');
let Base = require('./');
let base;
describe('constructor', function() {
it('should return an instance of Base', function() {
base = new Base();
assert(base instanceof Base);
});
it('should "visit" over an object to extend the instance', function() {
base = new Base({ foo: 'bar' });
assert.equal(base.cache.foo, 'bar');
const app = new Base(null, { a: true, b: false });
assert(app.options);
assert.equal(app.options.a, true);
assert.equal(app.options.b, false);
});
it('should map "visit" over an array to extend the instance', function() {
base = new Base([{foo: 'bar'}, {baz: 'qux'}]);
assert.equal(base.cache.foo, 'bar');
assert.equal(base.cache.baz, 'qux');
});
it('should set options passed as the second argument', function() {
base = new Base(null, {abc: 'xyz'});
assert.equal(base.options.abc, 'xyz');
});
it('should merge options throughout the inheritance chain', function() {
class Foo extends Base {
constructor(options) {
super(null, options);
this.options.x = 'y';
}
}
class Bar extends Foo {}
const bar = new Bar({a: 'b'});
assert.equal(bar.options.a, 'b');
assert.equal(bar.options.x, 'y');
});
it('should set isApp on the instance', function() {
base = new Base();
assert.equal(base.isApp, true);
});
});
describe('static properties', function() {
beforeEach(function() {
base = new Base();
});
it('should expose `.use` method', function() {
assert.equal(typeof Base.use, 'function');
});
it('should extend static properties', function() {
class Ctor extends Base {}
assert.equal(typeof Ctor.use, 'function');
});
describe('.use', function() {
it('should use a globally loaded plugin through the static use method', function() {
class Ctor extends Base {}
Ctor.use(function(app) {
app.foo = 'bar';
});
const inst = new Ctor();
assert.equal(inst.foo, 'bar');
});
it('should use a globally loaded plugin through the static use method with namespace', function() {
const Foo = Base.namespace('foo');
Foo.use(function(app) {
app.set('bar', 'baz');
});
const inst = new Foo();
assert.equal(inst.get('bar'), 'baz');
assert.equal(inst.foo.bar, 'baz');
});
it('should use different globally installed plugins when using different namespaces', function() {
const Foo = Base.namespace('foo');
const Bar = Base.namespace('bar');
Foo.use(function(app) {
app.set('bar', 'baz');
});
Bar.use(function(app) {
app.set('beep', 'boop');
});
const foo = new Foo();
const bar = new Bar();
assert.equal(foo.get('bar'), 'baz');
assert.equal(foo.foo.bar, 'baz');
assert.equal(typeof foo.get('beep'), 'undefined');
assert.equal(typeof foo.foo.beep, 'undefined');
assert.equal(bar.get('beep'), 'boop');
assert.equal(bar.bar.beep, 'boop');
assert.equal(typeof bar.get('bar'), 'undefined');
assert.equal(typeof bar.bar.bar, 'undefined');
});
});
});
describe('extend prototype methods', function() {
beforeEach(function() {
const Ctor = require('./');
Base = Ctor.namespace();
});
it('should extend the prototype of the given Ctor', function() {
class Ctor extends Base {}
assert.equal(typeof Ctor.use, 'function');
const ctor = new Ctor();
assert.equal(typeof ctor.set, 'function');
assert.equal(typeof ctor.get, 'function');
});
it('should expose `prototype.set` method', function() {
assert.equal(typeof Base.prototype.set, 'function');
});
it('should expose `prototype.get` method', function() {
assert.equal(typeof Base.prototype.get, 'function');
});
it('should expose `prototype.del` method', function() {
assert.equal(typeof Base.prototype.del, 'function');
});
it('should expose `prototype.visit` method', function() {
assert.equal(typeof Base.prototype.visit, 'function');
});
it('should expose `prototype.define` method', function() {
assert.equal(typeof Base.prototype.define, 'function');
});
it('should add prototype methods to the given Ctor', function() {
class Ctor extends Base {}
assert.equal(typeof Ctor.prototype.set, 'function');
assert.equal(typeof Ctor.prototype.get, 'function');
assert.equal(typeof Ctor.prototype.del, 'function');
assert.equal(typeof Ctor.prototype.visit, 'function');
assert.equal(typeof Ctor.prototype.define, 'function');
});
});
describe('instance properties', function() {
beforeEach(function() {
const Ctor = require('./');
Base = Ctor.namespace();
base = new Base();
});
it('should expose the options property', function() {
assert(base.options);
assert.equal(typeof base.options, 'object');
});
it('should expose the cache property', function() {
assert(base.cache);
assert.equal(typeof base.cache, 'object');
});
});
describe('prototype methods', function() {
beforeEach(function() {
const Ctor = require('./');
Base = Ctor.namespace();
base = new Base();
});
describe('.use', function() {
beforeEach(function() {
base = new Base();
});
it('should expose a .use method', function() {
assert(base.use);
assert.equal(typeof base.use, 'function');
});
it('should call the function passed to `use`', function(cb) {
base.use(function(app) {
assert(app);
cb();
});
});
it('should expose the app instance', function(cb) {
base.foo = 'bar';
base.use(function(app) {
assert.equal(app.foo, 'bar');
cb();
});
});
it('should expose the app instance as "this"', function(cb) {
base.foo = 'bar';
base.use(function(app) {
assert.equal(this.foo, 'bar');
cb();
});
});
it('should run a plugin multiple times', function() {
base.count = 0;
function plugin() {
delete this.registered.plugin;
this.count++;
}
base.use(plugin);
base.use(plugin);
base.use(plugin);
assert.equal(base.count, 3);
});
it('should not run a plugin more than once when a plugin name is given', function() {
base.count = 0;
function plugin() {
this.count++;
}
base.use('foo', plugin);
base.use('foo', plugin);
base.use('foo', plugin);
assert.equal(base.count, 1);
});
});
describe('when `isRegistered` is used', function() {
it('should not call a plugin more than once on the same instance', function() {
base.i = 0;
function plugin(app) {
if (app.isRegistered('foo')) return;
base.i++;
}
base.use(plugin);
base.use(plugin);
base.use(plugin);
base.use(plugin);
assert.equal(base.i, 1);
});
it('should not register a plugin when `false` is passed as the 2nd arg', function() {
function plugin(app) {
if (app.isRegistered('foo', false)) return;
this.foo = 'bar';
}
base.use(plugin);
assert(!base.registered.hasOwnProperty('foo'));
assert.equal(base.foo, 'bar');
});
it('should not call a plugin more than once on the same instance', function() {
base.i = 0;
function plugin(app) {
if (app.isRegistered('foo')) return;
base.i++;
}
base.use(plugin);
base.use(plugin);
base.use(plugin);
base.use(plugin);
assert.equal(base.i, 1);
});
});
describe('.define', function() {
it('should define a key-value pair on the instance', function() {
base.define('foo', 'bar');
assert.equal(base.foo, 'bar');
});
it('should define an own property', function() {
base.define('foo', 'bar');
assert(base.hasOwnProperty('foo'));
});
it('should define a non-emumerable property', function() {
base.define('foo', 'bar');
assert(Object.keys(base).indexOf('foo') === -1);
});
it('should multiple properties', function() {
base.define({
foo: 'bar',
baz: 'qux'
});
assert(base.hasOwnProperty('foo'));
assert(base.hasOwnProperty('baz'));
});
});
describe('.set', function() {
it('should set a key-value pair on the instance', function() {
base.set('foo', 'bar');
assert.equal(base.cache.foo, 'bar');
});
it('should set nested property', function() {
base.set('a.b.c', 'd');
assert.equal(base.cache.a.b.c, 'd');
});
it('should set a nested property with the key as an array', function() {
base.set(['a', 'b', 'c'], 'd');
assert.equal(base.cache.a.b.c, 'd');
});
it('should set an object on the instance', function() {
base.set({a: 'b'});
assert.equal(base.cache.a, 'b');
});
});
describe('.get', function() {
it('should get a property from the instance', function() {
base.set({a: 'b'});
assert.equal(base.get('a'), 'b');
});
it('should get a nested property from the instance', function() {
base.set({a: {b: {c: 'd'}}});
assert.equal(base.get('a.b.c'), 'd');
});
it('should get a property using an array', function() {
base.set({a: {b: {c: 'd'}}});
assert.equal(base.get(['a', 'b', 'c']), 'd');
});
it('should get a property using an array of path segments', function() {
base.set({a: {b: {c: 'd'}}});
assert.equal(base.get(['a', 'b', 'c']), 'd');
assert.equal(base.get(['a.b', 'c']), 'd');
});
});
describe('.has', function() {
it('should work with namespaces', function() {
const Ctor = require('./');
Base = Ctor.namespace('cache');
const foo = new Base();
foo.set({a: 'b'});
assert.equal(foo.has('a'), true);
});
it('should check for a property from the instance', function() {
base.set({a: 'b'});
assert.equal(base.has('a'), true);
});
it('should check for a nested property from the instance', function() {
base.set({a: {b: {c: 'd'}}});
assert.equal(base.has('a.b.c'), true);
});
it('should check for a property using an array', function() {
base.set({a: {b: {c: 'd'}}});
assert.equal(base.has(['a', 'b', 'c']), true);
});
it('should check for a property using a list of arguments', function() {
base.set({a: {b: {c: 'd'}}});
assert.equal(base.has('a', 'b', 'c'), true);
assert.equal(base.has(['a', 'b'], 'c'), true);
assert.equal(base.has('a', ['b', 'c']), true);
assert.equal(base.has('a', 'b.c'), true);
});
});
describe('.visit', function() {
it('should visit an object with the given method', function() {
base.visit('set', {a: 'b', c: 'd'});
assert.equal(base.get('a'), 'b');
assert.equal(base.get('c'), 'd');
});
it('should visit an array with the given method', function() {
base.visit('set', [{a: 'b', c: 'd'}]);
assert.equal(base.get('a'), 'b');
assert.equal(base.get('c'), 'd');
});
});
describe('.del', function() {
it('should remove a property', function() {
base.set({a: 'b'});
assert.equal(base.cache.a, 'b');
base.del('a');
assert.equal(typeof base.cache.a, 'undefined');
});
it('should remove an array of properties', function() {
base.set({
a: 'a'
});
base.set({
b: 'b'
});
assert.equal(base.cache.a, 'a');
assert.equal(base.cache.b, 'b');
base.del('a');
base.del('b');
assert.equal(typeof base.cache.a, 'undefined');
assert.equal(typeof base.cache.b, 'undefined');
});
});
});
describe('namespaces', function() {
beforeEach(function() {
Base = require('./');
});
describe('constructor', function() {
it('should expose `namespace`', function() {
assert.equal(typeof Base.namespace, 'function');
});
it('should extend the given Ctor with static methods', function() {
const Foo = Base.namespace('cache');
class Ctor extends Foo {}
assert.equal(typeof Ctor.use, 'function');
});
});
describe('prototype methods', function() {
beforeEach(function() {
const Custom = Base.namespace('cache');
base = new Custom();
});
describe('set', function() {
it('should set a key-value pair on the instance', function() {
base.set('foo', 'bar');
assert.equal(base.cache.foo, 'bar');
});
it('should set an object on the instance', function() {
base.set({
a: 'b'
});
assert.equal(base.cache.a, 'b');
});
});
describe('get', function() {
it('should get a property from the instance', function() {
base.set({
a: 'b'
});
assert.equal(base.get('a'), 'b');
});
it('should visit an object with the given method', function() {
base.visit('set', {
a: 'b',
c: 'd'
});
assert.equal(base.get('a'), 'b');
assert.equal(base.get('c'), 'd');
});
it('should visit an array with the given method', function() {
base.visit('set', [{
a: 'b',
c: 'd'
}]);
assert.equal(base.get('a'), 'b');
assert.equal(base.get('c'), 'd');
});
});
describe('del', function() {
it('should remove a property', function() {
base.set({a: 'b'});
assert.equal(base.cache.a, 'b');
base.del('a');
assert.equal(typeof base.cache.a, 'undefined');
});
});
});
});
describe('.base', function() {
beforeEach(function() {
base = new Base();
});
it('should set a `base` property on the instance', function() {
assert(base.base);
assert.equal(typeof base.base, 'object');
});
it('should use `parent` to set app.base', function() {
const foo = new Base();
foo.abc = 'xyz';
const bar = new Base();
bar.parent = foo;
const baz = new Base();
baz.parent = bar;
assert(baz.base);
assert.equal(baz.base.abc, 'xyz');
});
});
describe('.is', function() {
beforeEach(function() {
const Ctor = require('./');
Base = Ctor.namespace();
base = new Base();
});
it('should set a name prefixed with `is` on the instance', function() {
base.is('Foo');
assert(base.isFoo);
assert.equal(base.type, 'foo');
assert.equal(base.isFoo, true);
});
it('should remove isApp when another type is set', function() {
base.is('Foo');
assert(base.isFoo);
assert.equal(base.isApp, undefined);
});
});
describe('events', function() {
beforeEach(function() {
base = new Base();
});
it('should emit and listen for events', function(cb) {
base.on('foo', function(val) {
assert.equal(val, 'bar');
cb();
});
base.emit('foo', 'bar');
});
it('should emit set', function(cb) {
base.on('set', function(key, val) {
assert.equal(key, 'foo');
assert.equal(val, 'bar');
cb();
});
base.set('foo', 'bar');
});
it('should emit get', function(cb) {
base.on('get', function(key, val) {
assert.equal(key, 'foo');
assert.equal(val, 'bar');
cb();
});
base.set('foo', 'bar');
base.get('foo');
});
it('should emit del', function(cb) {
base.on('del', function(key, val) {
assert.equal(key, 'foo');
cb();
});
base.set('foo', 'bar');
base.del('foo');
});
it('should emit when a named plugin is registered the first time', function() {
const foo = () => () => {};
let count = 0;
base = new Base();
base.on('plugin', () => (count++));
base.use('foo', foo());
base.use('foo', foo());
base.use('foo', foo());
base.use('foo', foo());
assert.equal(count, 1);
});
});