UNPKG

4.49 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4Object.defineProperty(exports, "__esModule", {
5 value: true
6});
7exports.generateA11yTests = generateA11yTests;
8var _react = _interopRequireDefault(require("react"));
9var _uiTestSandbox = require("@instructure/ui-test-sandbox");
10var _expect = require("./expect");
11var _index = require("../index");
12var _generateComponentExamples = require("./generateComponentExamples");
13/*
14 * The MIT License (MIT)
15 *
16 * Copyright (c) 2015 - present Instructure, Inc.
17 *
18 * Permission is hereby granted, free of charge, to any person obtaining a copy
19 * of this software and associated documentation files (the "Software"), to deal
20 * in the Software without restriction, including without limitation the rights
21 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
22 * copies of the Software, and to permit persons to whom the Software is
23 * furnished to do so, subject to the following conditions:
24 *
25 * The above copyright notice and this permission notice shall be included in all
26 * copies or substantial portions of the Software.
27 *
28 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
31 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
32 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
33 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34 * SOFTWARE.
35 */
36
37const renderExample = _ref => {
38 let Component = _ref.Component,
39 componentProps = _ref.componentProps,
40 key = _ref.key;
41 return /*#__PURE__*/_react.default.createElement(Component, Object.assign({
42 key: key
43 }, componentProps));
44};
45
46/**
47 *
48 * This function will generate a11y tests based on the component and the component
49 * example definition json.
50 * It will enumerate over the generated component examples and will call the
51 * `.accessible()` function on it.
52 *
53 * ```js
54 * const subject = await mount(<Example />)
55 const element = within(subject.getDOMNode())
56
57 expect(await element.accessible()).to.be.true()
58 * ```
59 * @param Component - The base Component
60 * @param componentExample - The example definition json, this will be the basis for the prop
61 * combination generation.
62 * @param ariaRulesToIgnore - ARIA rules to ignore. these must be one of the
63 * rules described here: https://dequeuniversity.com/rules/axe/4.4
64 *
65 * @module generateA11yTests
66 * @private
67 */
68function generateA11yTests(Component, componentExample, ariaRulesToIgnore) {
69 const sections = (0, _generateComponentExamples.generateComponentExamples)(Component, componentExample);
70 describe(`${Component.displayName} should meet accessibility standards`, async () => {
71 sections.forEach((_ref2, i) => {
72 let pages = _ref2.pages,
73 propName = _ref2.propName,
74 propValue = _ref2.propValue;
75 const description = propName ? `rendered with prop '${String(propName)}' = '${propValue}'` : 'rendered';
76 describe(`${description}`, async () => {
77 let rendered = 0;
78 let j = 0;
79 pages.forEach(_ref3 => {
80 let examples = _ref3.examples;
81 examples.forEach(example => {
82 var _Example;
83 const Example = renderExample.bind(null, example);
84 const description = process.env.DEBUG ? `with prop combination: ${JSON.stringify(example.componentProps, null, 2)} [${i},${j}]` : `${j}`;
85 it(description, async () => {
86 const subject = await (0, _uiTestSandbox.mount)(_Example || (_Example = /*#__PURE__*/_react.default.createElement(Example, null)));
87 const element = (0, _index.within)(subject.getDOMNode());
88 let axeOptions = void 0;
89 if (ariaRulesToIgnore) {
90 axeOptions = {
91 ignores: ariaRulesToIgnore
92 };
93 }
94 const result = await element.accessible(axeOptions);
95 if (result !== true) {
96 // \n is needed because chai cuts the log in two with a
97 // expected - actual message.
98 _expect.expect.fail('\n' + result.message);
99 }
100 });
101 j++;
102 });
103 rendered = rendered + examples.length;
104 });
105 });
106 });
107 });
108}
\No newline at end of file