terra-toolkit
Version:
Utilities to help when developing terra modules.
134 lines (101 loc) • 5.12 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var _fs = _interopRequireDefault(require("fs"));
var _excluded = ["id"];
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
var axeCoreSrc;
/**
* Injects Axe on the on the test page and configures it options are included in the
* wdio configuration.
*
* @param {Object} [options] - the Axe test options
*/
var injectAxe = function injectAxe(axeOptions) {
if (!axeCoreSrc) {
axeCoreSrc = _fs.default.readFileSync(require.resolve('axe-core/axe.min.js'), 'utf8');
axeCoreSrc = axeCoreSrc.replace(/^\/\*.*\*\//, '');
if (axeOptions) {
var axeJsonConfigure = "axe.configure(".concat(JSON.stringify(axeOptions), ")");
axeCoreSrc = "".concat(axeCoreSrc, "\n").concat(axeJsonConfigure, ";");
}
}
browser.execute(axeCoreSrc);
};
/**
* Runs Axe against test page to check for WCAG 2.0 AA and Section 508 violations.
*
* @param {Object} [rules] - the axe rules to use to use in the axe run.
*
* @returns {Object} axeResults - the axe results as list of passes, violations, incomplete and inapplicable.
*/
var runAxeTest = function runAxeTest(axeRules) {
var globalAxeRules = browser.options.axe && browser.options.axe.options ? browser.options.axe.options.rules : undefined; // Converting `globalAxeRules` from array of rules object to object of rules.
var formattedGlobalRules = globalAxeRules && globalAxeRules.reduce(function (formattedRules, currentRule) {
var id = currentRule.id,
rest = (0, _objectWithoutProperties2.default)(currentRule, _excluded);
var rules = _objectSpread({}, formattedRules);
rules[id] = rest;
return rules;
}, {});
var rules = (formattedGlobalRules || axeRules) && _objectSpread(_objectSpread({}, formattedGlobalRules), axeRules);
/* Avoid arrow callback syntax as this function is injected into the browser */
/* eslint-disable func-names, prefer-arrow-callback, object-shorthand */
var axeResult = browser.executeAsync(function (opts, done) {
axe.run(document, opts, function (error, result) {
done({
error: error,
result: result
});
});
}, {
rules: rules,
restoreScroll: true,
runOnly: ['wcag2a', 'wcag2aa', 'wcag21aa', 'section508'],
resultTypes: ['violations']
});
/* eslint-enable func-names, prefer-arrow-callback, object-shorthand */
return axeResult.value;
};
/**
* Custom wdio command to test accessibly on the page for each viewport defined.
*
* @param {Object} [options] - the Axe test options
* @param {Object} [rules] - the axe rules to use to use in the axe run.
* @param {Object[]} [options.viewports] - the list of Terra viewports to test.
*
* @returns {Object} axeResults - the axe results as list of passes, violations, incomplete and inapplicable.
*/
var axeCommand = function axeCommand() {
var testOptions = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var globalConfig = browser.options.axe || {};
/**
* Conditionally inject axe. This allows consumers to inject it themselves in
* the test examples which would slightly speed up test runs.
*/
var injectScript = globalConfig.inject || false;
if (injectScript && browser.execute('return window.axe === undefined;')) {
injectAxe(globalConfig.options);
}
var rules = testOptions.rules,
viewports = testOptions.viewports;
if (!viewports) {
// analyze the current viewport
return [runAxeTest(rules)];
} // get the current viewport
var currentViewportSize = browser.getViewportSize(); // Get accessibility results for each specified viewport size
var results = viewports.map(function (viewport) {
browser.setViewportSize(viewport);
return runAxeTest(rules);
}); // reset viewport back to the current viewport
browser.setViewportSize(currentViewportSize);
return results;
};
var _default = axeCommand;
exports.default = _default;