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

refactor: onload listener for stylesheets in before hook of tests #1132

Merged
merged 12 commits into from
Nov 1, 2018
25 changes: 8 additions & 17 deletions test/integration/full/css-orientation-lock/passes.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,6 @@ describe('css-orientation-lock passes test', function() {
var shadowSupported = axe.testUtils.shadowSupport.v1;
var isPhantom = window.PHANTOMJS ? true : false;

function addSheet(data) {
if (data.href) {
var link = document.createElement('link');
link.rel = 'stylesheet';
link.href = data.href;
document.head.appendChild(link);
} else {
const style = document.createElement('style');
style.type = 'text/css';
style.appendChild(document.createTextNode(data.text));
document.head.appendChild(style);
}
}

var styleSheets = [
{
href:
Expand All @@ -34,9 +20,14 @@ describe('css-orientation-lock passes test', function() {
this.skip();
done();
} else {
styleSheets.forEach(addSheet);
// wait for network request to complete for added sheets
setTimeout(done, 5000);
axe.testUtils
.addStyleSheets(styleSheets)
.then(function() {
done();
})
.catch(function(error) {
done(new Error('Could not load stylesheets for testing. ' + error));
});
}
});

Expand Down
25 changes: 8 additions & 17 deletions test/integration/full/css-orientation-lock/violations.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,6 @@ describe('css-orientation-lock violations test', function() {
var shadowSupported = axe.testUtils.shadowSupport.v1;
var isPhantom = window.PHANTOMJS ? true : false;

function addSheet(data) {
if (data.href) {
var link = document.createElement('link');
link.rel = 'stylesheet';
link.href = data.href;
document.head.appendChild(link);
} else {
const style = document.createElement('style');
style.type = 'text/css';
style.appendChild(document.createTextNode(data.text));
document.head.appendChild(style);
}
}

var styleSheets = [
{
href:
Expand All @@ -33,9 +19,14 @@ describe('css-orientation-lock violations test', function() {
this.skip();
done();
} else {
styleSheets.forEach(addSheet);
// wait for network request to complete for added sheets
setTimeout(done, 5000);
axe.testUtils
.addStyleSheets(styleSheets)
.then(function() {
done();
})
.catch(function(error) {
done(new Error('Could not load stylesheets for testing. ' + error));
});
}
});

Expand Down
33 changes: 8 additions & 25 deletions test/integration/full/preload-cssom/preload-cssom.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,6 @@ describe('preload cssom integration test', function() {
var shadowSupported = axe.testUtils.shadowSupport.v1;
var isPhantom = window.PHANTOMJS ? true : false;

function addSheet(data) {
if (data.href) {
var link = document.createElement('link');
link.rel = 'stylesheet';
link.href = data.href;
if (data.mediaPrint) {
link.media = 'print';
}
document.head.appendChild(link);
} else {
const style = document.createElement('style');
style.type = 'text/css';
style.appendChild(document.createTextNode(data.text));
document.head.appendChild(style);
}
}

var styleSheets = [
{
href:
Expand All @@ -44,14 +27,14 @@ describe('preload cssom integration test', function() {
this.skip();
done();
} else {
styleSheets.forEach(addSheet);
// cache original axios object
if (axe.imports.axios) {
origAxios = axe.imports.axios;
}

// wait for network request to complete for added sheets
setTimeout(done, 5000);
axe.testUtils
.addStyleSheets(styleSheets)
.then(function() {
done();
})
.catch(function(error) {
done(new Error('Could not load stylesheets for testing. ' + error));
});
}
});

Expand Down
31 changes: 9 additions & 22 deletions test/integration/full/preload/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,6 @@ describe('preload integration test', function() {
var origAxios;
var isPhantom = window.PHANTOMJS ? true : false;

function addSheet(data) {
if (data.href) {
var link = document.createElement('link');
link.rel = 'stylesheet';
link.href = data.href;
if (data.mediaPrint) {
link.media = 'print';
}
document.head.appendChild(link);
} else {
const style = document.createElement('style');
style.type = 'text/css';
style.appendChild(document.createTextNode(data.text));
document.head.appendChild(style);
}
}

var styleSheets = [
{
href: 'https://unpkg.com/gutenberg-css@0.4'
Expand All @@ -41,8 +24,6 @@ describe('preload integration test', function() {
this.skip();
done();
} else {
styleSheets.forEach(addSheet);

// cache originals
if (axe.imports.axios) {
origAxios = axe.imports.axios;
Expand Down Expand Up @@ -82,9 +63,15 @@ describe('preload integration test', function() {
}
]
});

// wait for network request to complete for added sheets
setTimeout(done, 5000);
// load stylesheets
axe.testUtils
.addStyleSheets(styleSheets)
.then(function() {
done();
})
.catch(function(error) {
done(new Error('Could not load stylesheets for testing. ' + error));
});
}
});

Expand Down
62 changes: 62 additions & 0 deletions test/testutils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* global Promise */

// Let the user know they need to disable their axe/attest extension before running the tests.
if (window.__AXE_EXTENSION__) {
throw new Error(
Expand Down Expand Up @@ -220,4 +222,64 @@ testUtils.awaitNestedLoad = function awaitNestedLoad(win, cb) {
});
};

/**
* Add a given stylesheet dynamically to the document
*
* @param {Object} data composite object containing properties to create stylesheet
* @property {String} data.href relative or absolute url for stylesheet to be loaded
* @property {Boolean} data.mediaPrint boolean to represent if the constructed sheet is for print media
* @property {String} data.text text contents to be written to the stylesheet
* @returns {Object} a Promise
*/
testUtils.addStyleSheet = function addStyleSheet(data) {
function loadAsLinkTag(href, mediaPrint, resolve, reject) {
var link = document.createElement('link');
link.rel = 'stylesheet';
link.href = href;
if (mediaPrint) {
link.media = 'print';
}
link.onload = function() {
resolve();
};
link.onerror = function() {
reject();
};
document.head.appendChild(link);
}

function loadAsStyleTag(text, resolve) {
const style = document.createElement('style');
style.type = 'text/css';
style.appendChild(document.createTextNode(text));
document.head.appendChild(style);
resolve();
}

return new Promise(function(resolve) {
if (data.href) {
loadAsLinkTag(data.href, data.mediaPrint, resolve);
} else {
loadAsStyleTag(data.text, resolve);
}
});
};

/**
* Add a list of stylesheets
*
* @param {Object} sheets array of sheets data object
* @returns {Object} a Promise
*/
testUtils.addStyleSheets = function addStyleSheets(sheets) {
var promises = [];

sheets.forEach(function(data) {
var p = axe.testUtils.addStyleSheet(data);
promises.push(p);
});

return Promise.all(promises);
Copy link
Contributor

Choose a reason for hiding this comment

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

This won't work in IE.

};

axe.testUtils = testUtils;