@@ -44,19 +44,10 @@ const responses = {
44
44
}
45
45
}
46
46
47
- const cleanDOM = ( ) => {
48
- document . body . innerHTML = ''
49
- }
50
-
51
- const checkAsync = ( done , func ) => {
52
- try {
53
- func ( )
54
- done ( )
55
- } catch ( err ) {
56
- done ( err )
57
- } finally {
58
- cleanDOM ( )
59
- }
47
+ function when ( el , eventType ) {
48
+ return new Promise ( function ( resolve ) {
49
+ el . addEventListener ( eventType , resolve )
50
+ } )
60
51
}
61
52
62
53
window . IncludeFragmentElement . prototype . fetch = function ( request ) {
@@ -69,16 +60,17 @@ setup(function() {
69
60
} )
70
61
71
62
suite ( 'include-fragment-element' , function ( ) {
63
+ teardown ( ( ) => {
64
+ document . body . innerHTML = ''
65
+ } )
72
66
test ( 'create from document.createElement' , function ( ) {
73
67
const el = document . createElement ( 'include-fragment' )
74
68
assert . equal ( 'INCLUDE-FRAGMENT' , el . nodeName )
75
- cleanDOM ( )
76
69
} )
77
70
78
71
test ( 'create from constructor' , function ( ) {
79
72
const el = new window . IncludeFragmentElement ( )
80
73
assert . equal ( 'INCLUDE-FRAGMENT' , el . nodeName )
81
- cleanDOM ( )
82
74
} )
83
75
84
76
test ( 'src property' , function ( ) {
@@ -91,15 +83,13 @@ suite('include-fragment-element', function() {
91
83
const link = document . createElement ( 'a' )
92
84
link . href = '/hello'
93
85
assert . equal ( link . href , el . src )
94
- cleanDOM ( )
95
86
} )
96
87
97
88
test ( 'initial data is in error state' , function ( ) {
98
89
const el = document . createElement ( 'include-fragment' )
99
90
100
91
return el . data [ 'catch' ] ( function ( error ) {
101
92
assert . ok ( error )
102
- cleanDOM ( )
103
93
} )
104
94
} )
105
95
@@ -110,11 +100,9 @@ suite('include-fragment-element', function() {
110
100
return el . data . then (
111
101
function ( html ) {
112
102
assert . equal ( '<div id="replaced">hello</div>' , html )
113
- cleanDOM ( )
114
103
} ,
115
104
function ( ) {
116
105
assert . ok ( false )
117
- cleanDOM ( )
118
106
}
119
107
)
120
108
} )
@@ -126,11 +114,9 @@ suite('include-fragment-element', function() {
126
114
return el . data . then (
127
115
function ( html ) {
128
116
assert . equal ( '<div id="replaced">hello</div>' , html )
129
- cleanDOM ( )
130
117
} ,
131
118
function ( ) {
132
119
assert . ok ( false )
133
- cleanDOM ( )
134
120
}
135
121
)
136
122
} )
@@ -149,11 +135,9 @@ suite('include-fragment-element', function() {
149
135
} )
150
136
. then ( function ( text ) {
151
137
assert . equal ( '1' , text )
152
- cleanDOM ( )
153
138
} )
154
139
[ 'catch' ] ( function ( ) {
155
140
assert . ok ( false )
156
- cleanDOM ( )
157
141
} )
158
142
} )
159
143
@@ -171,11 +155,9 @@ suite('include-fragment-element', function() {
171
155
} )
172
156
. then ( function ( text ) {
173
157
assert . equal ( '1' , text )
174
- cleanDOM ( )
175
158
} )
176
159
[ 'catch' ] ( function ( ) {
177
160
assert . ok ( false )
178
- cleanDOM ( )
179
161
} )
180
162
} )
181
163
@@ -186,7 +168,6 @@ suite('include-fragment-element', function() {
186
168
el . data = 42
187
169
} finally {
188
170
assert . ok ( el . data !== 42 )
189
- cleanDOM ( )
190
171
}
191
172
} )
192
173
@@ -197,25 +178,21 @@ suite('include-fragment-element', function() {
197
178
delete el . data
198
179
} finally {
199
180
assert . ok ( el . data !== undefined )
200
- cleanDOM ( )
201
181
}
202
182
} )
203
183
204
- test ( 'replaces element on 200 status' , function ( done ) {
184
+ test ( 'replaces element on 200 status' , function ( ) {
205
185
const div = document . createElement ( 'div' )
206
186
div . innerHTML = '<include-fragment src="/hello">loading</include-fragment>'
207
187
document . body . appendChild ( div )
208
188
209
- div . firstChild . addEventListener ( 'load' , function ( ) {
210
- checkAsync ( done , ( ) => {
211
- assert . equal ( document . querySelector ( 'include-fragment' ) , null )
212
- assert . equal ( document . querySelector ( '#replaced' ) . textContent , 'hello' )
213
- cleanDOM ( )
214
- } )
189
+ return when ( div . firstChild , 'load' ) . then ( ( ) => {
190
+ assert . equal ( document . querySelector ( 'include-fragment' ) , null )
191
+ assert . equal ( document . querySelector ( '#replaced' ) . textContent , 'hello' )
215
192
} )
216
193
} )
217
194
218
- test ( 'does not replace element if it has no parent' , function ( done ) {
195
+ test ( 'does not replace element if it has no parent' , function ( ) {
219
196
const div = document . createElement ( 'div' )
220
197
div . innerHTML = '<include-fragment>loading</include-fragment>'
221
198
document . body . appendChild ( div )
@@ -234,82 +211,70 @@ suite('include-fragment-element', function() {
234
211
didRun = true
235
212
} )
236
213
237
- fragment . addEventListener ( 'load' , function ( ) {
238
- checkAsync ( done , ( ) => {
239
- assert . equal ( document . querySelector ( '#replaced' ) . textContent , 'hello' )
240
- } )
241
- } )
242
-
243
214
setTimeout ( ( ) => {
244
215
assert . ok ( ! didRun )
245
216
div . appendChild ( fragment )
246
217
} , 10 )
218
+
219
+ return when ( fragment , 'load' ) . then ( ( ) => {
220
+ assert . equal ( document . querySelector ( '#replaced' ) . textContent , 'hello' )
221
+ } )
247
222
} )
248
223
249
- test ( 'replaces with several new elements on 200 status' , function ( done ) {
224
+ test ( 'replaces with several new elements on 200 status' , function ( ) {
250
225
const div = document . createElement ( 'div' )
251
226
div . innerHTML = '<include-fragment src="/one-two">loading</include-fragment>'
252
227
document . body . appendChild ( div )
253
228
254
- div . firstChild . addEventListener ( 'load' , function ( ) {
255
- checkAsync ( done , ( ) => {
256
- assert . equal ( document . querySelector ( 'include-fragment' ) , null )
257
- assert . equal ( document . querySelector ( '#one' ) . textContent , 'one' )
258
- assert . equal ( document . querySelector ( '#two' ) . textContent , 'two' )
259
- } )
229
+ return when ( div . firstChild , 'load' ) . then ( ( ) => {
230
+ assert . equal ( document . querySelector ( 'include-fragment' ) , null )
231
+ assert . equal ( document . querySelector ( '#one' ) . textContent , 'one' )
232
+ assert . equal ( document . querySelector ( '#two' ) . textContent , 'two' )
260
233
} )
261
234
} )
262
235
263
- test ( 'error event is not cancelable or bubbles' , function ( done ) {
236
+ test ( 'error event is not cancelable or bubbles' , function ( ) {
264
237
const div = document . createElement ( 'div' )
265
238
div . innerHTML = '<include-fragment src="/boom">loading</include-fragment>'
266
239
document . body . appendChild ( div )
267
240
268
- div . firstChild . addEventListener ( 'error' , function ( event ) {
269
- checkAsync ( done , ( ) => {
270
- assert . equal ( event . bubbles , false )
271
- assert . equal ( event . cancelable , false )
272
- } )
241
+ return when ( div . firstChild , 'error' ) . then ( event => {
242
+ assert . equal ( event . bubbles , false )
243
+ assert . equal ( event . cancelable , false )
273
244
} )
274
245
} )
275
246
276
- test ( 'adds is-error class on 500 status' , function ( done ) {
247
+ test ( 'adds is-error class on 500 status' , function ( ) {
277
248
const div = document . createElement ( 'div' )
278
249
div . innerHTML = '<include-fragment src="/boom">loading</include-fragment>'
279
250
document . body . appendChild ( div )
280
251
281
- div . firstChild . addEventListener ( 'error' , function ( ) {
282
- checkAsync ( done , ( ) => {
283
- assert . ok ( document . querySelector ( 'include-fragment' ) . classList . contains ( 'is-error' ) )
284
- } )
285
- } )
252
+ return when ( div . firstChild , 'error' ) . then ( ( ) =>
253
+ assert . ok ( document . querySelector ( 'include-fragment' ) . classList . contains ( 'is-error' ) )
254
+ )
286
255
} )
287
256
288
- test ( 'adds is-error class on mising Content-Type' , function ( done ) {
257
+ test ( 'adds is-error class on mising Content-Type' , function ( ) {
289
258
const div = document . createElement ( 'div' )
290
259
div . innerHTML = '<include-fragment src="/blank-type">loading</include-fragment>'
291
260
document . body . appendChild ( div )
292
261
293
- div . firstChild . addEventListener ( 'error' , function ( ) {
294
- checkAsync ( done , ( ) => {
295
- assert . ok ( document . querySelector ( 'include-fragment' ) . classList . contains ( 'is-error' ) )
296
- } )
297
- } )
262
+ return when ( div . firstChild , 'error' ) . then ( ( ) =>
263
+ assert . ok ( document . querySelector ( 'include-fragment' ) . classList . contains ( 'is-error' ) )
264
+ )
298
265
} )
299
266
300
- test ( 'replaces element when src attribute is changed' , function ( done ) {
267
+ test ( 'replaces element when src attribute is changed' , function ( ) {
301
268
const elem = document . createElement ( 'include-fragment' )
302
269
document . body . appendChild ( elem )
303
270
304
- elem . addEventListener ( 'load' , function ( ) {
305
- checkAsync ( done , ( ) => {
306
- assert . equal ( document . querySelector ( 'include-fragment' ) , null )
307
- assert . equal ( document . querySelector ( '#replaced' ) . textContent , 'hello' )
308
- } )
309
- } )
310
-
311
271
setTimeout ( function ( ) {
312
272
elem . src = '/hello'
313
273
} , 10 )
274
+
275
+ return when ( elem , 'load' ) . then ( ( ) => {
276
+ assert . equal ( document . querySelector ( 'include-fragment' ) , null )
277
+ assert . equal ( document . querySelector ( '#replaced' ) . textContent , 'hello' )
278
+ } )
314
279
} )
315
280
} )
0 commit comments