1
1
import { on , off , fire } from '../delegated-events' ;
2
2
3
- describe ( 'delegated event listeners' , function ( ) {
4
- before ( function ( ) {
3
+ describe ( 'delegated event listeners' , function ( ) {
4
+ before ( function ( ) {
5
5
const container = document . createElement ( 'div' ) ;
6
6
container . innerHTML = `
7
7
<div class="js-test-parent">
@@ -10,9 +10,9 @@ describe('delegated event listeners', function() {
10
10
document . body . appendChild ( container ) ;
11
11
} ) ;
12
12
13
- describe ( 'custom event dispatch' , function ( ) {
14
- it ( 'fires custom events with detail' , function ( ) {
15
- const observer = function ( event ) {
13
+ describe ( 'custom event dispatch' , function ( ) {
14
+ it ( 'fires custom events with detail' , function ( ) {
15
+ const observer = function ( event ) {
16
16
assert ( event . bubbles ) ;
17
17
assert ( event . cancelable ) ;
18
18
assert . equal ( event . type , 'test:detail' ) ;
@@ -25,8 +25,8 @@ describe('delegated event listeners', function() {
25
25
document . removeEventListener ( 'test:detail' , observer ) ;
26
26
} ) ;
27
27
28
- it ( 'fires custom events without detail' , function ( ) {
29
- const observer = function ( event ) {
28
+ it ( 'fires custom events without detail' , function ( ) {
29
+ const observer = function ( event ) {
30
30
assert ( event . detail === undefined || event . detail === null ) ;
31
31
assert . instanceOf ( event , CustomEvent ) ;
32
32
} ;
@@ -35,15 +35,15 @@ describe('delegated event listeners', function() {
35
35
document . removeEventListener ( 'test:fire' , observer ) ;
36
36
} ) ;
37
37
38
- it ( 'returns canceled when default prevented' , function ( ) {
38
+ it ( 'returns canceled when default prevented' , function ( ) {
39
39
const observer = event => event . preventDefault ( ) ;
40
40
document . addEventListener ( 'test:cancel' , observer ) ;
41
41
const canceled = ! fire ( document . body , 'test:cancel' ) ;
42
42
assert . equal ( canceled , true ) ;
43
43
document . removeEventListener ( 'test:cancel' , observer ) ;
44
44
} ) ;
45
45
46
- it ( 'returns not canceled when default is not prevented' , function ( ) {
46
+ it ( 'returns not canceled when default is not prevented' , function ( ) {
47
47
const [ observer , trace ] = spy ( event => assert . ok ( event ) ) ;
48
48
document . addEventListener ( 'test:event' , observer ) ;
49
49
const canceled = ! fire ( document . body , 'test:event' ) ;
@@ -53,9 +53,9 @@ describe('delegated event listeners', function() {
53
53
} ) ;
54
54
} ) ;
55
55
56
- describe ( 'event observer registration' , function ( ) {
57
- it ( 'observes custom events' , function ( ) {
58
- const observer = function ( event ) {
56
+ describe ( 'event observer registration' , function ( ) {
57
+ it ( 'observes custom events' , function ( ) {
58
+ const observer = function ( event ) {
59
59
assert ( event . bubbles ) ;
60
60
assert ( event . cancelable ) ;
61
61
assert . equal ( event . type , 'test:on' ) ;
@@ -71,21 +71,21 @@ describe('delegated event listeners', function() {
71
71
off ( 'test:on' , 'body' , observer ) ;
72
72
} ) ;
73
73
74
- it ( 'removes bubble event observers' , function ( ) {
74
+ it ( 'removes bubble event observers' , function ( ) {
75
75
const observer = event => assert . fail ( event ) ;
76
76
on ( 'test:off' , '*' , observer ) ;
77
77
off ( 'test:off' , '*' , observer ) ;
78
78
fire ( document . body , 'test:off' ) ;
79
79
} ) ;
80
80
81
- it ( 'removes capture event observers' , function ( ) {
81
+ it ( 'removes capture event observers' , function ( ) {
82
82
const observer = event => assert . fail ( event ) ;
83
83
on ( 'test:off' , '*' , observer , { capture : true } ) ;
84
84
off ( 'test:off' , '*' , observer , { capture : true } ) ;
85
85
fire ( document . body , 'test:off' ) ;
86
86
} ) ;
87
87
88
- it ( 'can reregister after removing' , function ( ) {
88
+ it ( 'can reregister after removing' , function ( ) {
89
89
const [ observer , trace ] = spy ( event => assert . ok ( event ) ) ;
90
90
on ( 'test:register' , 'body' , observer ) ;
91
91
off ( 'test:register' , 'body' , observer ) ;
@@ -96,43 +96,43 @@ describe('delegated event listeners', function() {
96
96
} ) ;
97
97
} ) ;
98
98
99
- describe ( 'event propagation' , function ( ) {
100
- before ( function ( ) {
99
+ describe ( 'event propagation' , function ( ) {
100
+ before ( function ( ) {
101
101
this . parent = document . querySelector ( '.js-test-parent' ) ;
102
102
this . child = document . querySelector ( '.js-test-child' ) ;
103
103
} ) ;
104
104
105
- it ( 'fires observers in tree order' , function ( ) {
105
+ it ( 'fires observers in tree order' , function ( ) {
106
106
const order = [ ] ;
107
107
108
108
const parent = this . parent ;
109
109
const child = this . child ;
110
110
111
- const one = function ( event ) {
111
+ const one = function ( event ) {
112
112
assert . strictEqual ( child , event . target ) ;
113
113
assert . strictEqual ( parent , event . currentTarget ) ;
114
114
assert . strictEqual ( this , event . currentTarget ) ;
115
115
assert . strictEqual ( this , parent ) ;
116
116
order . push ( 1 ) ;
117
117
} ;
118
118
119
- const two = function ( event ) {
119
+ const two = function ( event ) {
120
120
assert . strictEqual ( child , event . target ) ;
121
121
assert . strictEqual ( child , event . currentTarget ) ;
122
122
assert . strictEqual ( this , event . currentTarget ) ;
123
123
assert . strictEqual ( this , child ) ;
124
124
order . push ( 2 ) ;
125
125
} ;
126
126
127
- const three = function ( event ) {
127
+ const three = function ( event ) {
128
128
assert . strictEqual ( child , event . target ) ;
129
129
assert . strictEqual ( parent , event . currentTarget ) ;
130
130
assert . strictEqual ( this , event . currentTarget ) ;
131
131
assert . strictEqual ( this , parent ) ;
132
132
order . push ( 3 ) ;
133
133
} ;
134
134
135
- const four = function ( event ) {
135
+ const four = function ( event ) {
136
136
assert . strictEqual ( child , event . target ) ;
137
137
assert . strictEqual ( child , event . currentTarget ) ;
138
138
assert . strictEqual ( this , event . currentTarget ) ;
@@ -153,7 +153,7 @@ describe('delegated event listeners', function() {
153
153
assert . deepEqual ( [ 1 , 2 , 4 , 3 ] , order ) ;
154
154
} ) ;
155
155
156
- it ( 'clears currentTarget after propagation' , function ( ) {
156
+ it ( 'clears currentTarget after propagation' , function ( ) {
157
157
const [ observer , trace ] = spy ( event => assert . ok ( event . currentTarget ) ) ;
158
158
const event = new CustomEvent ( 'test:clear' , { bubbles : true } ) ;
159
159
@@ -164,7 +164,7 @@ describe('delegated event listeners', function() {
164
164
off ( 'test:clear' , 'body' , observer ) ;
165
165
} ) ;
166
166
167
- it ( 'does not interfere with currentTarget when selectors match' , function ( ) {
167
+ it ( 'does not interfere with currentTarget when selectors match' , function ( ) {
168
168
const [ observer , trace ] = spy ( event =>
169
169
assert . strictEqual ( document . body , event . currentTarget )
170
170
) ;
@@ -185,7 +185,7 @@ describe('delegated event listeners', function() {
185
185
this . parent . removeEventListener ( 'test:target:capture' , observer2 ) ;
186
186
} ) ;
187
187
188
- it ( 'does not interfere with currentTarget when no selectors match' , function ( ) {
188
+ it ( 'does not interfere with currentTarget when no selectors match' , function ( ) {
189
189
const [ observer , trace ] = spy ( event => assert . ok ( event . currentTarget ) ) ;
190
190
const event = new CustomEvent ( 'test:currentTarget' , { bubbles : true } ) ;
191
191
@@ -202,7 +202,7 @@ describe('delegated event listeners', function() {
202
202
document . removeEventListener ( 'test:currentTarget' , observer ) ;
203
203
} ) ;
204
204
205
- it ( 'prevents redispatch after propagation is stopped' , function ( ) {
205
+ it ( 'prevents redispatch after propagation is stopped' , function ( ) {
206
206
const [ observer , trace ] = spy ( event => event . stopPropagation ( ) ) ;
207
207
const event = new CustomEvent ( 'test:redispatch' , { bubbles : true } ) ;
208
208
@@ -217,7 +217,7 @@ describe('delegated event listeners', function() {
217
217
off ( 'test:redispatch' , 'body' , observer ) ;
218
218
} ) ;
219
219
220
- it ( 'stops propagation bubbling to parent' , function ( ) {
220
+ it ( 'stops propagation bubbling to parent' , function ( ) {
221
221
const one = event => assert . fail ( event ) ;
222
222
const two = event => event . stopPropagation ( ) ;
223
223
on ( 'test:bubble' , '.js-test-parent' , one ) ;
@@ -227,7 +227,7 @@ describe('delegated event listeners', function() {
227
227
off ( 'test:bubble' , '.js-test-child' , two ) ;
228
228
} ) ;
229
229
230
- it ( 'stops immediate propagation' , function ( ) {
230
+ it ( 'stops immediate propagation' , function ( ) {
231
231
const one = event => event . stopImmediatePropagation ( ) ;
232
232
const two = event => assert . fail ( event ) ;
233
233
on ( 'test:immediate' , '.js-test-child' , one ) ;
@@ -237,7 +237,7 @@ describe('delegated event listeners', function() {
237
237
off ( 'test:immediate' , '.js-test-child' , two ) ;
238
238
} ) ;
239
239
240
- it ( 'stops immediate propagation and bubbling' , function ( ) {
240
+ it ( 'stops immediate propagation and bubbling' , function ( ) {
241
241
const one = event => assert . fail ( event ) ;
242
242
const two = event => event . stopImmediatePropagation ( ) ;
243
243
on ( 'test:stop' , '.js-test-parent' , one ) ;
@@ -247,10 +247,10 @@ describe('delegated event listeners', function() {
247
247
off ( 'test:stop' , '.js-test-child' , two ) ;
248
248
} ) ;
249
249
250
- it ( 'calculates selector matches before dispatching event' , function ( ) {
250
+ it ( 'calculates selector matches before dispatching event' , function ( ) {
251
251
this . child . classList . add ( 'inactive' ) ;
252
252
253
- const one = function ( event ) {
253
+ const one = function ( event ) {
254
254
event . target . classList . remove ( 'inactive' ) ;
255
255
event . target . classList . add ( 'active' ) ;
256
256
assert . ok ( event ) ;
@@ -270,7 +270,7 @@ describe('delegated event listeners', function() {
270
270
271
271
function spy ( fn ) {
272
272
const tracker = { calls : 0 } ;
273
- const capture = function ( ) {
273
+ const capture = function ( ) {
274
274
tracker . calls ++ ;
275
275
return fn . apply ( this , arguments ) ;
276
276
} ;
0 commit comments