feat: add a11y test for components demo (#51414)

* test: add accessibility test utilities

* refactor(tests): replace polling with Promise chain for axe test queue management

* test: add a11y test for components demo

* feat: add a11y test utils

* test: add a11y test for components demo

* test: add a11y test for components demo

* chore: remove unnecessary code

* feat: add polyfill for test environment

* test: add a11y test for components demo

* test: add a11y test for components demo

* chore: adjust code style
This commit is contained in:
Jony J 2024-11-01 11:30:11 +08:00 committed by GitHub
parent a796037a46
commit 08a9325182
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
39 changed files with 260 additions and 5 deletions

View File

@ -1,7 +1,7 @@
import React, { useEffect, useRef } from 'react'; import React, { useEffect, useRef } from 'react';
import Affix from '..'; import Affix from '..';
import accessibilityTest from '../../../tests/shared/accessibilityTest'; import { accessibilityTest } from '../../../tests/shared/accessibilityTest';
import rtlTest from '../../../tests/shared/rtlTest'; import rtlTest from '../../../tests/shared/rtlTest';
import { render, triggerResize, waitFakeTimer } from '../../../tests/utils'; import { render, triggerResize, waitFakeTimer } from '../../../tests/utils';
import Button from '../../button'; import Button from '../../button';

View File

@ -0,0 +1,5 @@
import accessibilityDemoTest from '../../../tests/shared/accessibilityTest';
describe('affix demo a11y', () => {
accessibilityDemoTest('affix');
});

View File

@ -0,0 +1,5 @@
import accessibilityDemoTest from '../../../tests/shared/accessibilityTest';
describe('alert demo a11y', () => {
accessibilityDemoTest('alert', { disabledRules: ['button-name'] });
});

View File

@ -3,7 +3,7 @@ import userEvent from '@testing-library/user-event';
import { resetWarned } from 'rc-util/lib/warning'; import { resetWarned } from 'rc-util/lib/warning';
import Alert from '..'; import Alert from '..';
import accessibilityTest from '../../../tests/shared/accessibilityTest'; import { accessibilityTest } from '../../../tests/shared/accessibilityTest';
import rtlTest from '../../../tests/shared/rtlTest'; import rtlTest from '../../../tests/shared/rtlTest';
import { act, render, screen, waitFakeTimer } from '../../../tests/utils'; import { act, render, screen, waitFakeTimer } from '../../../tests/utils';
import Button from '../../button'; import Button from '../../button';

View File

@ -0,0 +1,5 @@
import accessibilityDemoTest from '../../../tests/shared/accessibilityTest';
describe('anchor demo a11y', () => {
accessibilityDemoTest('anchor');
});

View File

@ -0,0 +1,5 @@
import accessibilityDemoTest from '../../../tests/shared/accessibilityTest';
describe('app demo a11y', () => {
accessibilityDemoTest('app');
});

View File

@ -0,0 +1,5 @@
import accessibilityDemoTest from '../../../tests/shared/accessibilityTest';
describe('avatar demo a11y', () => {
accessibilityDemoTest('avatar', { disabledRules: ['image-alt'] });
});

View File

@ -0,0 +1,5 @@
import accessibilityDemoTest from '../../../tests/shared/accessibilityTest';
describe('back-top demo a11y', () => {
accessibilityDemoTest('back-top');
});

View File

@ -0,0 +1,5 @@
import accessibilityDemoTest from '../../../tests/shared/accessibilityTest';
describe('badge demo a11y', () => {
accessibilityDemoTest('badge', { disabledRules: ['button-name'] });
});

View File

@ -1,7 +1,7 @@
import React from 'react'; import React from 'react';
import { resetWarned } from '../../_util/warning'; import { resetWarned } from '../../_util/warning';
import accessibilityTest from '../../../tests/shared/accessibilityTest'; import { accessibilityTest } from '../../../tests/shared/accessibilityTest';
import mountTest from '../../../tests/shared/mountTest'; import mountTest from '../../../tests/shared/mountTest';
import rtlTest from '../../../tests/shared/rtlTest'; import rtlTest from '../../../tests/shared/rtlTest';
import { render } from '../../../tests/utils'; import { render } from '../../../tests/utils';

View File

@ -0,0 +1,5 @@
import accessibilityDemoTest from '../../../tests/shared/accessibilityTest';
describe('breadcrumb demo a11y', () => {
accessibilityDemoTest('breadcrumb');
});

View File

@ -0,0 +1,5 @@
import accessibilityDemoTest from '../../../tests/shared/accessibilityTest';
describe('button demo a11y', () => {
accessibilityDemoTest('button');
});

View File

@ -0,0 +1,4 @@
import accessibilityDemoTest from '../../../tests/shared/accessibilityTest';
// skip _InternalPanelDoNotUseOrYouWillBeFired
accessibilityDemoTest('color-picker', { skip: ['pure-panel.tsx'] });

View File

@ -0,0 +1,3 @@
import accessibilityDemoTest from '../../../tests/shared/accessibilityTest';
accessibilityDemoTest('descriptions');

View File

@ -0,0 +1,3 @@
import accessibilityDemoTest from '../../../tests/shared/accessibilityTest';
accessibilityDemoTest('divider');

View File

@ -0,0 +1,3 @@
import accessibilityDemoTest from '../../../tests/shared/accessibilityTest';
accessibilityDemoTest('drawer', { disabledRules: ['image-alt'] });

View File

@ -0,0 +1,3 @@
import accessibilityDemoTest from '../../../tests/shared/accessibilityTest';
accessibilityDemoTest('dropdown');

View File

@ -0,0 +1,3 @@
import accessibilityDemoTest from '../../../tests/shared/accessibilityTest';
accessibilityDemoTest('empty', { disabledRules: ['label'] });

View File

@ -0,0 +1,3 @@
import accessibilityDemoTest from '../../../tests/shared/accessibilityTest';
accessibilityDemoTest('float-button', { disabledRules: ['button-name'] });

View File

@ -0,0 +1,3 @@
import accessibilityDemoTest from '../../../tests/shared/accessibilityTest';
accessibilityDemoTest('icon', { disabledRules: ['role-img-alt'] });

View File

@ -0,0 +1,3 @@
import accessibilityDemoTest from '../../../tests/shared/accessibilityTest';
accessibilityDemoTest('image', { disabledRules: ['image-alt', 'label'] });

View File

@ -0,0 +1,3 @@
import accessibilityDemoTest from '../../../tests/shared/accessibilityTest';
accessibilityDemoTest('input-number', { disabledRules: ['label'] });

View File

@ -0,0 +1,3 @@
import accessibilityDemoTest from '../../../tests/shared/accessibilityTest';
accessibilityDemoTest('input', { disabledRules: ['label'] });

View File

@ -0,0 +1,3 @@
import accessibilityDemoTest from '../../../tests/shared/accessibilityTest';
accessibilityDemoTest('mentions', { disabledRules: ['label'] });

View File

@ -0,0 +1,3 @@
import accessibilityDemoTest from '../../../tests/shared/accessibilityTest';
accessibilityDemoTest('menu', { disabledRules: ['button-name'] });

View File

@ -0,0 +1,3 @@
import accessibilityDemoTest from '../../../tests/shared/accessibilityTest';
accessibilityDemoTest('message');

View File

@ -0,0 +1,4 @@
import accessibilityDemoTest from '../../../tests/shared/accessibilityTest';
// skip debug components
accessibilityDemoTest('modal', { skip: ['wireframe.tsx', 'render-panel.tsx'] });

View File

@ -0,0 +1,3 @@
import accessibilityDemoTest from '../../../tests/shared/accessibilityTest';
accessibilityDemoTest('notification', { disabledRules: ['button-name', 'label'] });

View File

@ -0,0 +1,3 @@
import accessibilityDemoTest from '../../../tests/shared/accessibilityTest';
accessibilityDemoTest('popconfirm', { disabledRules: ['button-name'] });

View File

@ -0,0 +1,3 @@
import accessibilityDemoTest from '../../../tests/shared/accessibilityTest';
accessibilityDemoTest('radio');

View File

@ -0,0 +1,3 @@
import accessibilityDemoTest from '../../../tests/shared/accessibilityTest';
accessibilityDemoTest('result');

View File

@ -0,0 +1,3 @@
import accessibilityDemoTest from '../../../tests/shared/accessibilityTest';
accessibilityDemoTest('select', { disabledRules: ['label', 'button-name'] });

View File

@ -0,0 +1,3 @@
import accessibilityDemoTest from '../../../tests/shared/accessibilityTest';
accessibilityDemoTest('space', { disabledRules: ['label', 'button-name'] });

View File

@ -0,0 +1,3 @@
import accessibilityDemoTest from '../../../tests/shared/accessibilityTest';
accessibilityDemoTest('spin', { disabledRules: ['button-name'] });

View File

@ -0,0 +1,3 @@
import accessibilityDemoTest from '../../../tests/shared/accessibilityTest';
accessibilityDemoTest('splitter');

View File

@ -0,0 +1,3 @@
import accessibilityDemoTest from '../../../tests/shared/accessibilityTest';
accessibilityDemoTest('switch', { disabledRules: ['button-name'] });

View File

@ -0,0 +1,3 @@
import accessibilityDemoTest from '../../../tests/shared/accessibilityTest';
accessibilityDemoTest('tag');

View File

@ -0,0 +1,3 @@
import accessibilityDemoTest from '../../../tests/shared/accessibilityTest';
accessibilityDemoTest('timeline');

View File

@ -1,15 +1,145 @@
import React from 'react'; import React from 'react';
import { render } from '@testing-library/react'; import { render } from '@testing-library/react';
import { globSync } from 'glob';
import { axe } from 'jest-axe'; import { axe } from 'jest-axe';
class AxeQueueManager {
private queue: Promise<any> = Promise.resolve();
private isProcessing = false;
async enqueue<T>(task: () => Promise<T>): Promise<T> {
const currentQueue = this.queue;
const newTask = async () => {
try {
await currentQueue;
this.isProcessing = true;
return await task();
} finally {
this.isProcessing = false;
}
};
this.queue = this.queue.then(newTask, newTask);
return this.queue;
}
isRunning(): boolean {
return this.isProcessing;
}
}
const axeQueueManager = new AxeQueueManager();
const runAxe = async (...args: Parameters<typeof axe>): Promise<ReturnType<typeof axe>> => {
return axeQueueManager.enqueue(async () => {
try {
return await axe(...args);
} catch (error) {
console.error('Axe test failed:', error);
throw error;
}
});
};
type Rules = {
[key: string]: {
enabled: boolean;
};
};
const convertRulesToAxeFormat = (rules: string[]): Rules => {
return rules.reduce(
(acc, rule) => ({
...acc,
[rule]: { enabled: false },
}),
{},
);
};
// eslint-disable-next-line jest/no-export // eslint-disable-next-line jest/no-export
export default function accessibilityTest(Component: React.ComponentType) { export function accessibilityTest(Component: React.ComponentType, disabledRules?: string[]) {
beforeAll(() => {
// Fake ResizeObserver
global.ResizeObserver = jest.fn(() => {
return {
observe() {},
unobserve() {},
disconnect() {},
};
}) as jest.Mock;
// fake fetch
global.fetch = jest.fn(() => {
return {
then() {
return this;
},
catch() {
return this;
},
finally() {
return this;
},
};
}) as jest.Mock;
});
beforeEach(() => {
// Reset all mocks
if (global.fetch) {
(global.fetch as jest.Mock).mockClear();
}
});
afterEach(() => {
// Clear all mocks
jest.clearAllMocks();
});
describe(`accessibility`, () => { describe(`accessibility`, () => {
it(`component does not have any violations`, async () => { it(`component does not have any violations`, async () => {
jest.useRealTimers(); jest.useRealTimers();
const { container } = render(<Component />); const { container } = render(<Component />);
const results = await axe(container);
const rules = convertRulesToAxeFormat(disabledRules || []);
const results = await runAxe(container, { rules });
expect(results).toHaveNoViolations(); expect(results).toHaveNoViolations();
}, 30000);
});
}
type Options = {
skip?: boolean | string[];
disabledRules?: string[];
};
// eslint-disable-next-line jest/no-export
export default function accessibilityDemoTest(component: string, options: Options = {}) {
// If skip is true, return immediately without executing any tests
if (options.skip === true) {
describe.skip(`${component} demo a11y`, () => {
it('skipped', () => {});
});
return;
}
describe(`${component} demo a11y`, () => {
const files = globSync(`./components/${component}/demo/*.tsx`).filter(
(file) =>
!file.includes('_semantic') && !file.includes('debug') && !file.includes('component-token'),
);
files.forEach((file) => {
const shouldSkip = Array.isArray(options.skip) && options.skip.some((c) => file.endsWith(c));
const testMethod = shouldSkip ? describe.skip : describe;
testMethod(`Test ${file} accessibility`, () => {
const Demo = require(`../../${file}`).default;
accessibilityTest(Demo, options.disabledRules);
});
}); });
}); });
} }