test: add test helper for accessibility

As proposed in #22343, the components can be automatically validated
for accessibility. This adds a test helper that runs Axe to check
for accessibility violations.
This commit is contained in:
Stef Schenkelaars 2021-12-02 13:25:31 +01:00
parent e49a451650
commit 98e36aa036
No known key found for this signature in database
GPG Key ID: 74852D1171B29F81
6 changed files with 24 additions and 0 deletions

View File

@ -4,6 +4,7 @@ import Affix, { AffixProps, AffixState } from '..';
import { getObserverEntities } from '../utils';
import Button from '../../button';
import rtlTest from '../../../tests/shared/rtlTest';
import accessibilityTest from '../../../tests/shared/accessibilityTest';
import { sleep } from '../../../tests/utils';
const events: Partial<Record<keyof HTMLElementEventMap, (ev: Partial<Event>) => void>> = {};
@ -53,6 +54,7 @@ class AffixMounter extends React.Component<{
describe('Affix Render', () => {
rtlTest(Affix);
accessibilityTest(Affix);
const domMock = jest.spyOn(HTMLElement.prototype, 'getBoundingClientRect');
let affixMounterWrapper: ReactWrapper<unknown, unknown, AffixMounter>;

View File

@ -5,12 +5,14 @@ import Button from '../../button';
import Tooltip from '../../tooltip';
import Popconfirm from '../../popconfirm';
import rtlTest from '../../../tests/shared/rtlTest';
import accessibilityTest from '../../../tests/shared/accessibilityTest';
import { sleep } from '../../../tests/utils';
const { ErrorBoundary } = Alert;
describe('Alert', () => {
rtlTest(Alert);
accessibilityTest(Alert);
beforeAll(() => {
jest.useFakeTimers();

View File

@ -166,6 +166,7 @@
"@types/enzyme": "^3.10.5",
"@types/gtag.js": "^0.0.8",
"@types/jest": "^27.0.0",
"@types/jest-axe": "^3.5.3",
"@types/jest-environment-puppeteer": "^4.4.0",
"@types/jest-image-snapshot": "^4.1.0",
"@types/lodash": "^4.14.139",
@ -225,6 +226,7 @@
"intersection-observer": "^0.12.0",
"isomorphic-fetch": "^3.0.0",
"jest": "^27.0.3",
"jest-axe": "^5.0.1",
"jest-image-snapshot": "^4.5.1",
"jest-puppeteer": "^6.0.0",
"jquery": "^3.4.1",

View File

@ -1,5 +1,7 @@
import { toHaveNoViolations } from 'jest-axe';
import toMatchRenderedSnapshot from './matchers/rendered-snapshot';
expect.extend(toHaveNoViolations);
expect.extend({
toMatchRenderedSnapshot,
});

View File

@ -0,0 +1,15 @@
import React from 'react';
import { mount } from 'enzyme';
import { axe } from 'jest-axe';
// eslint-disable-next-line jest/no-export
export default function accessibilityTest(Component: React.ComponentType) {
describe(`accessibility`, () => {
it(`component does not have any violations`, async () => {
jest.useRealTimers();
const wrapper = mount(<Component />);
const results = await axe(wrapper.getDOMNode());
expect(results).toHaveNoViolations();
});
});
}

1
typings/jest.d.ts vendored
View File

@ -1,5 +1,6 @@
declare namespace jest {
interface Matchers<R> {
toMatchRenderedSnapshot(): R;
toHaveNoViolations(): R;
}
}