@@ -44,6 +44,12 @@ const responses = {
44
44
}
45
45
}
46
46
47
+ function when ( el , eventType ) {
48
+ return new Promise ( function ( resolve ) {
49
+ el . addEventListener ( eventType , resolve )
50
+ } )
51
+ }
52
+
47
53
window . IncludeFragmentElement . prototype . fetch = function ( request ) {
48
54
const pathname = new URL ( request . url ) . pathname
49
55
return Promise . resolve ( responses [ pathname ] ( request ) )
@@ -54,6 +60,9 @@ setup(function() {
54
60
} )
55
61
56
62
suite ( 'include-fragment-element' , function ( ) {
63
+ teardown ( ( ) => {
64
+ document . body . innerHTML = ''
65
+ } )
57
66
test ( 'create from document.createElement' , function ( ) {
58
67
const el = document . createElement ( 'include-fragment' )
59
68
assert . equal ( 'INCLUDE-FRAGMENT' , el . nodeName )
@@ -79,7 +88,7 @@ suite('include-fragment-element', function() {
79
88
test ( 'initial data is in error state' , function ( ) {
80
89
const el = document . createElement ( 'include-fragment' )
81
90
82
- el . data [ 'catch' ] ( function ( error ) {
91
+ return el . data [ 'catch' ] ( function ( error ) {
83
92
assert . ok ( error )
84
93
} )
85
94
} )
@@ -88,7 +97,7 @@ suite('include-fragment-element', function() {
88
97
const el = document . createElement ( 'include-fragment' )
89
98
el . src = '/hello'
90
99
91
- el . data . then (
100
+ return el . data . then (
92
101
function ( html ) {
93
102
assert . equal ( '<div id="replaced">hello</div>' , html )
94
103
} ,
@@ -102,7 +111,7 @@ suite('include-fragment-element', function() {
102
111
const el = document . createElement ( 'include-fragment' )
103
112
el . setAttribute ( 'src' , '/hello' )
104
113
105
- el . data . then (
114
+ return el . data . then (
106
115
function ( html ) {
107
116
assert . equal ( '<div id="replaced">hello</div>' , html )
108
117
} ,
@@ -116,7 +125,7 @@ suite('include-fragment-element', function() {
116
125
const el = document . createElement ( 'include-fragment' )
117
126
el . src = '/count'
118
127
119
- el . data
128
+ return el . data
120
129
. then ( function ( text ) {
121
130
assert . equal ( '1' , text )
122
131
el . src = '/count'
@@ -136,7 +145,7 @@ suite('include-fragment-element', function() {
136
145
const el = document . createElement ( 'include-fragment' )
137
146
el . setAttribute ( 'src' , '/count' )
138
147
139
- el . data
148
+ return el . data
140
149
. then ( function ( text ) {
141
150
assert . equal ( '1' , text )
142
151
el . setAttribute ( 'src' , '/count' )
@@ -177,32 +186,38 @@ suite('include-fragment-element', function() {
177
186
div . innerHTML = '<include-fragment src="/hello">loading</include-fragment>'
178
187
document . body . appendChild ( div )
179
188
180
- div . firstChild . addEventListener ( 'load' , function ( ) {
189
+ return when ( div . firstChild , 'load' ) . then ( ( ) => {
181
190
assert . equal ( document . querySelector ( 'include-fragment' ) , null )
182
191
assert . equal ( document . querySelector ( '#replaced' ) . textContent , 'hello' )
183
192
} )
184
193
} )
185
194
186
195
test ( 'does not replace element if it has no parent' , function ( ) {
187
196
const div = document . createElement ( 'div' )
188
- div . innerHTML = '<include-fragment src="/hello" >loading</include-fragment>'
197
+ div . innerHTML = '<include-fragment>loading</include-fragment>'
189
198
document . body . appendChild ( div )
190
199
191
200
const fragment = div . firstChild
192
201
fragment . remove ( )
202
+ fragment . src = '/hello'
203
+
204
+ let didRun = false
193
205
194
206
window . addEventListener ( 'unhandledrejection' , function ( ) {
195
207
assert . ok ( false )
196
208
} )
197
209
198
- fragment . addEventListener ( 'load' , function ( ) {
199
- assert . equal ( document . querySelector ( '#replaced' ) , null )
210
+ fragment . addEventListener ( 'loadstart' , ( ) => {
211
+ didRun = true
212
+ } )
200
213
214
+ setTimeout ( ( ) => {
215
+ assert . ok ( ! didRun )
201
216
div . appendChild ( fragment )
217
+ } , 10 )
202
218
203
- setTimeout ( function ( ) {
204
- assert . equal ( document . querySelector ( '#replaced' ) . textContent , 'hello' )
205
- } , 10 )
219
+ return when ( fragment , 'load' ) . then ( ( ) => {
220
+ assert . equal ( document . querySelector ( '#replaced' ) . textContent , 'hello' )
206
221
} )
207
222
} )
208
223
@@ -211,7 +226,7 @@ suite('include-fragment-element', function() {
211
226
div . innerHTML = '<include-fragment src="/one-two">loading</include-fragment>'
212
227
document . body . appendChild ( div )
213
228
214
- div . firstChild . addEventListener ( 'load' , function ( ) {
229
+ return when ( div . firstChild , 'load' ) . then ( ( ) => {
215
230
assert . equal ( document . querySelector ( 'include-fragment' ) , null )
216
231
assert . equal ( document . querySelector ( '#one' ) . textContent , 'one' )
217
232
assert . equal ( document . querySelector ( '#two' ) . textContent , 'two' )
@@ -223,7 +238,7 @@ suite('include-fragment-element', function() {
223
238
div . innerHTML = '<include-fragment src="/boom">loading</include-fragment>'
224
239
document . body . appendChild ( div )
225
240
226
- div . firstChild . addEventListener ( 'error' , function ( event ) {
241
+ return when ( div . firstChild , 'error' ) . then ( event => {
227
242
assert . equal ( event . bubbles , false )
228
243
assert . equal ( event . cancelable , false )
229
244
} )
@@ -234,33 +249,32 @@ suite('include-fragment-element', function() {
234
249
div . innerHTML = '<include-fragment src="/boom">loading</include-fragment>'
235
250
document . body . appendChild ( div )
236
251
237
- div . firstChild . addEventListener ( 'error' , function ( ) {
252
+ return when ( div . firstChild , 'error' ) . then ( ( ) =>
238
253
assert . ok ( document . querySelector ( 'include-fragment' ) . classList . contains ( 'is-error' ) )
239
- } )
254
+ )
240
255
} )
241
256
242
257
test ( 'adds is-error class on mising Content-Type' , function ( ) {
243
258
const div = document . createElement ( 'div' )
244
259
div . innerHTML = '<include-fragment src="/blank-type">loading</include-fragment>'
245
260
document . body . appendChild ( div )
246
261
247
- div . firstChild . addEventListener ( 'error' , function ( ) {
262
+ return when ( div . firstChild , 'error' ) . then ( ( ) =>
248
263
assert . ok ( document . querySelector ( 'include-fragment' ) . classList . contains ( 'is-error' ) )
249
- } )
264
+ )
250
265
} )
251
266
252
267
test ( 'replaces element when src attribute is changed' , function ( ) {
253
- const div = document . createElement ( 'div' )
254
- div . innerHTML = '<include-fragment>loading</include-fragment>'
255
- document . body . appendChild ( div )
268
+ const elem = document . createElement ( 'include-fragment' )
269
+ document . body . appendChild ( elem )
256
270
257
- div . firstChild . addEventListener ( 'load' , function ( ) {
271
+ setTimeout ( function ( ) {
272
+ elem . src = '/hello'
273
+ } , 10 )
274
+
275
+ return when ( elem , 'load' ) . then ( ( ) => {
258
276
assert . equal ( document . querySelector ( 'include-fragment' ) , null )
259
277
assert . equal ( document . querySelector ( '#replaced' ) . textContent , 'hello' )
260
278
} )
261
-
262
- setTimeout ( function ( ) {
263
- div . firstChild . src = '/hello'
264
- } , 10 )
265
279
} )
266
280
} )
0 commit comments