Repository navigation
Expand file tree
/
Copy pathevents.js
More file actions
584 lines (520 loc) · 24.6 KB
/
Copy pathevents.js
File metadata and controls
584 lines (520 loc) · 24.6 KB
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
"use strict";
var testcase = require('nodeunit').testCase;
var jsdom = require("../..");
class EventMonitor {
constructor() {
this.atEvents = [];
this.bubbledEvents = [];
this.capturedEvents = [];
this.allEvents = [];
this.handleEvent = function (event) {
this.allEvents.push(event);
switch (event.eventPhase) {
case event.CAPTURING_PHASE:
this.capturedEvents.push(event);
break;
case event.AT_TARGET:
this.atEvents.push(event);
break;
case event.BUBBLING_PHASE:
this.bubbledEvents.push(event);
break;
default:
throw new Error("Unspecified event phase");
}
}.bind(this);
}
}
var _setUp = function() {
var doc = require('../level1/core/files/hc_staff.xml').hc_staff();
var monitor = this.monitor = new EventMonitor();
this.win = doc.defaultView;
this.title = doc.getElementsByTagName("title").item(0);
this.body = doc.getElementsByTagName("body").item(0);
this.plist = doc.getElementsByTagName("p").item(0);
this.event = doc.createEvent("Events");
this._handleEvent = function(type) { return(function(event) { event[type](); monitor.handleEvent(event) }); }
}
var _tearDown = function(xs) {
xs = ['monitor', 'title', 'body', 'plist', 'event', '_handleEvent'].concat(xs ? xs : []);
var self = this;
xs.forEach(function(x){
self[x] = undefined;
delete(self[x]);
})
}
// A document is created using implementation.createDocument and cast to a DocumentEvent interface.
// @author Curt Arnold
// @see http://www.w3.org/TR/DOM-Level-2-Events/events#Events-DocumentEvent
exports['DocumentEvent interface'] = function (test) {
var doc = require('../level1/core/files/hc_staff.xml').hc_staff();
test.ok((doc.createEvent instanceof Function), "should have createEvent function");
test.done();
}
// A document is created using implementation.createDocument and cast to a EventTarget interface.
// @author Curt Arnold
// @see http://www.w3.org/TR/DOM-Level-2-Events/events#Events-EventTarget
exports['EventTarget interface'] = function (test) {
var doc = require('../level1/core/files/hc_staff.xml').hc_staff();
test.ok((doc instanceof doc.defaultView.EventTarget), 'should be an instance of EventTarget');
test.done();
}
// An object implementing the Event interface is created by using DocumentEvent.createEvent method with an eventType
// @author Curt Arnold
// @see http://www.w3.org/TR/DOM-Level-2-Events/events#Events-DocumentEvent-createEvent
exports['create event with each event type'] = function(test){
var doc = require('../level1/core/files/hc_staff.xml').hc_staff(),
event_types = {'Events': doc.defaultView.Event,
'MutationEvents': doc.defaultView.MutationEvent,
'UIEvents': doc.defaultView.UIEvent,
'MouseEvents': doc.defaultView.MouseEvent ,
'HTMLEvents': doc.defaultView.Event};
test.expect(10);
for (var type in event_types) {
var event = doc.createEvent(type);
test.notEqual(event, null, "should not be null for " + type);
test.ok((event instanceof event_types[type]),"should be instanceof " + type);
}
test.done();
}
// @see https://dom.spec.whatwg.org/#interface-event
exports['event interface eventInit parameter'] = function (test) {
var doc = jsdom.jsdom();
testEventConstructor(doc.defaultView.Event);
testEventConstructor(doc.defaultView.UIEvent);
testEventConstructor(doc.defaultView.MouseEvent);
test.throws(function () {
new doc.defaultView.MutationEvent('myevent');
}, TypeError);
test.done();
function testEventConstructor(Constructor) {
test.throws(function () {
new Constructor('myevent', 'not a dictionary');
}, TypeError);
var event = new Constructor('myevent');
test.equal(event.bubbles, false);
test.equal(event.cancelable, false);
event = new Constructor('myevent', null);
test.equal(event.bubbles, false);
test.equal(event.cancelable, false);
event = new Constructor('myevent', { bubbles: true });
test.equal(event.bubbles, true);
test.equal(event.cancelable, false);
event = new Constructor('myevent', { cancelable: true });
test.equal(event.bubbles, false);
test.equal(event.cancelable, true);
event = new Constructor('myevent', { bubbles: true, cancelable: true });
test.equal(event.bubbles, true);
test.equal(event.cancelable, true);
event = new Constructor('myevent', {});
test.equal(event.bubbles, false);
test.equal(event.cancelable, false);
event = new Constructor('myevent', { bubbles: null, cancelable: null });
test.equal(event.bubbles, false);
test.equal(event.cancelable, false);
event = new Constructor('myevent', { bubbles: 0, cancelable: 0 });
test.equal(event.bubbles, false);
test.equal(event.cancelable, false);
// values > 0 are considered true
event = new Constructor('myevent', { bubbles: 1, cancelable: 1 });
test.equal(event.bubbles, true);
test.equal(event.cancelable, true);
// values > 0 are considered true
event = new Constructor('myevent', { bubbles: 10, cancelable: 10 });
test.equal(event.bubbles, true);
test.equal(event.cancelable, true);
// empty string is considered false
event = new Constructor('myevent', { bubbles: "", cancelable: "" });
test.equal(event.bubbles, false);
test.equal(event.cancelable, false);
// non empty string is considered true
event = new Constructor('myevent', { bubbles: "false", cancelable: "false" });
test.equal(event.bubbles, true);
test.equal(event.cancelable, true);
// non empty string is considered true
event = new Constructor('myevent', { bubbles: "true", cancelable: "true" });
test.equal(event.bubbles, true);
test.equal(event.cancelable, true);
}
}
// @author Curt Arnold
// @see http://www.w3.org/TR/DOM-Level-2-Events/events#Events-EventTarget-dispatchEvent
// @see http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-17189187
exports['dispatch event'] = testcase({
setUp: function(cb){
this.doc = require('../level1/core/files/hc_staff.xml').hc_staff();
cb();
},
tearDown: function(cb){
_tearDown.call(this, 'doc');
cb();
},
'a null reference passed to dispatchEvent': function (test) {
var doc = this.doc;
test.throws(function(){ doc.dispatchEvent(null) }, TypeError, 'should throw an exception');
// TODO: figure out the best way to test (exception.code == 0) and (exception.message == 'Null event')
test.done();
},
'a created but not initialized event passed to dispatchEvent': function (test) {
var doc = this.doc,
event_types = ['Events', 'MutationEvents', 'UIEvents', 'MouseEvents', 'HTMLEvents'];
test.expect(event_types.length);
event_types.forEach(function(type){
test.throws(function(){ doc.dispatchEvent(doc.createEvent(type)) }, doc.defaultView.DOMException, 'should throw an exception for ' + type);
})
test.done();
},
// An EventListener registered on the target node with capture false, should receive any event fired on that node.
'EventListener with capture false': function (test) {
var monitor = new EventMonitor();
this.doc.addEventListener("foo", monitor.handleEvent, false);
var event = this.doc.createEvent("Events");
event.initEvent("foo",true,false);
test.expect(7);
test.strictEqual(event.eventPhase, 0);
test.strictEqual(event.currentTarget, null);
this.doc.dispatchEvent(event);
test.equal(monitor.atEvents.length, 1, 'should receive atEvent');
test.equal(monitor.bubbledEvents.length, 0, 'should not receive at bubble phase');
test.equal(monitor.capturedEvents.length, 0, 'should not receive at capture phase');
test.strictEqual(event.eventPhase, 0);
test.strictEqual(event.currentTarget, null);
test.done();
},
// An EventListener registered on the target node with capture true, should receive any event fired on that node.
'EventListener with capture true': function (test) {
var monitor = new EventMonitor();
this.doc.addEventListener("foo", monitor.handleEvent, true);
var event = this.doc.createEvent("Events");
event.initEvent("foo",true,false);
test.expect(7);
test.strictEqual(event.eventPhase, 0);
test.strictEqual(event.currentTarget, null);
this.doc.dispatchEvent(event);
test.equal(monitor.atEvents.length, 1, 'should receive atEvent');
test.equal(monitor.bubbledEvents.length, 0, 'should not receive at bubble phase');
test.equal(monitor.capturedEvents.length, 0, 'should not receive at capture phase');
test.strictEqual(event.eventPhase, 0);
test.strictEqual(event.currentTarget, null);
test.done();
},
// The same monitor is registered twice and an event is dispatched. The monitor should receive only one handleEvent call.
'EventListener is registered twice': function (test) {
var monitor = new EventMonitor();
this.doc.addEventListener("foo", monitor.handleEvent, false);
this.doc.addEventListener("foo", monitor.handleEvent, false);
var event = this.doc.createEvent("Events");
event.initEvent("foo",true,false);
this.doc.dispatchEvent(event);
test.expect(3);
test.equal(monitor.atEvents.length, 1, 'should receive atEvent only once');
test.equal(monitor.bubbledEvents.length, 0, 'should not receive at bubble phase');
test.equal(monitor.capturedEvents.length, 0, 'should not receive at capture phase');
test.done();
},
// The same monitor is registered twice, removed once, and an event is dispatched. The monitor should receive only no handleEvent calls.
'EventListener is registered twice, removed once': function (test) {
var monitor = new EventMonitor();
this.doc.addEventListener("foo", monitor.handleEvent, false);
this.doc.addEventListener("foo", monitor.handleEvent, false);
this.doc.removeEventListener("foo", monitor.handleEvent, false);
var event = this.doc.createEvent("Events");
event.initEvent("foo",true,false);
this.doc.dispatchEvent(event);
test.equal(monitor.allEvents.length, 0, 'should not receive any handleEvent calls');
test.done();
},
// A monitor is added, multiple calls to removeEventListener are made with similar but not identical arguments, and an event is dispatched.
// The monitor should receive handleEvent calls.
'EventListener is registered, other listeners (similar but not identical) are removed': function (test) {
var monitor = new EventMonitor();
var other = {handleEvent: function(){}}
this.doc.addEventListener("foo", monitor.handleEvent, false);
this.doc.removeEventListener("foo", monitor.handleEvent, true);
this.doc.removeEventListener("food", monitor.handleEvent, false);
this.doc.removeEventListener("foo", other.handleEvent, false);
var event = this.doc.createEvent("Events");
event.initEvent("foo",true,false);
this.doc.dispatchEvent(event);
test.equal(monitor.allEvents.length, 1, 'should still receive the handleEvent call');
test.done();
},
// Two listeners are registered on the same target, each of which will remove both itself and the other on the first event. Only one should see the event since event listeners can never be invoked after being removed.
'two EventListeners which both handle by unregistering itself and the other': function (test) {
// setup
var es = [];
var ls = [];
var EventListener1 = function() { ls.push(this); }
var EventListener2 = function() { ls.push(this); }
EventListener1.prototype.handleEvent = function(event) { _handleEvent(event); }
EventListener2.prototype.handleEvent = function(event) { _handleEvent(event); }
var _handleEvent = function(event) {
es.push(event);
ls.forEach(function(l){
event.currentTarget.removeEventListener("foo", l.handleEvent, false);
})
}
// test
var listener1 = new EventListener1();
var listener2 = new EventListener2();
this.doc.addEventListener("foo", listener1.handleEvent, false);
this.doc.addEventListener("foo", listener2.handleEvent, false);
var event = this.doc.createEvent("Events");
event.initEvent("foo",true,false);
this.doc.dispatchEvent(event);
test.equal(es.length, 1, 'should only be handled by one EventListener');
test.done();
}
})
// The Event.initEvent method is called for event returned by DocumentEvent.createEvent("Events") and DocumentEvent.createEvent("MutationEvents")
// The state is checked to see if it reflects the parameters.
// @author Curt Arnold
// @see http://www.w3.org/TR/DOM-Level-2-Events/events#Events-Event-initEvent
exports['init event'] = testcase({
setUp: function(cb){
var doc = require('../level1/core/files/hc_staff.xml').hc_staff();
this._events = ['Events', 'MutationEvents'].map(function(t){ return(doc.createEvent(t)); })
cb();
},
tearDown: function(cb){
_tearDown.call(this, '_events');
cb();
},
'set state from params, bubble no cancel': function (test) {
test.expect(8);
this._events.forEach(function(event){
test.notEqual(event, null, 'event should not be null for ' + event.eventType);
event.initEvent('rotate', true, false);
test.equal(event.type, 'rotate', 'event type should be \"rotate\" for ' + event.eventType);
test.equal(event.bubbles, true, 'event should bubble for ' + event.eventType);
test.equal(event.cancelable, false, 'event should not be cancelable for ' + event.eventType);
})
test.done();
},
'set state from params, cancel no bubble': function (test) {
test.expect(8);
this._events.forEach(function(event){
test.notEqual(event, null, 'event should not be null for' + event.eventType);
event.initEvent('rotate', false, true);
test.equal(event.type, 'rotate', 'event type should be \"rotate\" for ' + event.eventType);
test.equal(event.bubbles, false, 'event should not bubble for ' + event.eventType);
test.equal(event.cancelable, true, 'event should be cancelable for ' + event.eventType);
})
test.done();
},
'initEvent called multiple times, final time is definitive': function (test) {
test.expect(14);
this._events.forEach(function(event){
test.notEqual(event, null, 'event should not be null for ' + event.eventType);
// rotate
event.initEvent("rotate", true, true);
test.equal(event.type, 'rotate', 'event type should be \"rotate\" for ' + event.eventType);
test.equal(event.bubbles, true, 'event should bubble for ' + event.eventType);
test.equal(event.cancelable, true, 'event should be cancelable for ' + event.eventType);
// shear
event.initEvent("shear", false, false);
test.equal(event.type, 'shear', 'event type should be \"shear\" for ' + event.eventType);
test.equal(event.bubbles, false, 'event should not bubble for ' + event.eventType);
test.equal(event.cancelable, false, 'event should not be cancelable for ' + event.eventType);
})
test.done();
},
})
exports['capture event'] = testcase({
setUp: function(cb){
_setUp.call(this);
this.event.initEvent("foo",true,false);
cb();
},
tearDown: function(cb){
_tearDown.call(this);
cb();
},
'all capturing listeners in a direct line from dispatched node will receive the event': function(test) {
this.plist.addEventListener("foo", this.monitor.handleEvent, true);
this.plist.firstChild.addEventListener("foo", this.monitor.handleEvent, false);
this.plist.firstChild.dispatchEvent(this.event);
test.expect(3);
test.equal(this.monitor.atEvents.length, 1, 'should be at 1 event');
test.equal(this.monitor.bubbledEvents.length, 0, 'should not have any bubbled events');
test.equal(this.monitor.capturedEvents.length, 1, 'should have captured 1 event');
test.done();
},
'only capture listeners in a direct line from target to the document node should receive the event': function(test) {
var self = this;
this.title.addEventListener("foo", this.monitor.handleEvent, true);
this.plist.addEventListener("foo", function(event) { event.preventDefault(); self.monitor.handleEvent(event) }, false);
this.plist.firstChild.addEventListener("foo", this.monitor.handleEvent, false);
var return_val = this.plist.firstChild.dispatchEvent(this.event);
test.expect(4);
test.equal(return_val, true, 'dispatchEvent should return *true*');
test.equal(this.monitor.atEvents.length, 1, 'should be at 1 event');
test.equal(this.monitor.bubbledEvents.length, 1, 'should have bubbled 1 event');
test.equal(this.monitor.capturedEvents.length, 0, 'should not have captured any events');
test.done();
}
})
exports['bubble event'] = testcase({
setUp: function(cb){
_setUp.call(this);
this.event.initEvent("foo",true,false);
cb();
},
tearDown: function(cb){
_tearDown.call(this);
cb();
},
'all non-capturing listeners in a direct line from dispatched node will receive a bubbling event': function(test) {
this.win.addEventListener("foo", this.monitor.handleEvent, false);
this.plist.addEventListener("foo", this.monitor.handleEvent, false);
this.plist.firstChild.addEventListener("foo", th
8000
is.monitor.handleEvent, false);
this.plist.firstChild.dispatchEvent(this.event);
test.expect(3);
test.equal(this.monitor.atEvents.length, 1, 'should be at 1 event');
test.equal(this.monitor.bubbledEvents.length, 2, 'should have 2 bubbled events');
test.equal(this.monitor.capturedEvents.length, 0, 'should not have any captured events');
test.done();
},
'only bubble listeners in a direct line from target to the document node should receive the event': function(test) {
this.win.addEventListener("foo", this.monitor.handleEvent, false);
this.title.addEventListener("foo", this.monitor.handleEvent, false);
this.plist.addEventListener("foo", this._handleEvent('preventDefault'), true);
this.plist.firstChild.addEventListener("foo", this.monitor.handleEvent, false);
var return_val = this.plist.firstChild.dispatchEvent(this.event);
test.expect(4);
test.equal(return_val, true, 'dispatchEvent should return *true*');
test.equal(this.monitor.atEvents.length, 1, 'should be at 1 event');
test.equal(this.monitor.bubbledEvents.length, 1, 'should have 1 bubbled event');
test.equal(this.monitor.capturedEvents.length, 1, 'should have captured 1 event');
test.done();
},
'if an event does not bubble, bubble listeners should not receive the event': function(test) {
this.win.addEventListener("foo", this.monitor.handleEvent, false);
this.body.addEventListener("foo", this.monitor.handleEvent, true);
this.plist.addEventListener("foo", this._handleEvent('preventDefault'), false);
this.plist.firstChild.addEventListener("foo", this.monitor.handleEvent, false);
this.event.initEvent("foo",false,false);
var return_val = this.plist.firstChild.dispatchEvent(this.event);
test.expect(4);
test.equal(return_val, true, 'dispatchEvent should return *true*');
test.equal(this.monitor.atEvents.length, 1, 'should be at 1 event');
test.equal(this.monitor.bubbledEvents.length, 0, 'should not have any bubbled events');
test.equal(this.monitor.capturedEvents.length, 1, 'should have captured 1 event');
test.done();
}
})
exports['stop propagation'] = testcase({
setUp: function(cb){
_setUp.call(this);
this.event.initEvent("foo",true,false);
cb();
},
tearDown: function(cb){
_tearDown.call(this);
cb();
},
'should prevent the target from receiving the event': function(test) {
this.win.addEventListener("foo", this.monitor.handleEvent, false);
this.plist.addEventListener("foo", this._handleEvent('stopPropagation'), true);
this.plist.firstChild.addEventListener("foo", this.monitor.handleEvent, false);
this.plist.firstChild.dispatchEvent(this.event);
test.expect(3);
test.equal(this.monitor.atEvents.length, 0, 'should be at 0 events');
test.equal(this.monitor.bubbledEvents.length, 0, 'should have no bubbled events');
test.equal(this.monitor.capturedEvents.length, 1, 'should have 1 captured event');
test.done();
},
'should prevent all listeners from receiving the event': function(test) {
this.win.addEventListener("foo", this.monitor.handleEvent, false);
this.body.addEventListener("foo", this.monitor.handleEvent, false);
this.plist.addEventListener("foo", this._handleEvent('stopPropagation'), false);
this.plist.firstChild.addEventListener("foo", this.monitor.handleEvent, false);
this.plist.firstChild.dispatchEvent(this.event);
test.expect(3);
test.equal(this.monitor.atEvents.length, 1, 'should be at 1 event');
test.equal(this.monitor.bubbledEvents.length, 1, 'should have 1 bubbled event');
test.equal(this.monitor.capturedEvents.length, 0, 'should have no captured events');
test.done();
},
'stopPropagation should not prevent listeners on the same element from receiving the event': function(test) {
this.win.addEventListener("foo", this.monitor.handleEvent, false);
this.body.addEventListener("foo", this.monitor.handleEvent, false);
this.plist.addEventListener("foo", this._handleEvent('stopPropagation'), true);
this.plist.addEventListener("foo", this._handleEvent('stopPropagation'), false);
this.plist.addEventListener("foo", this.monitor.handleEvent, true);
this.plist.addEventListener("foo", this.monitor.handleEvent, false);
this.plist.dispatchEvent(this.event);
test.expect(3);
test.equal(this.monitor.atEvents.length, 4, 'should be at 4 events');
test.equal(this.monitor.bubbledEvents.length, 0, 'should have no bubbled events');
test.equal(this.monitor.capturedEvents.length, 0, 'should have no captured events');
test.done();
}
})
exports['prevent default'] = testcase({
setUp: function(cb){
_setUp.call(this);
this.event.initEvent("foo",true,true);
cb();
},
tearDown: function(cb){
_tearDown.call(this);
cb();
},
'the defaultPrevented flag is set when the event is prevented': function(test) {
this.title.addEventListener("foo", function(event) { event.preventDefault();}, false);
var return_val = this.title.dispatchEvent(this.event);
test.equal(this.event.defaultPrevented, true, 'the defaultPrevented flag should be true when the event is prevented');
test.done();
},
'the defaultPrevented flag is not set when the event is not prevented': function(test) {
this.title.addEventListener("foo", this.monitor.handleEvent, false);
var return_val = this.title.dispatchEvent(this.event);
test.equal(this.event.defaultPrevented, false, 'the defaultPrevented flag should be false when the event is not prevented');
test.done();
},
'a cancelable event can have its default event disabled': function(test) {
this.bod
61F1
y.addEventListener("foo", this.monitor.handleEvent, true);
this.plist.addEventListener("foo", this._handleEvent('preventDefault'), false);
this.plist.firstChild.addEventListener("foo", this.monitor.handleEvent, false);
var return_val = this.plist.firstChild.dispatchEvent(this.event);
test.expect(4);
test.equal(return_val, false, 'dispatchEvent should return *false*');
test.equal(this.monitor.atEvents.length, 1, 'should be at 1 event');
test.equal(this.monitor.bubbledEvents.length, 1, 'should have bubbled 1 event');
test.equal(this.monitor.capturedEvents.length, 1, 'should have captured 1 event');
test.done();
},
'a non-cancelable event cannot have its default event disabled': function(test) {
this.body.addEventListener("foo", this.monitor.handleEvent, true);
this.plist.addEventListener("foo", this._handleEvent('preventDefault'), false);
this.plist.firstChild.addEventListener("foo", this.monitor.handleEvent, false);
this.event.initEvent("foo",true,false);
var return_val = this.plist.firstChild.dispatchEvent(this.event);
test.expect(4);
test.equal(return_val, true, 'dispatchEvent should return *true*');
test.equal(this.monitor.atEvents.length, 1, 'should be at 1 event');
test.equal(this.monitor.bubbledEvents.length, 1, 'should have bubbled 1 event');
test.equal(this.monitor.capturedEvents.length, 1, 'should have captured 1 event');
test.done();
}
})
exports["remove listener in handler"] = t => {
const document = jsdom.jsdom();
let h1 = 0, h2 = 0;
document.addEventListener("click", function handler1() {
// Event handler that removes itself
h1++;
document.removeEventListener("click", handler1);
});
document.addEventListener("click", () => h2++);
const ev = document.createEvent("MouseEvents");
ev.initEvent("click", true, true);
document.dispatchEvent(ev);
t.strictEqual(h1, 1, "handler1 must be called once");
t.strictEqual(h2, 1, "handler2 must be called once");
document.dispatchEvent(ev);
t.strictEqual(h1, 1, "handler1 must be called once");
t.strictEqual(h2, 2, "handler2 must be called twice");
t.done();
};