terra-toolkit
Version:
Utilities to help when developing terra modules.
110 lines (92 loc) • 4.54 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _chai = _interopRequireDefault(require("chai"));
/**
* A chai assertion method to be paired with browser.axe() tests to assert no violations were found
* on the test page.
*/
function accessible() {
// eslint-disable-next-line no-underscore-dangle
new _chai.default.Assertion(this._obj).to.be.instanceof(Array);
/**
* This rule was introduced in axe-core v3.3 and causes failures in many Terra components.
* The solution to address this failure vary by component. It is being disabled until a solution is identified in the future.
*
* Reference: https://github.com/cerner/terra-framework/issues/991
*/
var axeRuleIdsToDisable = ['scrollable-region-focusable']; // eslint-disable-next-line no-underscore-dangle
var errors = this._obj.filter(function (test) {
return test.result;
}).reduce(function (all, test) {
return all.concat(test.result.violations);
}, []).filter(function (test) {
return !axeRuleIdsToDisable.includes(test.id);
}).filter(function (test) {
return test;
}).map(function (test) {
return "".concat(JSON.stringify(test, null, 2));
});
this.assert(errors.length === 0, "expected no accessibility violations but got:\n\t".concat(errors[0]), 'expected accessibility errors but received none');
}
/**
* Helper method to determine which comparison results are relevant for if the chai screenshot assertion
* fails.
*
* @param {Object[]} screenshots - The list of comparison results
* @param {boolean} screenshot.isSameDimensions - If the latest screenshot was the same size as the
* reference screenshot.
* @param {boolean} screenshot.isWithinMisMatchTolerance - If the latest screenshot was within the
* mismatch tolerance.
* @param {boolean} screenshot.isExactSameImage - If the latest screenshot matched the reference
* screenshot exactly, i.e. 0% mismatch.
* @param {Number} screenshot.misMatchPercentage - The mismatch percentage when comparing the latest
* screenshot to the reference screenshot.
* @param {Number} screenshot.viewport - The viewport that the latest screenshot was taken in.
*/
var getComparisonResults = function getComparisonResults(screenshots) {
if (screenshots.length < 1) {
return 'No screenshots to compare.';
}
var results = screenshots.map(function (comparison) {
var viewport = comparison.viewport,
misMatchPercentage = comparison.misMatchPercentage,
isSameDimensions = comparison.isSameDimensions;
var relevantInformation = {};
if (viewport) {
relevantInformation.viewport = viewport;
}
if (!isSameDimensions) {
relevantInformation.isSameDimensions = isSameDimensions;
}
relevantInformation.misMatchPercentage = misMatchPercentage;
return "".concat(JSON.stringify(relevantInformation, null, 2));
});
return results;
};
/**
* A chai assertion method to be paired with Visual Regression Service to assert each screenshot is within
* the mismatch tolerance and are the same size.
*/
function matchReference() {
// eslint-disable-next-line no-underscore-dangle
new _chai.default.Assertion(this._obj).to.be.instanceof(Array); // eslint-disable-next-line no-underscore-dangle
var screenshots = this._obj;
var comparisonResults = getComparisonResults(screenshots); // Validate the screenshot is the same size for the case when the new screenshot matches 100% but is 'n' pixel taller due to new content.
// For example: the latest screenshot of a list has two more items than the reference screenshot so it is 60 pixels taller. That should fail.
var imagesMatch = screenshots.every(function (screenshot) {
return screenshot && screenshot.isSameDimensions && screenshot.isWithinMisMatchTolerance;
});
var ignoreComparisonResults = global.browser.options.visualRegression.ignoreComparisonResults;
this.assert(ignoreComparisonResults || imagesMatch === true, "expected to be within the mismatch tolerance, but received the following comparison results \n".concat(comparisonResults), "did not expect to be within the mismatch tolerance, but received the following comparison results \n".concat(comparisonResults));
}
var chaiMethods = {
accessible: accessible,
getComparisonResults: getComparisonResults,
matchReference: matchReference
};
var _default = chaiMethods;
exports.default = _default;