UNPKG

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