-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
event-dispatcher-test.js
691 lines (575 loc) · 21.8 KB
/
event-dispatcher-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
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
import { RenderingTestCase, moduleFor, runTask } from 'internal-test-helpers';
import { Component } from '../utils/helpers';
import { getCurrentRunLoop, run } from '@ember/runloop';
import {
subscribe as instrumentationSubscribe,
reset as instrumentationReset,
} from '@ember/instrumentation';
import { EMBER_IMPROVED_INSTRUMENTATION } from '@ember/canary-features';
import { jQueryDisabled, jQuery } from '@ember/-internals/views';
import { HAS_NATIVE_PROXY } from '@ember/-internals/utils';
import { DEBUG } from '@glimmer/env';
let canDataTransfer = Boolean(document.createEvent('HTMLEvents').dataTransfer);
function fireNativeWithDataTransfer(node, type, dataTransfer) {
let event = document.createEvent('HTMLEvents');
event.initEvent(type, true, true);
event.dataTransfer = dataTransfer;
node.dispatchEvent(event);
}
moduleFor(
'EventDispatcher',
class extends RenderingTestCase {
['@test events bubble view hierarchy for form elements'](assert) {
let receivedEvent;
this.registerComponent('x-foo', {
ComponentClass: Component.extend({
change(event) {
receivedEvent = event;
},
}),
template: `<input id="is-done" type="checkbox">`,
});
this.render(`{{x-foo}}`);
runTask(() => this.$('#is-done').trigger('change'));
assert.ok(receivedEvent, 'change event was triggered');
assert.strictEqual(receivedEvent.target, this.$('#is-done')[0]);
}
['@test case insensitive events'](assert) {
let receivedEvent;
this.registerComponent('x-bar', {
ComponentClass: Component.extend({
clicked(event) {
receivedEvent = event;
},
}),
template: `<button id="is-done" onclick={{action clicked}}>my button</button>`,
});
this.render(`{{x-bar}}`);
runTask(() => this.$('#is-done').trigger('click'));
assert.ok(receivedEvent, 'change event was triggered');
assert.strictEqual(receivedEvent.target, this.$('#is-done')[0]);
}
['@test case sensitive events'](assert) {
let receivedEvent;
this.registerComponent('x-bar', {
ComponentClass: Component.extend({
clicked(event) {
receivedEvent = event;
},
}),
template: `<button id="is-done" onClick={{action clicked}}>my button</button>`,
});
this.render(`{{x-bar}}`);
runTask(() => this.$('#is-done').trigger('click'));
assert.ok(receivedEvent, 'change event was triggered');
assert.strictEqual(receivedEvent.target, this.$('#is-done')[0]);
}
['@test events bubble to parent view'](assert) {
let receivedEvent;
this.registerComponent('x-foo', {
ComponentClass: Component.extend({
change(event) {
receivedEvent = event;
},
}),
template: `{{yield}}`,
});
this.registerComponent('x-bar', {
ComponentClass: Component.extend({
change() {},
}),
template: `<input id="is-done" type="checkbox">`,
});
this.render(`{{#x-foo}}{{x-bar}}{{/x-foo}}`);
runTask(() => this.$('#is-done').trigger('change'));
assert.ok(receivedEvent, 'change event was triggered');
assert.strictEqual(receivedEvent.target, this.$('#is-done')[0]);
}
['@test events bubbling up can be prevented by returning false'](assert) {
let hasReceivedEvent;
this.registerComponent('x-foo', {
ComponentClass: Component.extend({
change() {
hasReceivedEvent = true;
},
}),
template: `{{yield}}`,
});
this.registerComponent('x-bar', {
ComponentClass: Component.extend({
change() {
return false;
},
}),
template: `<input id="is-done" type="checkbox">`,
});
this.render(`{{#x-foo}}{{x-bar}}{{/x-foo}}`);
runTask(() => this.$('#is-done').trigger('change'));
assert.notOk(hasReceivedEvent, 'change event has not been received');
}
['@test events bubbling up can be prevented by calling stopPropagation()'](assert) {
let hasReceivedEvent;
this.registerComponent('x-foo', {
ComponentClass: Component.extend({
change() {
hasReceivedEvent = true;
},
}),
template: `{{yield}}`,
});
this.registerComponent('x-bar', {
ComponentClass: Component.extend({
change(e) {
e.stopPropagation();
},
}),
template: `<input id="is-done" type="checkbox">`,
});
this.render(`{{#x-foo}}{{x-bar}}{{/x-foo}}`);
runTask(() => this.$('#is-done').trigger('change'));
assert.notOk(hasReceivedEvent, 'change event has not been received');
}
['@test event handlers are wrapped in a run loop'](assert) {
this.registerComponent('x-foo', {
ComponentClass: Component.extend({
change() {
assert.ok(getCurrentRunLoop(), 'a run loop should have started');
},
}),
template: `<input id="is-done" type="checkbox">`,
});
this.render(`{{x-foo}}`);
this.$('#is-done').trigger('click');
}
['@test delegated event listeners work for mouseEnter/Leave'](assert) {
let receivedEnterEvents = [];
let receivedLeaveEvents = [];
this.registerComponent('x-foo', {
ComponentClass: Component.extend({
mouseEnter(event) {
receivedEnterEvents.push(event);
},
mouseLeave(event) {
receivedLeaveEvents.push(event);
},
}),
template: `<div id="inner"></div>`,
});
this.render(`{{x-foo id="outer"}}`);
let parent = this.element;
let outer = this.$('#outer')[0];
let inner = this.$('#inner')[0];
// mouse moves over #outer
runTask(() => {
this.$(outer).trigger('mouseenter', { canBubble: false, relatedTarget: parent });
this.$(outer).trigger('mouseover', { relatedTarget: parent });
this.$(parent).trigger('mouseout', { relatedTarget: outer });
});
assert.equal(receivedEnterEvents.length, 1, 'mouseenter event was triggered');
assert.strictEqual(receivedEnterEvents[0].target, outer);
// mouse moves over #inner
runTask(() => {
this.$(inner).trigger('mouseover', { relatedTarget: outer });
this.$(outer).trigger('mouseout', { relatedTarget: inner });
});
assert.equal(receivedEnterEvents.length, 1, 'mouseenter event was not triggered again');
// mouse moves out of #inner
runTask(() => {
this.$(inner).trigger('mouseout', { relatedTarget: outer });
this.$(outer).trigger('mouseover', { relatedTarget: inner });
});
assert.equal(receivedLeaveEvents.length, 0, 'mouseleave event was not triggered');
// mouse moves out of #outer
runTask(() => {
this.$(outer).trigger('mouseleave', { canBubble: false, relatedTarget: parent });
this.$(outer).trigger('mouseout', { relatedTarget: parent });
this.$(parent).trigger('mouseover', { relatedTarget: outer });
});
assert.equal(receivedLeaveEvents.length, 1, 'mouseleave event was triggered');
assert.strictEqual(receivedLeaveEvents[0].target, outer);
}
['@test delegated event listeners work for mouseEnter on SVG elements'](assert) {
let receivedEnterEvents = [];
let receivedLeaveEvents = [];
this.registerComponent('x-foo', {
ComponentClass: Component.extend({
tagName: 'svg',
mouseEnter(event) {
receivedEnterEvents.push(event);
},
mouseLeave(event) {
receivedLeaveEvents.push(event);
},
}),
template: `<g id="inner"></g>`,
});
this.render(`{{x-foo id="outer"}}`);
let parent = this.element;
let outer = this.$('#outer')[0];
let inner = this.$('#inner')[0];
// mouse moves over #outer
runTask(() => {
this.$(outer).trigger('mouseenter', { canBubble: false, relatedTarget: parent });
this.$(outer).trigger('mouseover', { relatedTarget: parent });
this.$(parent).trigger('mouseout', { relatedTarget: outer });
});
assert.equal(receivedEnterEvents.length, 1, 'mouseenter event was triggered');
assert.strictEqual(receivedEnterEvents[0].target, outer);
// mouse moves over #inner
runTask(() => {
this.$(inner).trigger('mouseover', { relatedTarget: outer });
this.$(outer).trigger('mouseout', { relatedTarget: inner });
});
assert.equal(receivedEnterEvents.length, 1, 'mouseenter event was not triggered again');
// mouse moves out of #inner
runTask(() => {
this.$(inner).trigger('mouseout', { relatedTarget: outer });
this.$(outer).trigger('mouseover', { relatedTarget: inner });
});
assert.equal(receivedLeaveEvents.length, 0, 'mouseleave event was not triggered');
// mouse moves out of #outer
runTask(() => {
this.$(outer).trigger('mouseleave', { canBubble: false, relatedTarget: parent });
this.$(outer).trigger('mouseout', { relatedTarget: parent });
this.$(parent).trigger('mouseover', { relatedTarget: outer });
});
assert.equal(receivedLeaveEvents.length, 1, 'mouseleave event was triggered');
assert.strictEqual(receivedLeaveEvents[0].target, outer);
}
['@test delegated event listeners work for mouseEnter/Leave with skipped events'](assert) {
let receivedEnterEvents = [];
let receivedLeaveEvents = [];
this.registerComponent('x-foo', {
ComponentClass: Component.extend({
mouseEnter(event) {
receivedEnterEvents.push(event);
},
mouseLeave(event) {
receivedLeaveEvents.push(event);
},
}),
template: `<div id="inner"></div>`,
});
this.render(`{{x-foo id="outer"}}`);
let parent = this.element;
let outer = this.$('#outer')[0];
let inner = this.$('#inner')[0];
// we replicate fast mouse movement, where mouseover is fired directly in #inner, skipping #outer
runTask(() => {
this.$(outer).trigger('mouseenter', { canBubble: false, relatedTarget: parent });
this.$(inner).trigger('mouseover', { relatedTarget: parent });
this.$(parent).trigger('mouseout', { relatedTarget: inner });
});
assert.equal(receivedEnterEvents.length, 1, 'mouseenter event was triggered');
assert.strictEqual(receivedEnterEvents[0].target, inner);
// mouse moves out of #outer
runTask(() => {
this.$(outer).trigger('mouseleave', { canBubble: false, relatedTarget: parent });
this.$(inner).trigger('mouseout', { relatedTarget: parent });
this.$(parent).trigger('mouseover', { relatedTarget: inner });
});
assert.equal(receivedLeaveEvents.length, 1, 'mouseleave event was triggered');
assert.strictEqual(receivedLeaveEvents[0].target, inner);
}
['@test delegated event listeners work for mouseEnter/Leave with skipped events and subcomponent'](
assert
) {
let receivedEnterEvents = [];
let receivedLeaveEvents = [];
this.registerComponent('x-outer', {
ComponentClass: Component.extend({
mouseEnter(event) {
receivedEnterEvents.push(event);
},
mouseLeave(event) {
receivedLeaveEvents.push(event);
},
}),
template: `{{yield}}`,
});
this.registerComponent('x-inner', {
ComponentClass: Component.extend(),
template: ``,
});
this.render(`{{#x-outer id="outer"}}{{x-inner id="inner"}}{{/x-outer}}`);
let parent = this.element;
let outer = this.$('#outer')[0];
let inner = this.$('#inner')[0];
// we replicate fast mouse movement, where mouseover is fired directly in #inner, skipping #outer
runTask(() => {
this.$(outer).trigger('mouseenter', { canBubble: false, relatedTarget: parent });
this.$(inner).trigger('mouseover', { relatedTarget: parent });
this.$(parent).trigger('mouseout', { relatedTarget: inner });
});
assert.equal(receivedEnterEvents.length, 1, 'mouseenter event was triggered');
assert.strictEqual(receivedEnterEvents[0].target, inner);
// mouse moves out of #inner
runTask(() => {
this.$(outer).trigger('mouseleave', { canBubble: false, relatedTarget: parent });
this.$(inner).trigger('mouseout', { relatedTarget: parent });
this.$(parent).trigger('mouseover', { relatedTarget: inner });
});
assert.equal(receivedLeaveEvents.length, 1, 'mouseleave event was triggered');
assert.strictEqual(receivedLeaveEvents[0].target, inner);
}
}
);
moduleFor(
'EventDispatcher#setup',
class extends RenderingTestCase {
constructor() {
super(...arguments);
let dispatcher = this.owner.lookup('event_dispatcher:main');
run(dispatcher, 'destroy');
this.owner.__container__.reset('event_dispatcher:main');
this.dispatcher = this.owner.lookup('event_dispatcher:main');
}
['@test additional events can be specified'](assert) {
this.dispatcher.setup({ myevent: 'myEvent' });
this.registerComponent('x-foo', {
ComponentClass: Component.extend({
myEvent() {
assert.ok(true, 'custom event was triggered');
},
}),
template: `<p>Hello!</p>`,
});
this.render(`{{x-foo}}`);
this.$('div').trigger('myevent');
}
['@test a rootElement can be specified'](assert) {
this.element.innerHTML = '<div id="app"></div>';
// this.$().append('<div id="app"></div>');
this.dispatcher.setup({ myevent: 'myEvent' }, '#app');
assert.ok(this.$('#app').hasClass('ember-application'), 'custom rootElement was used');
assert.equal(this.dispatcher.rootElement, '#app', 'the dispatchers rootElement was updated');
}
['@test default events can be disabled via `customEvents`'](assert) {
this.dispatcher.setup({ click: null });
this.registerComponent('x-foo', {
ComponentClass: Component.extend({
click() {
assert.ok(false, 'click method was called');
},
null() {
assert.ok(false, 'null method was called');
},
doubleClick() {
assert.ok(true, 'a non-disabled event is still handled properly');
},
}),
template: `<p>Hello!</p>`,
});
this.render(`{{x-foo}}`);
this.$('div').trigger('click');
this.$('div').trigger('dblclick');
}
['@test throws if specified rootElement does not exist'](assert) {
assert.throws(() => {
this.dispatcher.setup({ myevent: 'myEvent' }, '#app');
});
}
}
);
if (EMBER_IMPROVED_INSTRUMENTATION) {
moduleFor(
'EventDispatcher - Instrumentation',
class extends RenderingTestCase {
teardown() {
super.teardown();
instrumentationReset();
}
['@test instruments triggered events'](assert) {
let clicked = 0;
this.registerComponent('x-foo', {
ComponentClass: Component.extend({
click() {
clicked++;
},
}),
template: `<p>hello</p>`,
});
this.render(`{{x-foo}}`);
this.$('div').trigger('click');
assert.equal(clicked, 1, 'precond - the click handler was invoked');
let clickInstrumented = 0;
instrumentationSubscribe('interaction.click', {
before() {
clickInstrumented++;
assert.equal(clicked, 1, 'invoked before event is handled');
},
after() {
clickInstrumented++;
assert.equal(clicked, 2, 'invoked after event is handled');
},
});
let keypressInstrumented = 0;
instrumentationSubscribe('interaction.keypress', {
before() {
keypressInstrumented++;
},
after() {
keypressInstrumented++;
},
});
this.$('div').trigger('click');
this.$('div').trigger('change');
assert.equal(clicked, 2, 'precond - The click handler was invoked');
assert.equal(clickInstrumented, 2, 'The click was instrumented');
assert.strictEqual(keypressInstrumented, 0, 'The keypress was not instrumented');
}
}
);
}
if (canDataTransfer) {
moduleFor(
'EventDispatcher - Event Properties',
class extends RenderingTestCase {
['@test dataTransfer property is added to drop event'](assert) {
let receivedEvent;
this.registerComponent('x-foo', {
ComponentClass: Component.extend({
drop(event) {
receivedEvent = event;
},
}),
});
this.render(`{{x-foo}}`);
fireNativeWithDataTransfer(this.$('div')[0], 'drop', 'success');
assert.equal(receivedEvent.dataTransfer, 'success');
}
}
);
}
if (jQueryDisabled) {
moduleFor(
'EventDispatcher#native-events',
class extends RenderingTestCase {
['@test native events are passed when jQuery is not present'](assert) {
let receivedEvent;
this.registerComponent('x-foo', {
ComponentClass: Component.extend({
click(event) {
receivedEvent = event;
},
}),
template: `<button id="foo">bar</button>`,
});
this.render(`{{x-foo}}`);
runTask(() => this.$('#foo').click());
assert.ok(receivedEvent, 'click event was triggered');
assert.notOk(receivedEvent.originalEvent, 'event is not a jQuery.Event');
}
}
);
} else {
moduleFor(
'EventDispatcher#jquery-events',
class extends RenderingTestCase {
beforeEach() {
this.jqueryIntegration = window.ENV._JQUERY_INTEGRATION;
}
afterEach() {
window.ENV._JQUERY_INTEGRATION = this.jqueryIntegration;
}
['@test jQuery events are passed when jQuery is present'](assert) {
let receivedEvent;
this.registerComponent('x-foo', {
ComponentClass: Component.extend({
click(event) {
receivedEvent = event;
},
}),
template: `<button id="foo">bar</button>`,
});
this.render(`{{x-foo}}`);
runTask(() => this.$('#foo').click());
assert.ok(receivedEvent, 'click event was triggered');
assert.ok(receivedEvent instanceof jQuery.Event, 'event is a jQuery.Event');
}
[`@${HAS_NATIVE_PROXY ? 'test' : 'skip'} accessing jQuery.Event#originalEvent is deprecated`](
assert
) {
let receivedEvent;
this.registerComponent('x-foo', {
ComponentClass: Component.extend({
click(event) {
receivedEvent = event;
},
}),
template: `<button id="foo">bar</button>`,
});
this.render(`{{x-foo}}`);
runTask(() => this.$('#foo').click());
expectDeprecation(() => {
let { originalEvent } = receivedEvent;
assert.ok(originalEvent, 'jQuery event has originalEvent property');
assert.equal(originalEvent.type, 'click', 'properties of originalEvent are available');
}, 'Accessing jQuery.Event specific properties is deprecated. Either use the ember-jquery-legacy addon to normalize events to native events, or explicitly opt into jQuery integration using @ember/optional-features.');
}
['@test other jQuery.Event properties do not trigger deprecation'](assert) {
let receivedEvent;
this.registerComponent('x-foo', {
ComponentClass: Component.extend({
click(event) {
receivedEvent = event;
},
}),
template: `<button id="foo">bar</button>`,
});
this.render(`{{x-foo}}`);
runTask(() => this.$('#foo').click());
expectNoDeprecation(() => {
receivedEvent.stopPropagation();
receivedEvent.stopImmediatePropagation();
receivedEvent.preventDefault();
assert.ok(receivedEvent.bubbles, 'properties of jQuery event are available');
assert.equal(receivedEvent.type, 'click', 'properties of jQuery event are available');
});
}
['@test accessing jQuery.Event#originalEvent does not trigger deprecations when jquery integration is explicitly enabled'](
assert
) {
let receivedEvent;
window.ENV._JQUERY_INTEGRATION = true;
this.registerComponent('x-foo', {
ComponentClass: Component.extend({
click(event) {
receivedEvent = event;
},
}),
template: `<button id="foo">bar</button>`,
});
this.render(`{{x-foo}}`);
runTask(() => this.$('#foo').click());
expectNoDeprecation(() => {
let { originalEvent } = receivedEvent;
assert.ok(originalEvent, 'jQuery event has originalEvent property');
assert.equal(originalEvent.type, 'click', 'properties of originalEvent are available');
});
}
[`@${
HAS_NATIVE_PROXY && DEBUG ? 'test' : 'skip'
} accessing jQuery.Event#__originalEvent does not trigger deprecations to support ember-jquery-legacy`](
assert
) {
let receivedEvent;
this.registerComponent('x-foo', {
ComponentClass: Component.extend({
click(event) {
receivedEvent = event;
},
}),
template: `<button id="foo">bar</button>`,
});
this.render(`{{x-foo}}`);
runTask(() => this.$('#foo').click());
expectNoDeprecation(() => {
let { __originalEvent: originalEvent } = receivedEvent;
assert.ok(originalEvent, 'jQuery event has __originalEvent property');
assert.equal(originalEvent.type, 'click', 'properties of __originalEvent are available');
});
}
}
);
}