Skip to content

Commit

Permalink
test: add a11y test using jest.axe
Browse files Browse the repository at this point in the history
  • Loading branch information
jmfrancois committed Oct 17, 2023
1 parent aa3e2e0 commit 9f79d0f
Show file tree
Hide file tree
Showing 32 changed files with 1,084 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ jest.mock('@talend/utils', () => {

describe('Accordion', () => {
it('should render a11y html', async () => {
// note we need to add the aria-label to be accessible
// TODO: make it required
const { container } = render(
<main>
<Accordion>
Expand Down
52 changes: 52 additions & 0 deletions packages/design-system/src/components/Badge/Badge.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { describe, it, expect } from '@jest/globals';
import { axe } from 'jest-axe';
import { render } from '@testing-library/react';
import { Badge, BadgeDropdown } from './';

jest.mock('@talend/utils', () => {
let i = 0;
return {
// we need stable but different uuid (is fixed to 42 by current mock)
randomUUID: () => `mocked-uuid-${i++}`,
};
});

describe('Badge', () => {
it('should render a11y html', async () => {
const selectedValue = '3';
const setSelectedValue = jest.fn();
const { container } = render(
<main>
<div data-testid="Badge">
<Badge
label="Awesome"
selectedId={selectedValue}
value={[
{ id: '1', label: 'Feature' },
{ id: '2', label: 'Item' },
{ id: '3', label: 'Component' },
]}
onChange={setSelectedValue}
variant="dropdown"
/>
</div>
<div data-testid="BadgeDropdown">
<BadgeDropdown
label="Awesome"
selectedId={selectedValue}
value={[
{ id: '1', label: 'Feature' },
{ id: '2', label: 'Item' },
{ id: '3', label: 'Component' },
]}
onChange={setSelectedValue}
/>
</div>
TODO: add popover and tag
</main>,
);
expect(container.firstChild).toMatchSnapshot();
const results = await axe(document.body);
expect(results).toHaveNoViolations();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,273 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Badge should render a11y html 1`] = `
<main>
<div
data-testid="Badge"
>
<span
class="theme-badge"
>
<div
class="theme-stack theme-justify-start theme-align-center theme-nowrap theme-row theme-inline theme-gap-x-XXS theme-gap-y-XXS theme-padding-top-0 theme-padding-right-XXS theme-padding-bottom-0 theme-padding-left-XXS"
>
<span
class="theme-badge__label"
data-test="badge-label"
data-testid="badge-label"
>
Awesome
</span>
<span
class="theme-badge__divider"
data-testid="badge-divider"
>
<hr
aria-orientation="vertical"
class="theme-divider"
orientation="vertical"
/>
</span>
<button
aria-expanded="false"
aria-pressed="false"
class="theme-clickable theme-badge__button"
data-test="dropdown.button.badge-button"
data-testid="dropdown.button.badge-button"
type="button"
>
<span
class="theme-stack theme-justify-start theme-align-center theme-nowrap theme-row theme-block theme-gap-x-XS theme-gap-y-XS"
>
Component
<span
class="theme-badge-dropdown__button__caret"
>
<svg
aria-hidden="true"
pointer-events="none"
shape-rendering="geometricPrecision"
style="width: 1.2rem; height: 1.2rem;"
>
<use
xlink:href="#chevron-down:S"
/>
</svg>
</span>
</span>
</button>
<div
aria-label="Select item"
aria-orientation="vertical"
class="theme-dropdownShell"
data-test="dropdown.menu"
data-testid="dropdown.menu"
role="menu"
style="position: absolute; left: 0px; top: 0px; display: none; transform: translate(0px, 0px);"
tabindex="-1"
>
<div
class="theme-animatedZone"
>
<div
class="theme-stack theme-justify-stretch theme-align-stretch theme-nowrap theme-column theme-block theme-gap-x-0 theme-gap-y-0"
>
<button
class="theme-clickable"
data-test="dropdown.menuitem.Feature-0"
data-testid="dropdown.menuitem.Feature-0"
id="mocked-uuid-6"
role="menuitem"
tabindex="0"
type="button"
>
<div
class="theme-dropdownEntry"
>
<span
class="theme-buttonContent"
>
Feature
</span>
</div>
</button>
<button
class="theme-clickable"
data-test="dropdown.menuitem.Item-1"
data-testid="dropdown.menuitem.Item-1"
id="mocked-uuid-7"
role="menuitem"
tabindex="0"
type="button"
>
<div
class="theme-dropdownEntry"
>
<span
class="theme-buttonContent"
>
Item
</span>
</div>
</button>
<button
class="theme-clickable"
data-test="dropdown.menuitem.Component-2"
data-testid="dropdown.menuitem.Component-2"
id="mocked-uuid-8"
role="menuitem"
tabindex="0"
type="button"
>
<div
class="theme-dropdownEntry"
>
<span
class="theme-buttonContent"
>
Component
</span>
</div>
</button>
</div>
</div>
</div>
</div>
</span>
</div>
<div
data-testid="BadgeDropdown"
>
<span
class="theme-badge"
>
<div
class="theme-stack theme-justify-start theme-align-center theme-nowrap theme-row theme-inline theme-gap-x-XXS theme-gap-y-XXS theme-padding-top-0 theme-padding-right-XXS theme-padding-bottom-0 theme-padding-left-XXS"
>
<span
class="theme-badge__label"
data-test="badge-label"
data-testid="badge-label"
>
Awesome
</span>
<span
class="theme-badge__divider"
data-testid="badge-divider"
>
<hr
aria-orientation="vertical"
class="theme-divider"
orientation="vertical"
/>
</span>
<button
aria-expanded="false"
aria-pressed="false"
class="theme-clickable theme-badge__button"
data-test="dropdown.button.badge-button"
data-testid="dropdown.button.badge-button"
type="button"
>
<span
class="theme-stack theme-justify-start theme-align-center theme-nowrap theme-row theme-block theme-gap-x-XS theme-gap-y-XS"
>
Component
<span
class="theme-badge-dropdown__button__caret"
>
<svg
aria-hidden="true"
pointer-events="none"
shape-rendering="geometricPrecision"
style="width: 1.2rem; height: 1.2rem;"
>
<use
xlink:href="#chevron-down:S"
/>
</svg>
</span>
</span>
</button>
<div
aria-label="Select item"
aria-orientation="vertical"
class="theme-dropdownShell"
data-test="dropdown.menu"
data-testid="dropdown.menu"
role="menu"
style="position: absolute; left: 0px; top: 0px; display: none; transform: translate(0px, 0px);"
tabindex="-1"
>
<div
class="theme-animatedZone"
>
<div
class="theme-stack theme-justify-stretch theme-align-stretch theme-nowrap theme-column theme-block theme-gap-x-0 theme-gap-y-0"
>
<button
class="theme-clickable"
data-test="dropdown.menuitem.Feature-0"
data-testid="dropdown.menuitem.Feature-0"
id="mocked-uuid-9"
role="menuitem"
tabindex="0"
type="button"
>
<div
class="theme-dropdownEntry"
>
<span
class="theme-buttonContent"
>
Feature
</span>
</div>
</button>
<button
class="theme-clickable"
data-test="dropdown.menuitem.Item-1"
data-testid="dropdown.menuitem.Item-1"
id="mocked-uuid-10"
role="menuitem"
tabindex="0"
type="button"
>
<div
class="theme-dropdownEntry"
>
<span
class="theme-buttonContent"
>
Item
</span>
</div>
</button>
<button
class="theme-clickable"
data-test="dropdown.menuitem.Component-2"
data-testid="dropdown.menuitem.Component-2"
id="mocked-uuid-11"
role="menuitem"
tabindex="0"
type="button"
>
<div
class="theme-dropdownEntry"
>
<span
class="theme-buttonContent"
>
Component
</span>
</div>
</button>
</div>
</div>
</div>
</div>
</span>
</div>
TODO: add popover and tag
</main>
`;
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { forwardRef, Ref } from 'react';
import classnames from 'classnames';
import styles from './BadgeButton.module.scss';
import { DataAttributes } from 'src/types';
import Clickable from '../../Clickable/Clickable';
import { Clickable } from '../../Clickable/Clickable';

type BadgeButtonProps = {
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { describe, it, expect } from '@jest/globals';
import { axe } from 'jest-axe';
import { render } from '@testing-library/react';
import { Breadcrumbs } from './';

jest.mock('@talend/utils', () => {
let i = 0;
return {
// we need stable but different uuid (is fixed to 42 by current mock)
randomUUID: () => `mocked-uuid-${i++}`,
};
});

describe('Breadcrumbs', () => {
it('should render a11y html', async () => {
const { container } = render(
<main>
<Breadcrumbs
items={[
{
label: 'Link example',
href: '/',
},
{
label: 'Link example',
href: '/here',
},
{
label: 'Link example',
href: '/there',
target: '_blank',
},
{
label: 'Link example',
href: '/away',
},
{
label:
'Link example that is much too long and should create an ellipsis if all is well',
href: '/more',
},
{
label: 'Label',
href: '/here',
},
]}
/>
</main>,
);
expect(container.firstChild).toMatchSnapshot();
const results = await axe(document.body);
expect(results).toHaveNoViolations();
});
});
Loading

0 comments on commit 9f79d0f

Please sign in to comment.