Skip to content

Commit 79fdc82

Browse files
committed
Fix ObjectURL revoke tests for Edge Legacy and IE.
1 parent cebde9b commit 79fdc82

File tree

1 file changed

+48
-24
lines changed

1 file changed

+48
-24
lines changed

test/test.js

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,25 @@
6868
}
6969
}
7070

71+
/**
72+
* Request helper function.
73+
*
74+
* @param {string} url URL to request
75+
* @param {Function} callback Request callback
76+
*/
77+
function request(url, callback) {
78+
var xhr = new XMLHttpRequest()
79+
xhr.onload = callback
80+
xhr.onerror = callback
81+
try {
82+
xhr.open('GET', url, true)
83+
} catch (e) {
84+
callback.call(xhr, e)
85+
return
86+
}
87+
xhr.send()
88+
}
89+
7190
describe('Loading', function () {
7291
it('Return an object with onload and onerror methods', function () {
7392
var img = loadImage(blobGIF, function () {})
@@ -116,32 +135,37 @@
116135
).to.be.ok
117136
})
118137

119-
it('Keep object URL if noRevoke is true', function (done) {
120-
expect(
121-
loadImage(
122-
blobGIF,
123-
function (img) {
124-
loadImage(img.src, function (img2) {
125-
expect(img.width).to.equal(img2.width)
126-
expect(img.height).to.equal(img2.height)
138+
describe('ObjectURL revoke', function () {
139+
// Using XMLHttpRequest via the request helper function to test Object
140+
// URLs to work around Edge Legacy and IE caching image URLs.
141+
if (!window.XMLHttpRequest) return
142+
143+
it('Keep object URL if noRevoke is set to true', function (done) {
144+
expect(
145+
loadImage(
146+
blobGIF,
147+
function (img) {
148+
request(img.src, function (event) {
149+
expect(event.type).to.equal('load')
150+
done()
151+
})
152+
},
153+
{ noRevoke: true }
154+
)
155+
).to.be.ok
156+
})
157+
158+
it('Discard object URL if noRevoke is not set', function (done) {
159+
expect(
160+
loadImage(blobGIF, function (img) {
161+
request(img.src, function (event) {
162+
// IE throws an error that has no type property:
163+
expect(event.type).to.be.oneOf(['error', undefined])
127164
done()
128165
})
129-
},
130-
{ noRevoke: true }
131-
)
132-
).to.be.ok
133-
})
134-
135-
it('Discard object URL if noRevoke is undefined/false', function (done) {
136-
expect(
137-
loadImage(blobGIF, function (img) {
138-
loadImage(img.src, function (img2) {
139-
expect(img2).to.be.an.instanceOf(window.Event)
140-
expect(img2.type).to.equal('error')
141-
done()
142166
})
143-
})
144-
).to.be.ok
167+
).to.be.ok
168+
})
145169
})
146170
})
147171

@@ -1232,7 +1256,7 @@
12321256
it('Should fetch image URL as blob if meta option is true', function (done) {
12331257
expect(
12341258
loadImage(
1235-
// IE11 does not allow XMLHttpRequest access to data URLs,
1259+
// IE does not allow XMLHttpRequest access to data URLs,
12361260
// so we use an ObjectURL instead of imageUrlJPEG directly:
12371261
loadImage.createObjectURL(blobJPEG),
12381262
function (img, data) {

0 commit comments

Comments
 (0)