eventify 2.0.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,730 +0,0 @@
1
- (function (root) {
2
-
3
- var expect = root.expect || require('expect.js'),
4
- Eventify,
5
- is_commons = typeof require !== 'undefined';
6
-
7
- if (is_commons) {
8
- Eventify = require('../');
9
- } else {
10
- Eventify = root.Eventify;
11
- }
12
-
13
- describe('Eventify', function () {
14
-
15
- var protoMethods = ['on', 'once', 'off', 'trigger', 'stopListening',
16
- 'listenTo', 'listenToOnce'
17
- ];
18
-
19
- if (!is_commons) {
20
- describe('No conflict', function () {
21
- it('should restore original Eventify', function () {
22
- var b = Eventify,
23
- currentVersion = b.noConflict();
24
- expect(currentVersion).to.be(b);
25
- expect(root.Eventify).to.be("original");
26
- });
27
- });
28
- }
29
-
30
- describe("when enabling events using Eventify.enable, it:", function () {
31
- it("should add the Events mixin to passed prototype", function () {
32
- var target = {};
33
- Eventify.enable(target);
34
- expect(target).to.include.keys(protoMethods);
35
- });
36
-
37
- it("should return augmented object", function (done) {
38
- Eventify.enable({}).on("foo", function (message) {
39
- expect(message).eql("hello emitter");
40
- done();
41
- }).trigger("foo", "hello emitter");
42
- });
43
-
44
- it("should augment an existing prototype", function (done) {
45
- function Plop() {}
46
- Eventify.enable(Plop.prototype);
47
- (new Plop()).on("foo", function (message) {
48
- expect(message).eql("hello emitter");
49
- done();
50
- }).trigger("foo", "hello emitter");
51
- });
52
-
53
- it("should only augment prototype with expected methods", function () {
54
- function Plop() {}
55
- Plop.prototype.foo = function () {};
56
- Eventify.enable(Plop.prototype);
57
- expect(Plop.prototype).to.only.have.keys(['foo'].concat(protoMethods));
58
- });
59
- });
60
-
61
- describe("Eventify.create", function () {
62
- it("should return an empty event emitter", function (done) {
63
- var emitter = Eventify.create();
64
- protoMethods.forEach(function (method) {
65
- expect(emitter[method]).to.be.a("function");
66
- });
67
- emitter.on("foo", function (foo) {
68
- expect(foo).to.equal("create");
69
- done();
70
- }).trigger("foo", "create");
71
- });
72
- });
73
-
74
-
75
-
76
- describe("On and trigger", function () {
77
- var obj = {
78
- counter: 0
79
- };
80
- Eventify.enable(obj);
81
- it('should increment counter', function () {
82
- obj.on('event', function () {
83
- obj.counter += 1;
84
- });
85
- obj.trigger('event');
86
- expect(obj.counter).to.be(1);
87
- });
88
- it('should increment counter five times', function () {
89
- obj.trigger('event');
90
- obj.trigger('event');
91
- obj.trigger('event');
92
- obj.trigger('event');
93
- expect(obj.counter).to.be(5);
94
- });
95
- });
96
-
97
- describe("Binding and triggering multiple events", function () {
98
- var obj = {
99
- counter: 0
100
- };
101
- Eventify.enable(obj);
102
-
103
- obj.on('a b c', function () {
104
- obj.counter += 1;
105
- });
106
-
107
- it('should only affect active counters', function () {
108
- obj.trigger('a');
109
- expect(obj.counter).to.be(1);
110
-
111
- obj.trigger('a b');
112
- expect(obj.counter).to.be(3);
113
-
114
- obj.trigger('c');
115
- expect(obj.counter).to.be(4);
116
-
117
- obj.off('a c');
118
- obj.trigger('a b c');
119
- expect(obj.counter).to.be(5);
120
- });
121
-
122
- it('should trigger all for each event', function () {
123
- var a, b, obj = {
124
- counter: 0
125
- };
126
- Eventify.enable(obj);
127
- obj.on('all', function (event) {
128
- obj.counter = obj.counter + 1;
129
- if (event === 'a') {
130
- a = true;
131
- }
132
- if (event === 'b') {
133
- b = true;
134
- }
135
- })
136
- .trigger('a b');
137
- expect(a).to.be(true);
138
- expect(b).to.be(true);
139
- expect(obj.counter).to.be(2);
140
- });
141
-
142
-
143
-
144
- it("binding and triggering with event maps", function () {
145
- var increment,
146
- obj = {
147
- counter: 0
148
- };
149
- Eventify.enable(obj);
150
-
151
- increment = function () {
152
- this.counter += 1;
153
- };
154
-
155
- obj.on({
156
- a: increment,
157
- b: increment,
158
- c: increment
159
- }, obj);
160
-
161
- obj.trigger('a');
162
- expect(obj.counter).eql(1);
163
-
164
- obj.trigger('a b');
165
- expect(obj.counter).eql(3);
166
-
167
- obj.trigger('c');
168
- expect(obj.counter).eql(4);
169
-
170
- obj.off({
171
- a: increment,
172
- c: increment
173
- }, obj);
174
- obj.trigger('a b c');
175
- expect(obj.counter).eql(5);
176
- });
177
-
178
-
179
- it("listenTo and stopListening", function () {
180
- var a = Eventify.enable({}),
181
- b = Eventify.enable({});
182
- a.listenTo(b, 'all', function () {
183
- expect(true).to.be.ok();
184
- });
185
- b.trigger('anything');
186
- a.listenTo(b, 'all', function () {
187
- expect(false).not.to.be.ok();
188
- });
189
- a.stopListening();
190
- b.trigger('anything');
191
- });
192
-
193
- it("listenTo and stopListening with event maps", function () {
194
- var a = Eventify.enable({}),
195
- b = Eventify.enable({}),
196
- cb = function () {
197
- expect(true).to.be.ok();
198
- };
199
- a.listenTo(b, {
200
- event: cb
201
- });
202
- b.trigger('event');
203
- a.listenTo(b, {
204
- event2: cb
205
- });
206
- b.on('event2', cb);
207
- a.stopListening(b, {
208
- event2: cb
209
- });
210
- b.trigger('event event2');
211
- a.stopListening();
212
- b.trigger('event event2');
213
- });
214
-
215
- it("stopListening with omitted args", function () {
216
- var a = Eventify.enable({}),
217
- b = Eventify.enable({}),
218
- cb = function () {
219
- expect(true).to.be.ok();
220
- };
221
- a.listenTo(b, 'event', cb);
222
- b.on('event', cb);
223
- a.listenTo(b, 'event2', cb);
224
- a.stopListening(null, {
225
- event: cb
226
- });
227
- b.trigger('event event2');
228
- b.off();
229
- a.listenTo(b, 'event event2', cb);
230
- a.stopListening(null, 'event');
231
- a.stopListening();
232
- b.trigger('event2');
233
- });
234
-
235
- it("listenToOnce and stopListening", function () {
236
- var a = Eventify.enable({}),
237
- b = Eventify.enable({});
238
- a.listenToOnce(b, 'all', function () {
239
- expect(true).to.be.ok();
240
- });
241
- b.trigger('anything');
242
- b.trigger('anything');
243
- a.listenToOnce(b, 'all', function () {
244
- expect(false).not.to.be.ok();
245
- });
246
- a.stopListening();
247
- b.trigger('anything');
248
- });
249
-
250
- it("listenTo, listenToOnce and stopListening", function () {
251
- var a = Eventify.enable({}),
252
- b = Eventify.enable({});
253
- a.listenToOnce(b, 'all', function () {
254
- expect(true).to.be.ok();
255
- });
256
- b.trigger('anything');
257
- b.trigger('anything');
258
- a.listenTo(b, 'all', function () {
259
- expect(false).not.to.be.ok();
260
- });
261
- a.stopListening();
262
- b.trigger('anything');
263
- });
264
-
265
- it("listenTo and stopListening with event maps", function () {
266
- var a = Eventify.enable({}),
267
- b = Eventify.enable({});
268
- a.listenTo(b, {
269
- change: function () {
270
- expect(true).to.be.ok();
271
- }
272
- });
273
- b.trigger('change');
274
- a.listenTo(b, {
275
- change: function () {
276
- expect(false).not.to.be.ok();
277
- }
278
- });
279
- a.stopListening();
280
- b.trigger('change');
281
- });
282
-
283
-
284
- it("listenTo yourself", function () {
285
- var e = Eventify.enable({});
286
- e.listenTo(e, "foo", function () {
287
- expect(true).to.be.ok();
288
- });
289
- e.trigger("foo");
290
- });
291
-
292
- it("listenTo yourself cleans yourself up with stopListening",
293
- function () {
294
- var e = Eventify.enable({});
295
- e.listenTo(e, "foo", function () {
296
- expect(true).to.be.ok();
297
- });
298
- e.trigger("foo");
299
- e.stopListening();
300
- e.trigger("foo");
301
- });
302
-
303
- it("listenTo with empty callback doesn't throw an error", function () {
304
- var e = Eventify.enable({});
305
- e.listenTo(e, "foo", null);
306
- e.trigger("foo");
307
- expect(true).to.be.ok();
308
- });
309
-
310
- });
311
-
312
- describe("On, then unbind all functions", function () {
313
- var callback,
314
- obj = {
315
- counter: 0
316
- };
317
- Eventify.enable(obj);
318
- callback = function () {
319
- obj.counter = obj.counter + 1;
320
- };
321
- obj.on('event', callback);
322
- obj.trigger('event');
323
- obj.off('event');
324
- obj.trigger('event');
325
- it("should have only been incremented once", function () {
326
- expect(obj.counter).to.be(1);
327
- });
328
- });
329
-
330
- describe("Bind two callbacks, unbind only one", function () {
331
- var callback,
332
- obj = {
333
- counterA: 0,
334
- counterB: 0
335
- };
336
- Eventify.enable(obj);
337
- callback = function () {
338
- obj.counterA = obj.counterA + 1;
339
- };
340
- obj.on('event', callback);
341
- obj.on('event', function () {
342
- obj.counterB = obj.counterB + 1;
343
- });
344
- obj.trigger('event');
345
- obj.off('event', callback);
346
- obj.trigger('event');
347
- it("should only increment counterA once", function () {
348
- expect(obj.counterA).to.be(1);
349
- });
350
- it("should increment counterB twice", function () {
351
- expect(obj.counterB).to.be(2);
352
- });
353
- });
354
-
355
- describe("Unbind a callback in the midst of it firing", function () {
356
- var callback,
357
- obj = {
358
- counter: 0
359
- };
360
- Eventify.enable(obj);
361
- callback = function () {
362
- obj.counter = obj.counter + 1;
363
- obj.off('event', callback);
364
- };
365
- obj.on('event', callback);
366
- obj.trigger('event');
367
- obj.trigger('event');
368
- obj.trigger('event');
369
- it('should unbound the callback', function () {
370
- expect(obj.counter).to.be(1);
371
- });
372
- });
373
-
374
- describe("Two binds that unbind themselves", function () {
375
- var obj = {
376
- counterA: 0,
377
- counterB: 0
378
- };
379
- Eventify.enable(obj);
380
-
381
- function incrA() {
382
- obj.counterA = obj.counterA + 1;
383
- obj.off('event', incrA);
384
- }
385
-
386
- function incrB() {
387
- obj.counterB = obj.counterB + 1;
388
- obj.off('event', incrB);
389
- }
390
- obj.on('event', incrA);
391
- obj.on('event', incrB);
392
- obj.trigger('event');
393
- obj.trigger('event');
394
- obj.trigger('event');
395
- it('should have incremented counterA only once', function () {
396
- expect(obj.counterA).to.be(1);
397
- });
398
- it('should have incremented counterB only once', function () {
399
- expect(obj.counterB).to.be(1);
400
- });
401
- });
402
-
403
- describe("bind a callback with a supplied context", function () {
404
- it('should bound `this` to the callback', function (done) {
405
- var obj,
406
- TestClass = function () {
407
- return this;
408
- };
409
- TestClass.prototype.assertTrue = function () {
410
- expect(true).to.be(true);
411
- done();
412
- };
413
- obj = Eventify.enable();
414
- obj.on('event', function () {
415
- this.assertTrue();
416
- }, (new TestClass()));
417
- obj.trigger('event');
418
- });
419
- });
420
-
421
- describe("nested trigger with unbind", function () {
422
- var obj = {
423
- counter: 0
424
- };
425
- Eventify.enable(obj);
426
-
427
- function incr1() {
428
- obj.counter = obj.counter + 1;
429
- obj.off('event', incr1);
430
- obj.trigger('event');
431
- }
432
-
433
- function incr2() {
434
- obj.counter = obj.counter + 1;
435
- }
436
- obj.on('event', incr1);
437
- obj.on('event', incr2);
438
- obj.trigger('event');
439
- it('should have been incremented the counter three times',
440
- function (done) {
441
- expect(obj.counter).to.be(3);
442
- done();
443
- });
444
- });
445
-
446
-
447
- describe("callback list is not altered during trigger", function () {
448
- var counter = 0,
449
- obj = Eventify.enable();
450
-
451
- function incr() {
452
- counter = counter + 1;
453
- }
454
- it('prevents bind from altering the callback list', function () {
455
- obj.on('event', function () {
456
- obj.on('event', incr).on('all', incr);
457
- })
458
- .trigger('event');
459
- expect(counter).to.be(0);
460
- });
461
- it('prevents unbind from altering the callback list', function () {
462
- obj.off()
463
- .on('event', function () {
464
- obj.off('event', incr).off('all', incr);
465
- })
466
- .on('event', incr)
467
- .on('all', incr)
468
- .trigger('event');
469
- expect(counter).to.be(2);
470
- });
471
- });
472
-
473
- describe("#1282 - 'all' callback list is retrieved after each event.",
474
- function () {
475
- var counter = 0,
476
- obj = Eventify.enable();
477
-
478
- function incr() {
479
- counter = counter + 1;
480
- }
481
- it('should retrieve all the callbacks', function () {
482
- obj.on('x', function () {
483
- obj.on('y', incr).on('all', incr);
484
- })
485
- .trigger('x y');
486
- expect(counter).to.be(2);
487
- });
488
- });
489
-
490
- describe("if no callback is provided, `on` is a noop", function () {
491
- Eventify.enable().on('test').trigger('test');
492
- });
493
-
494
- it("if callback is truthy but not a function, `on` should throw an error" +
495
- " just like jQuery", function (done) {
496
- var view = Eventify.enable({}).on('test', 'noop');
497
- try {
498
- view.trigger('test');
499
- } catch (e) {
500
- expect(e).to.be.an(Error);
501
- done();
502
- }
503
- });
504
-
505
- describe("remove all events for a specific context", function () {
506
- it("should remove context", function (done) {
507
- var obj = Eventify.enable();
508
- obj.on('x y all', function () {
509
- expect(true).to.be(true);
510
- });
511
- obj.on('x y all', function () {
512
- expect(true).to.be(false);
513
- }, obj);
514
- obj.off(null, null, obj);
515
- obj.trigger('x y');
516
- done();
517
- });
518
- });
519
-
520
- describe("remove all events for a specific callback", function () {
521
- it("should remove callback", function (done) {
522
- var obj = Eventify.enable();
523
-
524
- function success() {
525
- expect(true).to.be(true);
526
- }
527
-
528
- function fail() {
529
- expect(true).to.be(false);
530
- }
531
- obj.on('x y all', success);
532
- obj.on('x y all', fail);
533
- obj.off(null, fail);
534
- obj.trigger('x y');
535
- done();
536
- });
537
- });
538
-
539
- describe("off is chainable", function () {
540
- it("should be chainable", function () {
541
- var obj = Eventify.enable();
542
- // With no events
543
- expect(obj.off() === obj).to.be(true);
544
- // When removing all events
545
- obj.on('event', function () {}, obj);
546
- expect(obj.off() === obj).to.be(true);
547
- // When removing some events
548
- obj.on('event', function () {}, obj);
549
- expect(obj.off('event') === obj).to.be(true);
550
- });
551
- });
552
-
553
- describe("#1310 - off does not skip consecutive events", function () {
554
- it("should not skip", function (done) {
555
- var obj = Eventify.enable();
556
- obj.on('event', function () {
557
- expect(true).to.be(false);
558
- }, obj);
559
- obj.on('event', function () {
560
- expect(true).to.be(false);
561
- }, obj);
562
- obj.off(null, null, obj);
563
- obj.trigger('event');
564
- done();
565
- });
566
- });
567
-
568
-
569
- describe("When attaching an event listener only once, it:",
570
- function () {
571
- it("once", function () {
572
- // Same as the previous test, but we use once rather than
573
- // having to explicitly unbind
574
- var incrA, incrB,
575
- obj = {
576
- counterA: 0,
577
- counterB: 0
578
- };
579
- Eventify.enable(obj);
580
- incrA = function () {
581
- obj.counterA += 1;
582
- obj.trigger('event');
583
- };
584
- incrB = function () {
585
- obj.counterB += 1;
586
- };
587
- obj.once('event', incrA);
588
- obj.once('event', incrB);
589
- obj.trigger('event');
590
- expect(obj.counterA, 1, 'counterA should have only been ' +
591
- 'incremented once.');
592
- expect(obj.counterB, 1, 'counterB should have only been ' +
593
- 'incremented once.');
594
- });
595
-
596
- it("once variant one", function () {
597
- var f = function () {
598
- expect(true).to.be.ok();
599
- },
600
- a = Eventify.enable({}).once('event', f),
601
- b = Eventify.enable({}).on('event', f);
602
-
603
- a.trigger('event');
604
-
605
- b.trigger('event');
606
- b.trigger('event');
607
- });
608
-
609
- it("once variant two", function () {
610
- var f = function () {
611
- expect(true).to.be.ok();
612
- },
613
- obj = Eventify.enable({});
614
-
615
- obj
616
- .once('event', f)
617
- .on('event', f)
618
- .trigger('event')
619
- .trigger('event');
620
- });
621
-
622
- it("once with off", function () {
623
- var f = function () {
624
- expect(true).to.be.ok();
625
- },
626
- obj = Eventify.enable({});
627
-
628
- obj.once('event', f);
629
- obj.off('event', f);
630
- obj.trigger('event');
631
- });
632
-
633
- it("once with event maps", function () {
634
- var increment,
635
- obj = {
636
- counter: 0
637
- };
638
-
639
- Eventify.enable(obj);
640
-
641
- increment = function () {
642
- this.counter += 1;
643
- };
644
-
645
- obj.once({
646
- a: increment,
647
- b: increment,
648
- c: increment
649
- }, obj);
650
-
651
- obj.trigger('a');
652
- expect(obj.counter).eql(1);
653
-
654
- obj.trigger('a b');
655
- expect(obj.counter).eql(2);
656
-
657
- obj.trigger('c');
658
- expect(obj.counter).eql(3);
659
-
660
- obj.trigger('a b c');
661
- expect(obj.counter).eql(3);
662
- });
663
-
664
- it("once with off only by context", function () {
665
- var context = {},
666
- obj = Eventify.enable({});
667
- obj.once('event', function () {
668
- expect(false).not.to.be.ok();
669
- }, context);
670
- obj.off(null, null, context);
671
- obj.trigger('event');
672
- });
673
-
674
- it("once with asynchronous events", function (done) {
675
- var func = function () {
676
- setTimeout(done, 50);
677
- },
678
- obj = Eventify.enable({}).once('async', func);
679
-
680
- obj.trigger('async');
681
- obj.trigger('async');
682
- });
683
-
684
- it("once with multiple events.", function () {
685
- var obj = Eventify.enable({});
686
- obj.once('x y', function () {
687
- expect(true).to.be.ok();
688
- });
689
- obj.trigger('x y');
690
- });
691
-
692
- it("Off during iteration with once.", function () {
693
- var obj = Eventify.enable({}),
694
- f = function () {
695
- this.off('event', f);
696
- };
697
- obj.on('event', f);
698
- obj.once('event', function () {});
699
- obj.on('event', function () {
700
- expect(true).to.be.ok();
701
- });
702
-
703
- obj.trigger('event');
704
- obj.trigger('event');
705
- });
706
-
707
- it("once without a callback is a noop", function () {
708
- Eventify.enable({}).once('event').trigger('event');
709
- });
710
-
711
- });
712
-
713
-
714
- describe("Additional parameters", function () {
715
- it("should include additional parameters", function (done) {
716
- var obj = Eventify.enable(),
717
- param1 = "one",
718
- param2 = ["two"];
719
- obj.on('event', function (one, two) {
720
- expect(one).to.be(param1);
721
- expect(two).to.be(param2);
722
- done();
723
- });
724
- obj.trigger('event', param1, param2);
725
- });
726
- });
727
-
728
- });
729
-
730
- }(this));