Skip to content

Commit 2e31ffd

Browse files
committed
Add test
1 parent f415f24 commit 2e31ffd

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

test/test.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,28 @@ const responses = {
3939
'Content-Type': 'text/html'
4040
}
4141
})
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+
})
4264
}
4365
}
4466

@@ -159,6 +181,35 @@ suite('include-fragment-element', function() {
159181
})
160182
})
161183

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, /expected text\/html but was text\/javascript/)
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, /expected text\/html; fragment but was text\/html; charset=utf-8/)
209+
}
210+
)
211+
})
212+
162213
test('data is not writable', function() {
163214
const el = document.createElement('include-fragment')
164215
assert.ok(el.data !== 42)
@@ -231,6 +282,17 @@ suite('include-fragment-element', function() {
231282
})
232283
})
233284

285+
test('replaces with response with the right accept header', function() {
286+
const div = document.createElement('div')
287+
div.innerHTML = '<include-fragment src="/fragment" accept="text/html; fragment">loading</include-fragment>'
288+
document.body.appendChild(div)
289+
290+
return when(div.firstChild, 'load').then(() => {
291+
assert.equal(document.querySelector('include-fragment'), null)
292+
assert.equal(document.querySelector('#fragment').textContent, 'fragment')
293+
})
294+
})
295+
234296
test('error event is not cancelable or bubbles', function() {
235297
const div = document.createElement('div')
236298
div.innerHTML = '<include-fragment src="/boom">loading</include-fragment>'
@@ -262,6 +324,16 @@ suite('include-fragment-element', function() {
262324
)
263325
})
264326

327+
test('adds is-error class on incorrect Content-Type', function() {
328+
const div = document.createElement('div')
329+
div.innerHTML = '<include-fragment src="/fragment">loading</include-fragment>'
330+
document.body.appendChild(div)
331+
332+
return when(div.firstChild, 'error').then(() =>
333+
assert.ok(document.querySelector('include-fragment').classList.contains('is-error'))
334+
)
335+
})
336+
265337
test('replaces element when src attribute is changed', function() {
266338
const elem = document.createElement('include-fragment')
267339
document.body.appendChild(elem)

0 commit comments

Comments
 (0)