Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(preload): reject promise axe.utils.preload when XHR fails #2009

Merged
merged 6 commits into from
Feb 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
54 changes: 31 additions & 23 deletions lib/core/utils/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ axe.utils.getPreloadConfig = function getPreloadConfig(options) {
if (
options.preload.timeout &&
typeof options.preload.timeout === 'number' &&
!Number.isNaN(options.preload.timeout)
!isNaN(options.preload.timeout)
Copy link
Contributor

@straker straker Feb 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Number.isNaN does not exist in IE11, and was causing the timeout test to fail, but not because of the timeout (this was only discovered when we added looking at the error message in the test)

) {
config.timeout = options.preload.timeout;
}
Expand Down Expand Up @@ -97,12 +97,14 @@ axe.utils.preload = function preload(options) {
* Start `timeout` timer for preloading assets
* -> reject if allowed time expires.
*/
setTimeout(() => reject(`Preload assets timed out.`), timeout);
const preloadTimeout = setTimeout(
() => reject(new Error(`Preload assets timed out.`)),
timeout
);

/**
* Fetch requested `assets`
*/

Promise.all(
assets.map(asset =>
preloadFunctionsMap[asset](options).then(results => {
Expand All @@ -111,26 +113,32 @@ axe.utils.preload = function preload(options) {
};
})
)
).then(results => {
/**
* Combine array of results into an object map
*
* From ->
* [{cssom: [...], aom: [...]}]
* To ->
* {
* cssom: [...]
* aom: [...]
* }
*/
const preloadAssets = results.reduce((out, result) => {
return {
...out,
...result
};
}, {});
)
.then(results => {
/**
* Combine array of results into an object map
*
* From ->
* [{cssom: [...], aom: [...]}]
* To ->
* {
* cssom: [...]
* aom: [...]
* }
*/
const preloadAssets = results.reduce((out, result) => {
return {
...out,
...result
};
}, {});

resolve(preloadAssets);
});
clearTimeout(preloadTimeout);
resolve(preloadAssets);
})
.catch(err => {
clearTimeout(preloadTimeout);
reject(err);
});
});
};
90 changes: 69 additions & 21 deletions test/integration/full/preload/preload.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* global axe */
/* global axe, Promise */

describe('axe.utils.preload integration test', function() {
'use strict';

Expand All @@ -8,6 +9,10 @@ describe('axe.utils.preload integration test', function() {
id: 'crossOriginLinkHref',
href: 'https://unpkg.com/gutenberg-css@0.4'
},
crossOriginDoesNotExist: {
id: 'styleTag',
text: '@import "https://i-do-not-exist.css"'
},
crossOriginLinkHrefMediaPrint: {
id: 'crossOriginLinkHrefMediaPrint',
href:
Expand Down Expand Up @@ -70,17 +75,19 @@ describe('axe.utils.preload integration test', function() {
if (err) {
done(err);
}
getPreload().then(function(preloadedAssets) {
assert.property(preloadedAssets, 'cssom');
assert.lengthOf(preloadedAssets.cssom, 1);
var sheetData = preloadedAssets.cssom[0].sheet;
axe.testUtils.assertStylesheet(
sheetData,
'.inline-css-test',
stylesForPage[0].text
);
done();
});
getPreload()
.then(function(preloadedAssets) {
assert.property(preloadedAssets, 'cssom');
assert.lengthOf(preloadedAssets.cssom, 1);
var sheetData = preloadedAssets.cssom[0].sheet;
axe.testUtils.assertStylesheet(
sheetData,
'.inline-css-test',
stylesForPage[0].text
);
done();
})
.catch(done);
});
});

Expand All @@ -90,24 +97,65 @@ describe('axe.utils.preload integration test', function() {
if (err) {
done(err);
}
getPreload().then(function(preloadedAssets) {
assert.property(preloadedAssets, 'cssom');
assert.lengthOf(preloadedAssets.cssom, 0);
done();
});
getPreload()
.then(function(preloadedAssets) {
assert.property(preloadedAssets, 'cssom');
assert.lengthOf(preloadedAssets.cssom, 0);
done();
})
.catch(done);
});
});

it('returns NO preloaded CSSOM assets when requested stylesheet does not exist`', function(done) {
stylesForPage = [styleSheets.crossOriginDoesNotExist];
attachStylesheets({ styles: stylesForPage }, function(err) {
if (err) {
done(err);
}
getPreload()
.then(function() {
done(new Error('Not expecting to complete the promise'));
})
.catch(function(err) {
assert.isNotNull(err);
assert.isTrue(!err.message.includes('Preload assets timed out'));
done();
})
.catch(done);
});
});

it('rejects preload function when timed out before fetching assets', function(done) {
stylesForPage = [styleSheets.crossOriginLinkHref];

var origPreloadCssom = axe.utils.preloadCssom;
axe.utils.preloadCssom = function() {
return new Promise(function(res) {
setTimeout(function() {
res(true);
}, 2000);
});
};

attachStylesheets({ styles: stylesForPage }, function(err) {
if (err) {
done(err);
}
getPreload(1).catch(function(err) {
assert.isNotNull(err);
done();
});
getPreload(1)
.then(function() {
done(new Error('Not expecting to complete the promise'));
})
.catch(function(err) {
assert.isNotNull(err);
assert.isTrue(err.message.includes('Preload assets timed out'));

done();
})
.catch(done)
.finally(function() {
axe.utils.preloadCssom = origPreloadCssom;
});
});
});

Expand Down