1
1
/* eslint-env qunit */
2
- import document from 'global/document' ;
3
- import window from 'global/window' ;
4
2
import sinon from 'sinon' ;
5
3
import evented from '../../../src/js/mixins/evented' ;
6
4
import * as Dom from '../../../src/js/utils/dom' ;
@@ -13,20 +11,6 @@ const errors = {
13
11
target : new Error ( 'Invalid target; must be a DOM node or evented object.' )
14
12
} ;
15
13
16
- // Cross-browser event creation.
17
- const createEvent = ( type ) => {
18
- let event ;
19
-
20
- try {
21
- event = new window . Event ( type ) ;
22
- } catch ( x ) {
23
- event = document . createEvent ( 'Event' ) ;
24
- event . initEvent ( type , true , true ) ;
25
- }
26
-
27
- return event ;
28
- } ;
29
-
30
14
const validateListenerCall = ( call , thisValue , eventExpectation ) => {
31
15
const eventActual = call . args [ 0 ] ;
32
16
@@ -47,13 +31,7 @@ QUnit.module('mixins: evented', {
47
31
} ,
48
32
49
33
afterEach ( ) {
50
- Object . keys ( this . targets ) . forEach ( k => {
51
- if ( typeof this . targets [ k ] . trigger === 'function' ) {
52
- this . targets [ k ] . trigger ( 'dispose' ) ;
53
- } else {
54
- this . targets [ k ] = null ;
55
- }
56
- } ) ;
34
+ Object . keys ( this . targets ) . forEach ( k => this . targets [ k ] . trigger ( 'dispose' ) ) ;
57
35
}
58
36
} ) ;
59
37
@@ -83,7 +61,7 @@ QUnit.test('evented() with custom element', function(assert) {
83
61
) ;
84
62
} ) ;
85
63
86
- QUnit . test ( 'on() and one() error states ' , function ( assert ) {
64
+ QUnit . test ( 'on() and one() errors ' , function ( assert ) {
87
65
const target = this . targets . a = evented ( { } ) ;
88
66
89
67
[ 'on' , 'one' ] . forEach ( method => {
@@ -96,7 +74,7 @@ QUnit.test('on() and one() error states', function(assert) {
96
74
} ) ;
97
75
} ) ;
98
76
99
- QUnit . test ( 'off() error states ' , function ( assert ) {
77
+ QUnit . test ( 'off() errors ' , function ( assert ) {
100
78
const target = this . targets . a = evented ( { } ) ;
101
79
102
80
// An invalid event actually causes an invalid target error because it
@@ -108,7 +86,7 @@ QUnit.test('off() error states', function(assert) {
108
86
assert . throws ( ( ) => target . off ( evented ( { } ) , 'x' , null ) , errors . listener , 'the expected error is thrown' ) ;
109
87
} ) ;
110
88
111
- QUnit . test ( 'on() can add a listener to one event type on itself ' , function ( assert ) {
89
+ QUnit . test ( 'on() can add a listener to one event type on this object ' , function ( assert ) {
112
90
const a = this . targets . a = evented ( { } ) ;
113
91
const spy = sinon . spy ( ) ;
114
92
@@ -123,7 +101,7 @@ QUnit.test('on() can add a listener to one event type on itself', function(asser
123
101
} ) ;
124
102
} ) ;
125
103
126
- QUnit . test ( 'on() can add a listener to an array of event types on itself ' , function ( assert ) {
104
+ QUnit . test ( 'on() can add a listener to an array of event types on this object ' , function ( assert ) {
127
105
const a = this . targets . a = evented ( { } ) ;
128
106
const spy = sinon . spy ( ) ;
129
107
@@ -144,7 +122,7 @@ QUnit.test('on() can add a listener to an array of event types on itself', funct
144
122
} ) ;
145
123
} ) ;
146
124
147
- QUnit . test ( 'one() can add a listener to one event type on itself ' , function ( assert ) {
125
+ QUnit . test ( 'one() can add a listener to one event type on this object ' , function ( assert ) {
148
126
const a = this . targets . a = evented ( { } ) ;
149
127
const spy = sinon . spy ( ) ;
150
128
@@ -160,7 +138,7 @@ QUnit.test('one() can add a listener to one event type on itself', function(asse
160
138
} ) ;
161
139
} ) ;
162
140
163
- QUnit . test ( 'one() can add a listener to an array of event types on itself ' , function ( assert ) {
141
+ QUnit . test ( 'one() can add a listener to an array of event types on this object ' , function ( assert ) {
164
142
const a = this . targets . a = evented ( { } ) ;
165
143
const spy = sinon . spy ( ) ;
166
144
@@ -183,7 +161,7 @@ QUnit.test('one() can add a listener to an array of event types on itself', func
183
161
} ) ;
184
162
} ) ;
185
163
186
- QUnit . test ( 'on() can add a listener to one event type on a different evented object' , function ( assert ) {
164
+ QUnit . test ( 'on() can add a listener to one event type on a different target object' , function ( assert ) {
187
165
const a = this . targets . a = evented ( { } ) ;
188
166
const b = this . targets . b = evented ( { } ) ;
189
167
const spy = sinon . spy ( ) ;
@@ -202,47 +180,7 @@ QUnit.test('on() can add a listener to one event type on a different evented obj
202
180
} ) ;
203
181
} ) ;
204
182
205
- QUnit . test ( 'on() can add a listener to one event type on a node object' , function ( assert ) {
206
- const a = this . targets . a = evented ( { } ) ;
207
- const b = this . targets . b = Dom . createEl ( 'div' ) ;
208
- const spy = sinon . spy ( ) ;
209
- const event = createEvent ( 'x' ) ;
210
-
211
- a . on ( b , 'x' , spy ) ;
212
- b . dispatchEvent ( event ) ;
213
-
214
- // Make sure we aren't magically binding a listener to "a".
215
- a . trigger ( 'x' ) ;
216
-
217
- assert . strictEqual ( spy . callCount , 1 , 'the listener was called the expected number of times' ) ;
218
-
219
- validateListenerCall ( spy . getCall ( 0 ) , a , {
220
- type : 'x' ,
221
- target : b
222
- } ) ;
223
- } ) ;
224
-
225
- QUnit . test ( 'on() can add a listener to one event type on the window object' , function ( assert ) {
226
- const a = this . targets . a = evented ( { } ) ;
227
- const b = this . targets . b = window ;
228
- const spy = sinon . spy ( ) ;
229
- const event = createEvent ( 'x' ) ;
230
-
231
- a . on ( b , 'x' , spy ) ;
232
- b . dispatchEvent ( event ) ;
233
-
234
- // Make sure we aren't magically binding a listener to "a".
235
- a . trigger ( 'x' ) ;
236
-
237
- assert . strictEqual ( spy . callCount , 1 , 'the listener was called the expected number of times' ) ;
238
-
239
- validateListenerCall ( spy . getCall ( 0 ) , a , {
240
- type : 'x' ,
241
- target : b
242
- } ) ;
243
- } ) ;
244
-
245
- QUnit . test ( 'on() can add a listener to an array of event types on a different evented object' , function ( assert ) {
183
+ QUnit . test ( 'on() can add a listener to an array of event types on a different target object' , function ( assert ) {
246
184
const a = this . targets . a = evented ( { } ) ;
247
185
const b = this . targets . b = evented ( { } ) ;
248
186
const spy = sinon . spy ( ) ;
@@ -268,70 +206,13 @@ QUnit.test('on() can add a listener to an array of event types on a different ev
268
206
} ) ;
269
207
} ) ;
270
208
271
- QUnit . test ( 'on() can add a listener to an array of event types on a node object' , function ( assert ) {
272
- const a = this . targets . a = evented ( { } ) ;
273
- const b = this . targets . b = Dom . createEl ( 'div' ) ;
274
- const spy = sinon . spy ( ) ;
275
- const x = createEvent ( 'x' ) ;
276
- const y = createEvent ( 'y' ) ;
277
-
278
- a . on ( b , [ 'x' , 'y' ] , spy ) ;
279
- b . dispatchEvent ( x ) ;
280
- b . dispatchEvent ( y ) ;
281
-
282
- // Make sure we aren't magically binding a listener to "a".
283
- a . trigger ( 'x' ) ;
284
- a . trigger ( 'y' ) ;
285
-
286
- assert . strictEqual ( spy . callCount , 2 , 'the listener was called the expected number of times' ) ;
287
-
288
- validateListenerCall ( spy . getCall ( 0 ) , a , {
289
- type : 'x' ,
290
- target : b
291
- } ) ;
292
-
293
- validateListenerCall ( spy . getCall ( 1 ) , a , {
294
- type : 'y' ,
295
- target : b
296
- } ) ;
297
- } ) ;
298
-
299
- QUnit . test ( 'on() can add a listener to an array of event types on the window object' , function ( assert ) {
300
- const a = this . targets . a = evented ( { } ) ;
301
- const b = this . targets . b = window ;
302
- const spy = sinon . spy ( ) ;
303
- const x = createEvent ( 'x' ) ;
304
- const y = createEvent ( 'y' ) ;
305
-
306
- a . on ( b , [ 'x' , 'y' ] , spy ) ;
307
- b . dispatchEvent ( x ) ;
308
- b . dispatchEvent ( y ) ;
309
-
310
- // Make sure we aren't magically binding a listener to "a".
311
- a . trigger ( 'x' ) ;
312
- a . trigger ( 'y' ) ;
313
-
314
- assert . strictEqual ( spy . callCount , 2 , 'the listener was called the expected number of times' ) ;
315
-
316
- validateListenerCall ( spy . getCall ( 0 ) , a , {
317
- type : 'x' ,
318
- target : b
319
- } ) ;
320
-
321
- validateListenerCall ( spy . getCall ( 1 ) , a , {
322
- type : 'y' ,
323
- target : b
324
- } ) ;
325
- } ) ;
326
-
327
- QUnit . test ( 'one() can add a listener to one event type on a different evented object' , function ( assert ) {
209
+ QUnit . test ( 'one() can add a listener to one event type on a different target object' , function ( assert ) {
328
210
const a = this . targets . a = evented ( { } ) ;
329
211
const b = this . targets . b = evented ( { } ) ;
330
212
const spy = sinon . spy ( ) ;
331
213
332
214
a . one ( b , 'x' , spy ) ;
333
215
b . trigger ( 'x' ) ;
334
- b . trigger ( 'x' ) ;
335
216
336
217
// Make sure we aren't magically binding a listener to "a".
337
218
a . trigger ( 'x' ) ;
@@ -344,49 +225,9 @@ QUnit.test('one() can add a listener to one event type on a different evented ob
344
225
} ) ;
345
226
} ) ;
346
227
347
- QUnit . test ( 'one() can add a listener to one event type on a node object' , function ( assert ) {
348
- const a = this . targets . a = evented ( { } ) ;
349
- const b = this . targets . b = Dom . createEl ( 'div' ) ;
350
- const spy = sinon . spy ( ) ;
351
- const event = createEvent ( 'x' ) ;
352
-
353
- a . one ( b , 'x' , spy ) ;
354
- b . dispatchEvent ( event ) ;
355
- b . dispatchEvent ( event ) ;
356
-
357
- // Make sure we aren't magically binding a listener to "a".
358
- a . trigger ( 'x' ) ;
359
-
360
- assert . strictEqual ( spy . callCount , 1 , 'the listener was called the expected number of times' ) ;
361
-
362
- validateListenerCall ( spy . getCall ( 0 ) , a , {
363
- type : 'x' ,
364
- target : b
365
- } ) ;
366
- } ) ;
367
-
368
- QUnit . test ( 'one() can add a listener to one event type on the window object' , function ( assert ) {
369
- const a = this . targets . a = evented ( { } ) ;
370
- const b = this . targets . b = window ;
371
- const spy = sinon . spy ( ) ;
372
- const event = createEvent ( 'x' ) ;
373
-
374
- a . one ( b , 'x' , spy ) ;
375
- b . dispatchEvent ( event ) ;
376
- b . dispatchEvent ( event ) ;
377
-
378
- // Make sure we aren't magically binding a listener to "a".
379
- a . trigger ( 'x' ) ;
380
-
381
- assert . strictEqual ( spy . callCount , 1 , 'the listener was called the expected number of times' ) ;
382
-
383
- validateListenerCall ( spy . getCall ( 0 ) , a , {
384
- type : 'x' ,
385
- target : b
386
- } ) ;
387
- } ) ;
388
-
389
- QUnit . test ( 'one() can add a listener to an array of event types on a different evented object' , function ( assert ) {
228
+ // The behavior here unfortunately differs from the identical case where "a"
229
+ // listens to itself. This is something that should be resolved...
230
+ QUnit . test ( 'one() can add a listener to an array of event types on a different target object' , function ( assert ) {
390
231
const a = this . targets . a = evented ( { } ) ;
391
232
const b = this . targets . b = evented ( { } ) ;
392
233
const spy = sinon . spy ( ) ;
@@ -409,56 +250,6 @@ QUnit.test('one() can add a listener to an array of event types on a different e
409
250
} ) ;
410
251
} ) ;
411
252
412
- QUnit . test ( 'one() can add a listener to an array of event types on a node object' , function ( assert ) {
413
- const a = this . targets . a = evented ( { } ) ;
414
- const b = this . targets . b = Dom . createEl ( 'div' ) ;
415
- const spy = sinon . spy ( ) ;
416
- const x = createEvent ( 'x' ) ;
417
- const y = createEvent ( 'y' ) ;
418
-
419
- a . one ( b , [ 'x' , 'y' ] , spy ) ;
420
- b . dispatchEvent ( x ) ;
421
- b . dispatchEvent ( y ) ;
422
- b . dispatchEvent ( x ) ;
423
- b . dispatchEvent ( y ) ;
424
-
425
- // Make sure we aren't magically binding a listener to "a".
426
- a . trigger ( 'x' ) ;
427
- a . trigger ( 'y' ) ;
428
-
429
- assert . strictEqual ( spy . callCount , 1 , 'the listener was called the expected number of times' ) ;
430
-
431
- validateListenerCall ( spy . getCall ( 0 ) , a , {
432
- type : 'x' ,
433
- target : b
434
- } ) ;
435
- } ) ;
436
-
437
- QUnit . test ( 'one() can add a listener to an array of event types on the window object' , function ( assert ) {
438
- const a = this . targets . a = evented ( { } ) ;
439
- const b = this . targets . b = window ;
440
- const spy = sinon . spy ( ) ;
441
- const x = createEvent ( 'x' ) ;
442
- const y = createEvent ( 'y' ) ;
443
-
444
- a . one ( b , [ 'x' , 'y' ] , spy ) ;
445
- b . dispatchEvent ( x ) ;
446
- b . dispatchEvent ( y ) ;
447
- b . dispatchEvent ( x ) ;
448
- b . dispatchEvent ( y ) ;
449
-
450
- // Make sure we aren't magically binding a listener to "a".
451
- a . trigger ( 'x' ) ;
452
- a . trigger ( 'y' ) ;
453
-
454
- assert . strictEqual ( spy . callCount , 1 , 'the listener was called the expected number of times' ) ;
455
-
456
- validateListenerCall ( spy . getCall ( 0 ) , a , {
457
- type : 'x' ,
458
- target : b
459
- } ) ;
460
- } ) ;
461
-
462
253
QUnit . test ( 'off() with no arguments will remove all listeners from all events on this object' , function ( assert ) {
463
254
const a = this . targets . a = evented ( { } ) ;
464
255
const spyX = sinon . spy ( ) ;
0 commit comments