-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
85 lines (73 loc) · 2.94 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// @ts-check
/* global expect, describe, test */
/** @typedef {import('react').FunctionComponentElement<any>} FunctionComponentElement */
/** @typedef {import('@storybook/react').Args} Args */
/** @typedef {import('@storybook/react').Story} Story */
/** @typedef {import('@storybook/react').StoryContext} StoryContext */
/** @typedef {import('./index').TestOptions} TestOptions */
/** @typedef {((story: Story, context?: Partial<StoryContext>) => FunctionComponentElement)} StoryDecorator */
/** @typedef {{ decorators: StoryDecorator[], globals: Args }} GlobalStoryConfig */
const { toBeEmptyDOMElement } = require('@testing-library/jest-dom/matchers');
const { render, waitFor } = require('@testing-library/react');
const glob = require('glob');
const { axe, toHaveNoViolations } = require('jest-axe');
const { createElement } = require('react');
const { getStoryTestCases } = require('./utils');
module.exports = testStorybookA11y;
/**
*
* @param {string} [storyGlob]
* @param {GlobalStoryConfig} [globalConfig]
* @param {TestOptions} [options]
* @returns {void}
*/
function testStorybookA11y(storyGlob, globalConfig, options) {
const { timeout, axeOptions } = options || { timeout: 1000 };
// Get stories to test
const search = storyGlob || '**/*.stories.@(js|jsx|ts|tsx)';
const files = glob.sync(search, { absolute: true, ignore: ['**/node_modules/**/*'] });
const cases = getStoryTestCases(files);
// Configure Jest custom matchers
expect.extend({ toBeEmptyDOMElement, ...toHaveNoViolations });
// Run tests
describe.each(cases)('%s a11y', (_, module) => {
// eslint-disable-next-line
const storyModule = require(module);
// Get story decorators, if any
/** @type {{decorators: StoryDecorator[]}} */
const { decorators: storyDecorators = [] } = storyModule.default || {};
const decorators = [...((globalConfig && globalConfig.decorators) || []), ...storyDecorators];
const { globals } = globalConfig || {};
/** @type {[string, Story, any][]} */
const stories = Object.entries(storyModule)
.filter(([key]) => key !== 'default' && key !== '__esModule')
.map(([key, value]) => [key, value, value.args]);
test.each(stories)('%s story should have no a11y violations', async (__, story, props) => {
const { container } = renderWithDecorators(story, props, decorators, globals);
// Wait for story to render completely
await waitFor(
() => {
expect(container).not.toBeEmptyDOMElement();
},
{ timeout }
);
// Test for a11y violations
const results = await axe(container, axeOptions);
expect(results).toHaveNoViolations();
});
});
}
/**
*
* @param {Story} story
* @param {any} props
* @param {StoryDecorator[]} [decorators]
* @param {Args} [globals]
*/
function renderWithDecorators(story, props, decorators, globals = {}) {
const decorated = (decorators || []).reverse().reduce(
(elem, decorator) => () => decorator(elem, { globals }),
() => createElement(story, props)
);
return render(decorated());
}