Skip to content

Commit 1f28cc2

Browse files
author
Colt Pini
committed
Changed checkAsync to a much better promise based solution, thanks mislav, and removed a few missed lazyloads in the js.
1 parent 3d31691 commit 1f28cc2

File tree

2 files changed

+41
-76
lines changed

2 files changed

+41
-76
lines changed

include-fragment-element.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export class IncludeFragmentElement extends HTMLElement {
7474
}
7575

7676
attributeChangedCallback(attribute) {
77-
if (attribute === 'src' && !this.lazyload) {
77+
if (attribute === 'src') {
7878
// Source changed after attached so replace element.
7979
if (this._attached) {
8080
handleData(this)
@@ -84,7 +84,7 @@ export class IncludeFragmentElement extends HTMLElement {
8484

8585
connectedCallback() {
8686
this._attached = true
87-
if (this.src && !this.lazyload) {
87+
if (this.src) {
8888
handleData(this)
8989
}
9090
}

test/test.js

Lines changed: 39 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,10 @@ const responses = {
4444
}
4545
}
4646

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+
})
6051
}
6152

6253
window.IncludeFragmentElement.prototype.fetch = function(request) {
@@ -69,16 +60,17 @@ setup(function() {
6960
})
7061

7162
suite('include-fragment-element', function() {
63+
teardown(() => {
64+
document.body.innerHTML = ''
65+
})
7266
test('create from document.createElement', function() {
7367
const el = document.createElement('include-fragment')
7468
assert.equal('INCLUDE-FRAGMENT', el.nodeName)
75-
cleanDOM()
7669
})
7770

7871
test('create from constructor', function() {
7972
const el = new window.IncludeFragmentElement()
8073
assert.equal('INCLUDE-FRAGMENT', el.nodeName)
81-
cleanDOM()
8274
})
8375

8476
test('src property', function() {
@@ -91,15 +83,13 @@ suite('include-fragment-element', function() {
9183
const link = document.createElement('a')
9284
link.href = '/hello'
9385
assert.equal(link.href, el.src)
94-
cleanDOM()
9586
})
9687

9788
test('initial data is in error state', function() {
9889
const el = document.createElement('include-fragment')
9990

10091
return el.data['catch'](function(error) {
10192
assert.ok(error)
102-
cleanDOM()
10393
})
10494
})
10595

@@ -110,11 +100,9 @@ suite('include-fragment-element', function() {
110100
return el.data.then(
111101
function(html) {
112102
assert.equal('<div id="replaced">hello</div>', html)
113-
cleanDOM()
114103
},
115104
function() {
116105
assert.ok(false)
117-
cleanDOM()
118106
}
119107
)
120108
})
@@ -126,11 +114,9 @@ suite('include-fragment-element', function() {
126114
return el.data.then(
127115
function(html) {
128116
assert.equal('<div id="replaced">hello</div>', html)
129-
cleanDOM()
130117
},
131118
function() {
132119
assert.ok(false)
133-
cleanDOM()
134120
}
135121
)
136122
})
@@ -149,11 +135,9 @@ suite('include-fragment-element', function() {
149135
})
150136
.then(function(text) {
151137
assert.equal('1', text)
152-
cleanDOM()
153138
})
154139
['catch'](function() {
155140
assert.ok(false)
156-
cleanDOM()
157141
})
158142
})
159143

@@ -171,11 +155,9 @@ suite('include-fragment-element', function() {
171155
})
172156
.then(function(text) {
173157
assert.equal('1', text)
174-
cleanDOM()
175158
})
176159
['catch'](function() {
177160
assert.ok(false)
178-
cleanDOM()
179161
})
180162
})
181163

@@ -186,7 +168,6 @@ suite('include-fragment-element', function() {
186168
el.data = 42
187169
} finally {
188170
assert.ok(el.data !== 42)
189-
cleanDOM()
190171
}
191172
})
192173

@@ -197,25 +178,21 @@ suite('include-fragment-element', function() {
197178
delete el.data
198179
} finally {
199180
assert.ok(el.data !== undefined)
200-
cleanDOM()
201181
}
202182
})
203183

204-
test('replaces element on 200 status', function(done) {
184+
test('replaces element on 200 status', function() {
205185
const div = document.createElement('div')
206186
div.innerHTML = '<include-fragment src="/hello">loading</include-fragment>'
207187
document.body.appendChild(div)
208188

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')
215192
})
216193
})
217194

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() {
219196
const div = document.createElement('div')
220197
div.innerHTML = '<include-fragment>loading</include-fragment>'
221198
document.body.appendChild(div)
@@ -234,82 +211,70 @@ suite('include-fragment-element', function() {
234211
didRun = true
235212
})
236213

237-
fragment.addEventListener('load', function() {
238-
checkAsync(done, () => {
239-
assert.equal(document.querySelector('#replaced').textContent, 'hello')
240-
})
241-
})
242-
243214
setTimeout(() => {
244215
assert.ok(!didRun)
245216
div.appendChild(fragment)
246217
}, 10)
218+
219+
return when(fragment, 'load').then(() => {
220+
assert.equal(document.querySelector('#replaced').textContent, 'hello')
221+
})
247222
})
248223

249-
test('replaces with several new elements on 200 status', function(done) {
224+
test('replaces with several new elements on 200 status', function() {
250225
const div = document.createElement('div')
251226
div.innerHTML = '<include-fragment src="/one-two">loading</include-fragment>'
252227
document.body.appendChild(div)
253228

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')
260233
})
261234
})
262235

263-
test('error event is not cancelable or bubbles', function(done) {
236+
test('error event is not cancelable or bubbles', function() {
264237
const div = document.createElement('div')
265238
div.innerHTML = '<include-fragment src="/boom">loading</include-fragment>'
266239
document.body.appendChild(div)
267240

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)
273244
})
274245
})
275246

276-
test('adds is-error class on 500 status', function(done) {
247+
test('adds is-error class on 500 status', function() {
277248
const div = document.createElement('div')
278249
div.innerHTML = '<include-fragment src="/boom">loading</include-fragment>'
279250
document.body.appendChild(div)
280251

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+
)
286255
})
287256

288-
test('adds is-error class on mising Content-Type', function(done) {
257+
test('adds is-error class on mising Content-Type', function() {
289258
const div = document.createElement('div')
290259
div.innerHTML = '<include-fragment src="/blank-type">loading</include-fragment>'
291260
document.body.appendChild(div)
292261

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+
)
298265
})
299266

300-
test('replaces element when src attribute is changed', function(done) {
267+
test('replaces element when src attribute is changed', function() {
301268
const elem = document.createElement('include-fragment')
302269
document.body.appendChild(elem)
303270

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-
311271
setTimeout(function() {
312272
elem.src = '/hello'
313273
}, 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+
})
314279
})
315280
})

0 commit comments

Comments
 (0)