UNPKG

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