Cross Reference: /yui3/src/event-custom/tests/customevent.html
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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>YUI3 Custom Event Tests</title>
<script type="text/javascript" src="/build/yui/yui.js"></script>
</head>
<body class="yui3-skin-sam">
<h1>Event Tests</h1>
<p><input type="button" value="Run Tests" id="btnRun" disabled="true" /></p>
<div id="adiv">a div</div>
<div id="demo" class="yui-module">
<div class="yui-hd">
<h4>Animation Demo</h4>
<a title="remove module" class="yui-remove"><em>x</em></a>
</div>
<div class="yui-bd">
<p>This an example of what you can do with the YUI Animation Utility.</p>
<p><em>Follow the instructions above to see the animation in action.</em></p>
</div>
</div>
<script type="text/javascript">
(function() {
var global_notified;
//YUI.add("selector-native", function(){});
YUI({
filter: (window.location.search.match(/[?&]filter=([^&]+)/) || [])[1] || 'min',
allowRollup: false,
onCSS: function(Y) {
Y.log('CSS is done loading', 'info', 'testcase');
},
logExclude: { get: true, Dom: true, Selector: true, Node: true, attribute: true, event: true, base: true, widget: true },
filters: {
base: 'raw',
// dom: null,
attribute: 'min'
}
}).use("dump", "test", "anim", "console", function(Y) {
// Y.Global.on('yui:log', function(e) {
// console.log('GLOBAL LOG: ' + e.msg);
// });
var button = Y.one('#btnRun');
// Set up the page
button.set("disabled", false);
Y.on("click", function() {
Y.Test.Runner.run();
}, button);
var myConsole = new Y.Console().render();
Y.log('{}');
var testEventTarget = new Y.Test.Case({
name: "Custom event tests",
_should: {
fail: {
testStopFnOnceFromBubbleTarget: "ticket pending",
testStopFnFromBubbleTarget: "ticket pending"
}
},
testAugment: function() {
var fired = false;
var O = function(id) {
this.id = id;
Y.log('O constructor executed ' + id);
}
O.prototype = {
oOo: function(ok) {
Y.log('oOo');
}
}
// pass configuration info into EventTarget with the following
// construct
Y.augment(O, Y.EventTarget, null, null, {
emitFacade: true
});
var o = new O(),
handle = o.on('testAugment', function(e, arg1, arg2) {
Y.Assert.isTrue(this instanceof O);
Y.Assert.isTrue(e instanceof Y.EventFacade);
Y.Assert.isTrue(e.foo === 'afoo');
Y.Assert.isTrue(e.details[1] === 1);
Y.Assert.isTrue(arg1 === 1);
Y.Assert.isTrue(arg2 === 2);
fired = true;
});
o.fire('testAugment', { foo: 'afoo' }, 1, 2);
Y.Assert.isTrue(fired);
handle.detach();
// if the first argument is not an object, the
// event facade is moved in front of the args rather
// than overwriting existing object.
o.on('testAugment', function(e, arg1, arg2) {
Y.Assert.areEqual(1, arg1);
Y.Assert.areEqual(2, arg2);
});
o.fire('testAugment', 1, 2);
},
test_detach_by_signature: function() {
var anim = new Y.Anim({
node: '#demo',
to: { opacity: 0 }
}),
count = 0;
tester = function() {
count++;
Y.detach('foo', tester);
};
Y.on('foo', tester);
Y.fire('foo');
Y.fire('foo');
Y.Assert.areEqual(1, count);
var onEnd = function() {
count++;
// this.detach('anim:end', onEnd);
this.detach('end', onEnd);
this.setAttrs({
to: { height: 1 },
easing: Y.Easing.bounceOut
});
this.run();
if (count > 2) {
throw new Error('onEnd should only have happened once');
}
};
// anim.on('end', onEnd);
anim.run();
anim.run();
},
testExtend: function() {
var fired = false;
var Base = function() {
Y.log('Base constructor executed');
arguments.callee.superclass.constructor.apply(this, arguments);
}
Y.extend(Base, Y.EventTarget, {
base: function() {
Y.log('all your base...');
}
});
var b = new Base();
b.on('testExtend', function(arg1, arg2) {
Y.Assert.isTrue(this instanceof Base);
Y.Assert.isTrue(arg1 === 1);
Y.Assert.isTrue(arg2 === 2);
fired = true;
});
b.fire('testExtend', 1, 2);
Y.Assert.isTrue(fired);
},
testPrefix: function() {
var fired1 = false,
fired2 = false;
var O = function(id) {
this.id = id;
Y.log('O constructor executed ' + id);
}
O.prototype = {
oOo: function(ok) {
Y.log('oOo');
}
}
// pass configuration info into EventTarget with the following
// construct
Y.augment(O, Y.EventTarget, null, null, {
emitFacade: true,
prefix: 'prefix'
});
var o = new O();
o.on('testPrefix', function(e, arg1, arg2) {
Y.Assert.isTrue(this instanceof O);
fired1 = true;
});
o.on('prefix:testPrefix', function(e, arg1, arg2) {
Y.Assert.isTrue(this instanceof O);
fired2 = true;
});
o.fire('testPrefix', { foo: 'afoo' }, 1, 2);
Y.Assert.isTrue(fired1);
// Y.Assert.isTrue(fired2);
fired1 = false;
fired2 = false;
o.fire('prefix:testPrefix', { foo: 'afoo' }, 1, 2);
Y.Assert.isTrue(fired1);
Y.Assert.isTrue(fired2);
},
testDetachKey: function() {
var fired1 = false,
fired2 = false;
Y.on('handle|test:event', function() {
fired1 = true;
});
// one listener
Y.fire('test:event');
Y.Assert.isTrue(fired1);
Y.Assert.isFalse(fired2);
Y.detach('handle|test:event');
fired1 = false;
fired2 = false;
Y.on('handle|test:event', function() {
fired2 = true;
});
// first lisener detached, added a new listener
Y.fire('test:event');
Y.Assert.isFalse(fired1);
Y.Assert.isTrue(fired2);
Y.detach('handle|test:event');
fired1 = false;
fired2 = false;
Y.after('handle|test:event', function(arg1) {
Y.Assert.areEqual('orange', arg1);
Y.Assert.isTrue(fired1);
fired2 = true;
});
// comma or pipe
Y.on('handle|test:event', function(arg1) {
Y.Assert.areEqual('orange', arg1);
Y.Assert.isFalse(fired2);
fired1 = true;
});
// testing on and after order
Y.fire('test:event', 'orange');
fired1 = false;
fired2 = false;
// spaces after the comma or lack thereof should have
// no effect on the addition or removal of listeners
var ret = Y.detach('handle|test:event');
Y.Assert.areEqual(Y, ret);
// added both an on listener and an after listener,
// then detached both
Y.fire('test:event', 'orange');
Y.Assert.isFalse(fired1);
Y.Assert.isFalse(fired2);
},
testDetachAllByKey: function() {
var fired1 = false,
fired2 = false;
Y.after('handle|event2', function() {
fired2 = true;
});
Y.on('handle|event2', function() {
fired1 = true;
});
// detachAll
Y.detach('handle|*');
Y.fire('event2');
Y.Assert.isFalse(fired1, 'fired1, the after listener should not have fired.');
Y.Assert.isFalse(fired2, 'fired2, the on listener should not have fired.');
},
testChain: function() {
var fired1 = false,
fired2 = false,
fired3 = false,
fired4 = false,
fired5 = false;
// should be executed once, after f2
var f1 = function() {
Y.Assert.isTrue(fired2);
fired1 = true;
};
// should be executed once, before f1
var f2 = function() {
Y.Assert.isFalse(fired1);
fired2 = true;
};
// should be executed once, different event from f1 and f2
var f3 = function() {
fired3 = true;
};
// detached before fired, should not executed
var f4 = function() {
fired4 = true;
};
// should fire once, preserving the custom prefix rather
// than using the configured event target prefix
var f5 = function() {
fired5 = true;
};
// configure chaining via global default or on the event target
YUI({ /* chain: true */
base:'/build/',
logInclude: {
test: true
}
}).use('event-custom', function(Y2) {
var o = new Y2.EventTarget({
prefix: 'foo',
chain : true
});
// without event target prefix manipulation (incomplete now)
// @TODO an error here is throwing an uncaught exception rather than failing the test
// Y2.after('p:e', f1).on('p:e', f2).on('p:e2', f3).on('detach, p:e', f4).detach('detach, p:e').fire('p:e').fire('p:e2');
// with event target prefix manipulation ('e' is the same event as 'foo:e',
// but 'pre:e' is a different event only accessible by using that exact name)
o.after('e', f1).on('foo:e', f2).on('foo:e2', f3).on('detach, e', f4).detach('detach,e').fire('foo:e').fire('e2').on('pre:e', f5).fire('pre:e');
Y.Assert.isTrue(fired1); // verifies chaining, on/after order, and adding the event target prefix
Y.Assert.isTrue(fired2); // verifies chaining, on/after order, and accepting the prefix in the event name
Y.Assert.isTrue(fired3); // verifies no interaction between events, and prefix manipulation
Y.Assert.isFalse(fired4); // verifies detach works (regardless of spaces after comma)
Y.Assert.isTrue(fired5); // verifies custom prefix
});
},
testObjType: function() {
var f1, f2;
Y.on({
'y:click': function() {f1 = true},
'y:clack': function() {f2 = true}
});
Y.fire('y:click');
Y.fire('y:clack');
Y.Assert.isTrue(f1);
Y.Assert.isTrue(f2);
},
testBubble: function() {
var count = 0,
ret,
config = {
emitFacade: true,
bubbles: true
},
a = new Y.EventTarget(config),
b = new Y.EventTarget(config);
b.addTarget(a);
// this should not be necessary // fixed
// b.publish('test:foo');
a.on('test:foo', function(e) {
count++;
// we will fire this on the parent, so that should be the target
Y.Assert.areEqual(b, e.target);
Y.Assert.areEqual(a, e.currentTarget);
});
ret = b.fire('test:foo', {}, b);
Y.Assert.areEqual(1, count);
Y.Assert.isTrue(ret);
b.on('test:foo', function(e) {
e.stopPropagation();
});
ret = b.fire('test:foo', {}, b);
Y.Assert.areEqual(1, count);
Y.Assert.isFalse(ret);
},
testPreventFnOnce: function() {
var count = 0;
Y.publish('y:foo1', {
emitFacade: true,
preventedFn: function() {
count++;
Y.Assert.isTrue(this instanceof YUI);
}
});
Y.on('y:foo1', function(e) {
e.preventDefault();
});
Y.on('y:foo1', function(e) {
e.preventDefault();
});
Y.fire('y:foo1');
Y.Assert.areEqual(1, count);
},
testPreventFromBubbleTarget: function () {
var count = 0,
target = new Y.EventTarget({ prefix: 'x' });
target.publish('foo', {
emitFacade: true,
preventedFn: function() {
count++;
}
});
target.addTarget(Y);
Y.on('x:foo', function(e) {
e.preventDefault();
});
target.fire('foo');
Y.Assert.areEqual(1, count);
},
testPreventedFnOnceFromBubbleTarget: function () {
var count = 0,
target = new Y.EventTarget({ prefix: 'x' });
target.publish('foo', {
emitFacade: true,
preventedFn: function() {
count++;
}
});
target.addTarget(Y);
Y.on('x:foo', function(e) {
e.preventDefault();
});
Y.on('x:foo', function(e) {
e.preventDefault();
});
target.fire('foo');
Y.Assert.areEqual(1, count);
target.on('foo', function (e) {
e.preventDefault();
});
target.fire('foo');
Y.Assert.areEqual(2, count);
},
testStopFnOnce: function () {
var count = 0,
target = new Y.EventTarget({ prefix: 'a' });
target.publish('foo', {
emitFacade: true,
stoppedFn: function () {
count++;
}
});
target.on('foo', function (e) {
e.stopPropagation();
});
target.on('foo', function (e) {
e.stopPropagation();
});
target.fire('foo');
Y.Assert.areEqual(1, count);
},
testStopFnFromBubbleTarget: function () {
var count = 0,
origin = new Y.EventTarget({ prefix: 'a' }),
targetB = new Y.EventTarget({ prefix: 'b' });
origin.publish('foo', {
emitFacade: true,
stoppedFn: function () {
count++;
}
});
targetB.on('foo', function (e) {
e.stopPropagation();
});
origin.fire('foo');
Y.Assert.areEqual(1, count);
},
testStopFnOnceFromBubbleTarget: function () {
var count = 0,
origin = new Y.EventTarget({ prefix: 'a' }),
targetB = new Y.EventTarget({ prefix: 'b' }),
targetC = new Y.EventTarget({ prefix: 'c' });
origin.publish('foo', {
emitFacade: true,
stoppedFn: function() {
count++;
}
});
targetB.addTarget(targetC);
origin.addTarget(targetB);
targetB.on('foo', function (e) {
e.stopPropagation();
});
targetB.on('foo', function (e) {
e.stopPropagation();
});
origin.fire('foo');
Y.Assert.areEqual(1, count, "stopProp called twice from bubble target resulted in stoppedFn called wrong number of times");
count = 0;
targetC.on('foo', function (e) {
e.stopPropagation();
});
origin.fire('foo');
Y.Assert.areEqual(1, count, "stopProp called from intermediate bubble target didn't prevent stoppedFn call from subsequent bubble target");
count = 0;
origin.on('foo', function (e) {
e.stopPropagation();
});
origin.fire('foo');
Y.Assert.areEqual(1, count, "stopProp called from event origin subscription didn't prevent calls to stoppedFn from bubble target");
},
testDetachHandle: function() {
var count = 0, handle, handle2;
Y.publish('y:foo', {
emitFacade: true
});
Y.on('y:foo', function(e) {
count++;
handle2.detach();
});
handle = Y.on('y:foo', function(e) {
count += 100;
});
handle2 = Y.on('y:foo', function(e) {
count += 1000;
});
Y.detach(handle);
Y.fire('y:foo');
Y.Assert.areEqual(1, count);
count = 0;
var handle3 = Y.on('y:click', function() {
count++;
handle3.detach();
});
Y.fire('y:click');
Y.fire('y:click');
var o = new Y.EventTarget();
count = 0;
o.on('foo', function(e) {
count++;
});
o.on('foo', function(e) {
count++;
});
o.detachAll();
o.fire('foo');
Y.Assert.areEqual(0, count);
var handle3 = Y.on('y:click', function() {
count++;
});
// detachAll can't be allowed to work on the YUI instance.
Y.detachAll();
Y.fire('y:click');
Y.Assert.areEqual(1, count);
},
testBroadcast: function() {
var o = new Y.EventTarget(), s1, s2, s3, s4;
o.publish('y:foo2', {
emitFacade: true,
broadcast: 1
});
Y.on('y:foo2', function() {
Y.log('Y foo2 executed');
s1 = 1;
});
Y.Global.on('y:foo2', function() {
Y.log('GLOBAL foo2 executed');
s2 = 1;
});
o.fire('y:foo2');
Y.Assert.areEqual(1, s1);
Y.Assert.areNotEqual(1, s2);
s1 = 0;
s2 = 0;
o.publish('y:bar', {
emitFacade: true,
broadcast: 2
});
Y.on('y:bar', function() {
Y.log('Y bar executed');
s3 = 1;
});
Y.Global.on('y:bar', function() {
Y.log('GLOBAL bar executed');
s4 = 1;
});
o.fire('y:bar');
Y.Assert.areEqual(1, s3);
Y.Assert.areEqual(1, s4);
Y.Global.on('y:bar', function(e) {
Y.Assert.areEqual(0, e.stopped);
// Y.Assert.areEqual(0, e._event.stopped);
Y.log('GLOBAL bar executed');
e.stopPropagation();
});
o.fire('y:bar');
o.fire('y:bar');
Y.Global.detachAll();
},
test_fire_once: function() {
var notified = 0;
Y.publish('fireonce', {
fireOnce: true
});
Y.fire('fireonce', 'foo', 'bar');
Y.on('fireonce', function(arg1, arg2) {
notified++;
Y.Assert.areEqual('foo', arg1, 'arg1 not correct for lazy fireOnce listener')
Y.Assert.areEqual('bar', arg2, 'arg2 not correct for lazy fireOnce listener')
});
Y.fire('fireonce', 'foo2', 'bar2');
Y.fire('fireonce', 'foo3', 'bar3');
global_notified = false;
Y.on('fireonce', function(arg1, arg2) {
Y.log('the notification is asynchronous, so I need to wait for this test');
Y.Assert.areEqual(1, notified, 'listener notified more than once.');
global_notified = true;
});
// it is no longer asynchronous
// Y.Assert.isFalse(global_notified, 'notification was not asynchronous');
},
test_async_fireonce: function() {
Y.Assert.isTrue(global_notified, 'asynchronous notification did not seem to work.');
},
test_node_publish: function() {
var node = Y.one('#adiv');
var preventCount = 0, heard = 0;
node.publish('foo1', {
emitFacade: true,
// should only be called once
preventedFn: function() {
preventCount++;
Y.Assert.isTrue(this instanceof Y.Node);
}
});
node.on('foo1', function(e) {
Y.Assert.areEqual('faking foo', e.type);
Y.Assert.areEqual('foo1', e._type);
heard++;
e.preventDefault();
});
node.on('foo1', function(e) {
heard++;
e.preventDefault();
});
node.fire('foo1', {
type: 'faking foo'
});
Y.Assert.areEqual(1, preventCount);
Y.Assert.areEqual(2, heard);
},
// SRC, ON
// BUBBLE, ON
// BUBBLE, DEFAULT BEHAVIOR
// BUBBLE, AFTER
// SRC, DEFAULT BEHAVIOR
// SRC, AFTER
__testBubbleSequence300GA: function() {
var count = 0,
called = null,
fn = function() {
called = this.name;
},
config = {
emitFacade: true,
bubbles: true
},
leaf = new Y.EventTarget(config),
branch = new Y.EventTarget(config),
root = new Y.EventTarget(config);
leaf.name = 'leaf';
branch.name = 'branch';
root.name = 'root';
leaf.addTarget(branch);
branch.addTarget(root);
leaf.publish('test:foo', { defaultFn: fn});
branch.publish('test:foo', { defaultFn: fn});
root.publish('test:foo', { defaultFn: fn});
leaf.on('test:foo', function(e) {
Y.Assert.areEqual(0, count, 'leaf.on should be first');
Y.Assert.isNull(called, 'leaf.on should be executed before any default function');
count++;
});
branch.on('test:foo', function() {
Y.Assert.areEqual(1, count, 'branch.on should be second');
Y.Assert.isNull(called, 'branch.on should be executed before any default function');
count++;
});
root.on('test:foo', function() {
Y.Assert.areEqual(2, count, 'root.on should be third');
Y.Assert.isNull(called, 'root.on should be executed before any default function');
count++;
});
root.after('test:foo', function() {
Y.Assert.areEqual(3, count, 'root.after should be fourth');
Y.Assert.areEqual('root', called, 'root.after should be executed after the root default function');
count++;
});
branch.after('test:foo', function() {
Y.Assert.areEqual(4, count, 'branch.after should be fifth');
Y.Assert.areEqual('branch', called, 'branch.after should be executed after the branch default function');
count++;
});
leaf.after('test:foo', function(e) {
Y.Assert.areEqual(5, count, 'leaf.after should be sixth and last');
Y.Assert.areEqual('leaf', called, 'leaf.after should be executed after the leaf default function');
count++;
});
leaf.fire('test:foo', {}, leaf);
Y.Assert.areEqual(6, count);
},
// Ideally it should be this, but the defaultFn order is the least important bit
// and there are issues changing the order.
// SRC, ON
// BUBBLE, ON
// SRC, DEFAULT BEHAVIOR
// BUBBLE, DEFAULT BEHAVIOR (unless configured to only execute the default function on the target)
// SRC, AFTER
// BUBBLE, AFTER
// The actual order is this:
// SRC, ON
// BUBBLE, ON
// BUBBLE, DEFAULT BEHAVIOR
// SRC, DEFAULT BEHAVIOR (unless configured to only execute the default function on the target)
// SRC, AFTER
// BUBBLE, AFTER
testAlternativeSequencePost300GA: function() {
var count = 0,
called = null,
fn = function() {
called = this.name;
},
config = {
emitFacade: true,
bubbles: true
},
leaf = new Y.EventTarget(config),
branch = new Y.EventTarget(config),
root = new Y.EventTarget(config);
leaf.name = 'leaf';
branch.name = 'branch';
root.name = 'root';
leaf.addTarget(branch);
branch.addTarget(root);
leaf.publish('test:foo', { defaultFn: fn});
branch.publish('test:foo', { defaultFn: fn});
root.publish('test:foo', { defaultFn: fn});
leaf.on('test:foo', function(e) {
Y.Assert.areEqual(0, count, 'leaf.on should be first');
Y.Assert.isNull(called, 'leaf.on should be executed before any default function');
count++;
});
branch.on('test:foo', function() {
Y.Assert.areEqual(1, count, 'branch.on should be second');
Y.Assert.isNull(called, 'branch.on should be executed before any default function');
count++;
});
root.on('test:foo', function() {
Y.Assert.areEqual(2, count, 'root.on should be third');
Y.Assert.isNull(called, 'root.on should be executed before any default function');
count++;
});
leaf.after('test:foo', function(e) {
Y.Assert.areEqual(3, count, 'leaf.after should be fourth');
// Y.Assert.areEqual('root', called, 'leaf.after should be executed after the root default function');
Y.Assert.areEqual('leaf', called, 'leaf.after should be executed after the root default function');
count++;
});
branch.after('test:foo', function() {
Y.Assert.areEqual(4, count, 'branch.after should be fifth');
// Y.Assert.areEqual('root', called, 'leaf.after should be executed after the root default function');
Y.Assert.areEqual('leaf', called, 'leaf.after should be executed after the root default function');
count++;
});
root.after('test:foo', function() {
Y.Assert.areEqual(5, count, 'root.after should be sixth and last');
// Y.Assert.areEqual('root', called, 'leaf.after should be executed after the root default function');
Y.Assert.areEqual('leaf', called, 'leaf.after should be executed after the root default function');
count++;
});
leaf.fire('test:foo', {}, leaf);
Y.Assert.areEqual(6, count, 'total subscriber count');
},
testStarSubscriber: function() {
var count = 0,
ret,
config = {
emitFacade: true,
bubbles: true,
prefix: 'stars'
},
z = new Y.EventTarget(config),
a = new Y.EventTarget(config),
b = new Y.EventTarget(config);
b.addTarget(a);
a.addTarget(z);
z.on('*:foo', function(e) {
count++;
// b -> a -> z -- the parent's parent should be the target
Y.Assert.areEqual(b, e.target);
Y.Assert.areEqual(z, e.currentTarget);
switch (count) {
case 1:
Y.Assert.areEqual('a:foo', e.type);
break;
case 2:
Y.Assert.areEqual('b:foo', e.type);
break;
case 3:
Y.Assert.areEqual('stars:foo', e.type);
break;
}
});
ret = b.fire('a:foo', {}, b);
Y.Assert.areEqual(1, count);
Y.Assert.isTrue(ret);
ret = b.fire('b:foo', {}, b);
Y.Assert.areEqual(2, count);
Y.Assert.isTrue(ret);
// if the event target is not configured with a prefix, this won't work by design.
ret = b.fire('foo', {}, b);
Y.Assert.areEqual(3, count);
Y.Assert.isTrue(ret);
},
testPreventBubble: function() {
var count = 0,
ret,
config = {
emitFacade: true,
bubbles: true,
prefix: 'stars'
},
z = new Y.EventTarget(config),
a = new Y.EventTarget(config),
b = new Y.EventTarget(config);
b.addTarget(a);
a.addTarget(z);
z.after('*:foo', function(e) {
// e.preventDefault();
Y.Assert.areEqual(b, e.target);
});
ret = b.fire('a:foo', {}, b);
Y.Assert.areEqual(0, count);
},
test_listen_once: function() {
var count = 0;
Y.once(['foo', 'bar'], function(e) {
count++;
});
Y.fire('foo', 'bar');
Y.fire('bar', 'bar');
Y.Assert.areEqual(2, count);
Y.fire('foo', 'bar');
Y.fire('bar', 'bar');
Y.Assert.areEqual(2, count);
},
test_array_type_param: function() {
var result = '';
var handle1 = Y.after(['foo', 'bar'], function(type) {
result += 'after' + type;
});
var handle2 = Y.on(['foo', 'bar'], function(type) {
result += 'on' + type;
});
Y.fire('foo', 'foo');
Y.fire('bar', 'bar');
Y.Assert.areEqual('onfooafterfooonbarafterbar', result);
handle1.detach();
handle2.detach();
Y.fire('foo', 'foo');
Y.fire('bar', 'bar');
Y.Assert.areEqual('onfooafterfooonbarafterbar', result);
},
test_bubble_config: function() {
var a = new Y.EventTarget(),
b = new Y.EventTarget(),
result;
a.publish("foo", {
emitFacade: true
});
a.addTarget(b);
b.on("foo", function(e) {
result = (e instanceof Y.EventFacade);
});
a.fire("foo");
Y.Assert.isTrue(result);
},
test_onceAfter: function () {
var a = new Y.EventTarget({ emitFacade: true, prefix: 'a' }),
result = '';
a.on('foo', function () { result += 'A'; });
a.once('foo', function () { result += 'B'; });
a.after('foo', function () { result += 'C'; });
a.onceAfter('foo', function () { result += 'D'; });
a.fire('foo');
a.fire('foo');
Y.Assert.areSame("ABCDAC", result);
},
test_multiple_object_publish: function () {
var target = new Y.EventTarget({ emitFacade: true, prefix: 'a' }),
pass;
target.publish({ foo: {} });
target.publish({
bar: {
defaultFn: function () { pass = true; }
}
});
target.fire('bar');
Y.Assert.isTrue(pass);
}
});
Y.Test.Runner.setName("CustomEvent");
Y.Test.Runner.add(testEventTarget);
Y.Test.Runner.run();
});
// YUI({
// base: "/build/",
// filter: "debug",
// combine: false,
// useConsole: true,
// logExclude: {Dom: true, Selector: true, Node: true, attribute: true, base: true, loader: true, get: true, widget: true}
// }).use("datasource", function(Y) {
// Y.log('loaded datasource: ' + Y.DataSource);
// });
})();
</script>
</body>
</html>