@@ -39,6 +39,28 @@ const responses = {
39
39
'Content-Type' : 'text/html'
40
40
}
41
41
} )
42
+ } ,
43
+ '/fragment' : function ( request ) {
44
+ if ( request . headers . get ( 'Accept' ) === 'text/html; fragment' ) {
45
+ return new Response ( '<div id="fragment">fragment</div>' , {
46
+ status : 200 ,
47
+ headers : {
48
+ 'Content-Type' : 'text/html; fragment'
49
+ }
50
+ } )
51
+ } else {
52
+ return new Response ( '406' , {
53
+ status : 406
54
+ } )
55
+ }
56
+ } ,
57
+ '/test.js' : function ( ) {
58
+ return new Response ( 'alert("what")' , {
59
+ status : 200 ,
60
+ headers : {
61
+ 'Content-Type' : 'text/javascript'
62
+ }
63
+ } )
42
64
}
43
65
}
44
66
@@ -159,6 +181,49 @@ suite('include-fragment-element', function() {
159
181
} )
160
182
} )
161
183
184
+ test ( 'throws on incorrect Content-Type' , function ( ) {
185
+ const el = document . createElement ( 'include-fragment' )
186
+ el . setAttribute ( 'src' , '/test.js' )
187
+
188
+ return el . data . then (
189
+ ( ) => {
190
+ assert . ok ( false )
191
+ } ,
192
+ error => {
193
+ assert . match ( error , / e x p e c t e d t e x t \/ h t m l b u t w a s t e x t \/ j a v a s c r i p t / )
194
+ }
195
+ )
196
+ } )
197
+
198
+ test ( 'throws on non-matching Content-Type' , function ( ) {
199
+ const el = document . createElement ( 'include-fragment' )
200
+ el . setAttribute ( 'accept' , 'text/html; fragment' )
201
+ el . setAttribute ( 'src' , '/hello' )
202
+
203
+ return el . data . then (
204
+ ( ) => {
205
+ assert . ok ( false )
206
+ } ,
207
+ error => {
208
+ assert . match ( error , / e x p e c t e d t e x t \/ h t m l ; f r a g m e n t b u t w a s t e x t \/ h t m l ; c h a r s e t = u t f - 8 / )
209
+ }
210
+ )
211
+ } )
212
+
213
+ test ( 'throws on 406' , function ( ) {
214
+ const el = document . createElement ( 'include-fragment' )
215
+ el . setAttribute ( 'src' , '/fragment' )
216
+
217
+ return el . data . then (
218
+ ( ) => {
219
+ assert . ok ( false )
220
+ } ,
221
+ error => {
222
+ assert . match ( error , / t h e s e r v e r r e s p o n d e d w i t h a s t a t u s o f 4 0 6 / )
223
+ }
224
+ )
225
+ } )
226
+
162
227
test ( 'data is not writable' , function ( ) {
163
228
const el = document . createElement ( 'include-fragment' )
164
229
assert . ok ( el . data !== 42 )
@@ -231,6 +296,28 @@ suite('include-fragment-element', function() {
231
296
} )
232
297
} )
233
298
299
+ test ( 'replaces with response with accept header for any' , function ( ) {
300
+ const div = document . createElement ( 'div' )
301
+ div . innerHTML = '<include-fragment src="/test.js" accept="*/*">loading</include-fragment>'
302
+ document . body . appendChild ( div )
303
+
304
+ return when ( div . firstChild , 'load' ) . then ( ( ) => {
305
+ assert . equal ( document . querySelector ( 'include-fragment' ) , null )
306
+ assert . match ( document . body . textContent , / a l e r t \( " w h a t " \) / )
307
+ } )
308
+ } )
309
+
310
+ test ( 'replaces with response with the right accept header' , function ( ) {
311
+ const div = document . createElement ( 'div' )
312
+ div . innerHTML = '<include-fragment src="/fragment" accept="text/html; fragment">loading</include-fragment>'
313
+ document . body . appendChild ( div )
314
+
315
+ return when ( div . firstChild , 'load' ) . then ( ( ) => {
316
+ assert . equal ( document . querySelector ( 'include-fragment' ) , null )
317
+ assert . equal ( document . querySelector ( '#fragment' ) . textContent , 'fragment' )
318
+ } )
319
+ } )
320
+
234
321
test ( 'error event is not cancelable or bubbles' , function ( ) {
235
322
const div = document . createElement ( 'div' )
236
323
div . innerHTML = '<include-fragment src="/boom">loading</include-fragment>'
@@ -262,6 +349,16 @@ suite('include-fragment-element', function() {
262
349
)
263
350
} )
264
351
352
+ test ( 'adds is-error class on incorrect Content-Type' , function ( ) {
353
+ const div = document . createElement ( 'div' )
354
+ div . innerHTML = '<include-fragment src="/fragment">loading</include-fragment>'
355
+ document . body . appendChild ( div )
356
+
357
+ return when ( div . firstChild , 'error' ) . then ( ( ) =>
358
+ assert . ok ( document . querySelector ( 'include-fragment' ) . classList . contains ( 'is-error' ) )
359
+ )
360
+ } )
361
+
265
362
test ( 'replaces element when src attribute is changed' , function ( ) {
266
363
const elem = document . createElement ( 'include-fragment' )
267
364
document . body . appendChild ( elem )
0 commit comments