Skip to content

Commit 892fce8

Browse files
author
Colt Pini
authored
Merge pull request #34 from github/lazyload
Fixed tests and a few changes:
2 parents 81d26fc + 1f28cc2 commit 892fce8

File tree

4 files changed

+57
-57
lines changed

4 files changed

+57
-57
lines changed

include-fragment-element.js

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ function fire(name, target) {
1010
}, 0)
1111
}
1212

13-
function handleData(el, data) {
14-
return data.then(
13+
function handleData(el) {
14+
return getData(el).then(
1515
function(html) {
1616
const parentNode = el.parentNode
1717
if (parentNode) {
@@ -44,10 +44,6 @@ function getData(el) {
4444
export class IncludeFragmentElement extends HTMLElement {
4545
constructor() {
4646
super()
47-
// Preload data cache
48-
getData(this)['catch'](function() {
49-
// Ignore `src missing` error on pre-load.
50-
})
5147
}
5248

5349
static get observedAttributes() {
@@ -79,20 +75,17 @@ export class IncludeFragmentElement extends HTMLElement {
7975

8076
attributeChangedCallback(attribute) {
8177
if (attribute === 'src') {
82-
// Reload data load cache.
83-
const data = getData(this)
84-
8578
// Source changed after attached so replace element.
8679
if (this._attached) {
87-
handleData(this, data)
80+
handleData(this)
8881
}
8982
}
9083
}
9184

9285
connectedCallback() {
9386
this._attached = true
9487
if (this.src) {
95-
handleData(this, getData(this))
88+
handleData(this)
9689
}
9790
}
9891

@@ -116,38 +109,31 @@ export class IncludeFragmentElement extends HTMLElement {
116109
}
117110

118111
load() {
119-
const self = this
120-
121112
return Promise.resolve()
122-
.then(function() {
123-
const request = self.request()
124-
fire('loadstart', self)
125-
return self.fetch(request)
113+
.then(() => {
114+
fire('loadstart', this)
115+
return this.fetch(this.request())
126116
})
127-
.then(function(response) {
117+
.then(response => {
128118
if (response.status !== 200) {
129119
throw new Error(`Failed to load resource: the server responded with a status of ${response.status}`)
130120
}
131-
132121
const ct = response.headers.get('Content-Type')
133122
if (!ct || !ct.match(/^text\/html/)) {
134123
throw new Error(`Failed to load resource: expected text/html but was ${ct}`)
135124
}
136-
137125
return response
138126
})
139-
.then(function(response) {
140-
return response.text()
141-
})
127+
.then(response => response.text())
142128
.then(
143-
function(data) {
144-
fire('load', self)
145-
fire('loadend', self)
129+
data => {
130+
fire('load', this)
131+
fire('loadend', this)
146132
return data
147133
},
148-
function(error) {
149-
fire('error', self)
150-
fire('loadend', self)
134+
error => {
135+
fire('error', this)
136+
fire('loadend', this)
151137
throw error
152138
}
153139
)

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "include-fragment-element",
3-
"version": "4.0.0",
3+
"version": "4.0.1",
44
"main": "dist/index-umd.js",
55
"license": "MIT",
66
"repository": {

test/test.js

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ const responses = {
4444
}
4545
}
4646

47+
function when(el, eventType) {
48+
return new Promise(function(resolve) {
49+
el.addEventListener(eventType, resolve)
50+
})
51+
}
52+
4753
window.IncludeFragmentElement.prototype.fetch = function(request) {
4854
const pathname = new URL(request.url).pathname
4955
return Promise.resolve(responses[pathname](request))
@@ -54,6 +60,9 @@ setup(function() {
5460
})
5561

5662
suite('include-fragment-element', function() {
63+
teardown(() => {
64+
document.body.innerHTML = ''
65+
})
5766
test('create from document.createElement', function() {
5867
const el = document.createElement('include-fragment')
5968
assert.equal('INCLUDE-FRAGMENT', el.nodeName)
@@ -79,7 +88,7 @@ suite('include-fragment-element', function() {
7988
test('initial data is in error state', function() {
8089
const el = document.createElement('include-fragment')
8190

82-
el.data['catch'](function(error) {
91+
return el.data['catch'](function(error) {
8392
assert.ok(error)
8493
})
8594
})
@@ -88,7 +97,7 @@ suite('include-fragment-element', function() {
8897
const el = document.createElement('include-fragment')
8998
el.src = '/hello'
9099

91-
el.data.then(
100+
return el.data.then(
92101
function(html) {
93102
assert.equal('<div id="replaced">hello</div>', html)
94103
},
@@ -102,7 +111,7 @@ suite('include-fragment-element', function() {
102111
const el = document.createElement('include-fragment')
103112
el.setAttribute('src', '/hello')
104113

105-
el.data.then(
114+
return el.data.then(
106115
function(html) {
107116
assert.equal('<div id="replaced">hello</div>', html)
108117
},
@@ -116,7 +125,7 @@ suite('include-fragment-element', function() {
116125
const el = document.createElement('include-fragment')
117126
el.src = '/count'
118127

119-
el.data
128+
return el.data
120129
.then(function(text) {
121130
assert.equal('1', text)
122131
el.src = '/count'
@@ -136,7 +145,7 @@ suite('include-fragment-element', function() {
136145
const el = document.createElement('include-fragment')
137146
el.setAttribute('src', '/count')
138147

139-
el.data
148+
return el.data
140149
.then(function(text) {
141150
assert.equal('1', text)
142151
el.setAttribute('src', '/count')
@@ -177,32 +186,38 @@ suite('include-fragment-element', function() {
177186
div.innerHTML = '<include-fragment src="/hello">loading</include-fragment>'
178187
document.body.appendChild(div)
179188

180-
div.firstChild.addEventListener('load', function() {
189+
return when(div.firstChild, 'load').then(() => {
181190
assert.equal(document.querySelector('include-fragment'), null)
182191
assert.equal(document.querySelector('#replaced').textContent, 'hello')
183192
})
184193
})
185194

186195
test('does not replace element if it has no parent', function() {
187196
const div = document.createElement('div')
188-
div.innerHTML = '<include-fragment src="/hello">loading</include-fragment>'
197+
div.innerHTML = '<include-fragment>loading</include-fragment>'
189198
document.body.appendChild(div)
190199

191200
const fragment = div.firstChild
192201
fragment.remove()
202+
fragment.src = '/hello'
203+
204+
let didRun = false
193205

194206
window.addEventListener('unhandledrejection', function() {
195207
assert.ok(false)
196208
})
197209

198-
fragment.addEventListener('load', function() {
199-
assert.equal(document.querySelector('#replaced'), null)
210+
fragment.addEventListener('loadstart', () => {
211+
didRun = true
212+
})
200213

214+
setTimeout(() => {
215+
assert.ok(!didRun)
201216
div.appendChild(fragment)
217+
}, 10)
202218

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')
206221
})
207222
})
208223

@@ -211,7 +226,7 @@ suite('include-fragment-element', function() {
211226
div.innerHTML = '<include-fragment src="/one-two">loading</include-fragment>'
212227
document.body.appendChild(div)
213228

214-
div.firstChild.addEventListener('load', function() {
229+
return when(div.firstChild, 'load').then(() => {
215230
assert.equal(document.querySelector('include-fragment'), null)
216231
assert.equal(document.querySelector('#one').textContent, 'one')
217232
assert.equal(document.querySelector('#two').textContent, 'two')
@@ -223,7 +238,7 @@ suite('include-fragment-element', function() {
223238
div.innerHTML = '<include-fragment src="/boom">loading</include-fragment>'
224239
document.body.appendChild(div)
225240

226-
div.firstChild.addEventListener('error', function(event) {
241+
return when(div.firstChild, 'error').then(event => {
227242
assert.equal(event.bubbles, false)
228243
assert.equal(event.cancelable, false)
229244
})
@@ -234,33 +249,32 @@ suite('include-fragment-element', function() {
234249
div.innerHTML = '<include-fragment src="/boom">loading</include-fragment>'
235250
document.body.appendChild(div)
236251

237-
div.firstChild.addEventListener('error', function() {
252+
return when(div.firstChild, 'error').then(() =>
238253
assert.ok(document.querySelector('include-fragment').classList.contains('is-error'))
239-
})
254+
)
240255
})
241256

242257
test('adds is-error class on mising Content-Type', function() {
243258
const div = document.createElement('div')
244259
div.innerHTML = '<include-fragment src="/blank-type">loading</include-fragment>'
245260
document.body.appendChild(div)
246261

247-
div.firstChild.addEventListener('error', function() {
262+
return when(div.firstChild, 'error').then(() =>
248263
assert.ok(document.querySelector('include-fragment').classList.contains('is-error'))
249-
})
264+
)
250265
})
251266

252267
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)
256270

257-
div.firstChild.addEventListener('load', function() {
271+
setTimeout(function() {
272+
elem.src = '/hello'
273+
}, 10)
274+
275+
return when(elem, 'load').then(() => {
258276
assert.equal(document.querySelector('include-fragment'), null)
259277
assert.equal(document.querySelector('#replaced').textContent, 'hello')
260278
})
261-
262-
setTimeout(function() {
263-
div.firstChild.src = '/hello'
264-
}, 10)
265279
})
266280
})

0 commit comments

Comments
 (0)