UNPKG

react-native-accessibility-engine

Version:

Make accessibility-related assertions on React Native code using React Test Renderer

85 lines (61 loc) 2.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = exports.AccessibilityError = void 0; var _reactTestRenderer = _interopRequireDefault(require("react-test-renderer")); var _rules2 = _interopRequireDefault(require("../rules")); var _helpers = require("../helpers"); var _utils = require("../utils"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } class AccessibilityError extends Error { constructor() { let message = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''; super(message); this.name = 'AccessibilityError'; } } exports.AccessibilityError = AccessibilityError; const engine = (treeOrTestInstance, options) => { let testInstance = (0, _helpers.isReactTestInstance)(treeOrTestInstance) ? treeOrTestInstance : _reactTestRenderer.default.create(treeOrTestInstance).root; const _rules = options !== null && options !== void 0 && options.rules ? options.rules.map(id => _rules2.default.find(rule => rule.id === id)) : _rules2.default; const violations = []; // For every rule for (const rule of _rules) { // Traverse the component tree below the root to find the components that should be tested const matchedComponents = testInstance.findAll(rule.matcher, { deep: true }); // Check if the root of the tree should be tested as well if (rule.matcher(testInstance)) { matchedComponents.push(testInstance); } // For all the components that were found for (const component of matchedComponents) { let didPassAssertion = false; if ((0, _helpers.isHidden)(component)) { // Skip checks on hidden components didPassAssertion = true; } else { // Check if the component meets the rule's assertion didPassAssertion = rule.assertion(component); } // If not, add component to violation array if (!didPassAssertion) { violations.push({ pathToComponent: (0, _helpers.getPathToComponent)(component), ...rule.help }); } } } if (options !== null && options !== void 0 && options.customViolationHandler) { return options.customViolationHandler(violations); } if (options !== null && options !== void 0 && options.returnViolations) { return violations; } if (violations.length) { throw new AccessibilityError((0, _utils.generateCheckError)(violations)); } return []; }; var _default = engine; exports.default = _default; //# sourceMappingURL=index.js.map