Skip to content

Commit

Permalink
test(a11y): add a11y testing (based on axe-core)
Browse files Browse the repository at this point in the history
  • Loading branch information
sabertazimi committed Apr 16, 2022
1 parent 32e00d6 commit 15dfee9
Show file tree
Hide file tree
Showing 27 changed files with 325 additions and 55 deletions.
8 changes: 4 additions & 4 deletions src/components/Article/Article.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,26 @@ describe('Article', () => {
<Article post={mockPost} commentUrl={mockUrl} socialUrl={mockUrl} />
);

const results = await axe(container, {
const a11y = await axe(container, {
rules: {
'nested-interactive': { enabled: false },
},
});

expect(results).toHaveNoViolations();
expect(a11y).toHaveNoViolations();
});

test('should render accessibility guidelines (AXE) with partial data', async () => {
const { container } = render(
<Article post={mockBasePost} commentUrl={mockUrl} socialUrl={mockUrl} />
);

const results = await axe(container, {
const a11y = await axe(container, {
rules: {
'nested-interactive': { enabled: false },
},
});

expect(results).toHaveNoViolations();
expect(a11y).toHaveNoViolations();
});
});
4 changes: 2 additions & 2 deletions src/components/Article/ArticleComments.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ describe('ArticleComments', () => {
test('Should render accessibility guidelines (AXE)', async () => {
const { container } = render(<ArticleComments url={mockUrl} />);

const results = await axe(container);
const a11y = await axe(container);

expect(results).toHaveNoViolations();
expect(a11y).toHaveNoViolations();
});
});
9 changes: 9 additions & 0 deletions src/components/Article/ArticleToc.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import MockData from '@MockData';
import { fireEvent, render, screen } from '@testing-library/react';
import { axe } from 'jest-axe';
import React from 'react';
import { create } from 'react-test-renderer';
import ArticleToc from './ArticleToc';
Expand All @@ -13,6 +14,14 @@ describe('ArticleToc', () => {
expect(tree).toMatchSnapshot();
});

test('Should render accessibility guidelines (AXE)', async () => {
const { container } = render(<ArticleToc toc={mockToc} />);

const a11y = await axe(container);

expect(a11y).toHaveNoViolations();
});

test('should expand ToC when clicked', () => {
render(<ArticleToc toc={mockToc} />);

Expand Down
10 changes: 10 additions & 0 deletions src/components/BooksGrid/BooksGrid.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import MockData from '@MockData';
import { render } from '@testing-library/react';
import { axe } from 'jest-axe';
import React from 'react';
import { create } from 'react-test-renderer';
import BooksGrid from './BooksGrid';
Expand All @@ -11,4 +13,12 @@ describe('BooksGrid', () => {

expect(tree).toMatchSnapshot();
});

test('Should render accessibility guidelines (AXE)', async () => {
const { container } = render(<BooksGrid bookList={mockBooks} />);

const a11y = await axe(container);

expect(a11y).toHaveNoViolations();
});
});
13 changes: 13 additions & 0 deletions src/components/Container/Container.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { render, screen } from '@testing-library/react';
import { axe } from 'jest-axe';
import React from 'react';
import { create } from 'react-test-renderer';
import Container from './Container';
Expand All @@ -14,6 +15,18 @@ describe('Container', () => {
expect(tree).toMatchSnapshot();
});

test('should render accessibility guidelines (AXE)', async () => {
const { container } = render(
<Container role="main">
<h1>Container</h1>
</Container>
);

const a11y = await axe(container);

expect(a11y).toHaveNoViolations();
});

test('should render children correctly', () => {
render(
<Container role="main">
Expand Down
25 changes: 25 additions & 0 deletions src/components/ErrorBoundary/ErrorBoundary.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { render, screen } from '@testing-library/react';
import { axe } from 'jest-axe';
import React from 'react';
import { create } from 'react-test-renderer';
import ErrorBoundary from './ErrorBoundary';
Expand Down Expand Up @@ -47,6 +48,30 @@ describe('ErrorBoundary', () => {
expect(tree).toMatchSnapshot();
});

test('should render children accessibility guidelines (AXE)', async () => {
const { container } = render(
<ErrorBoundary>
<ComponentWithError />
</ErrorBoundary>
);

const a11y = await axe(container);

expect(a11y).toHaveNoViolations();
});

test('should render alert message accessibility guidelines (AXE)', async () => {
const { container } = render(
<ErrorBoundary>
<ComponentWithError shouldThrow />
</ErrorBoundary>
);

const a11y = await axe(container);

expect(a11y).toHaveNoViolations();
});

test('should render alert message when error happened', () => {
const { rerender } = render(<ComponentWithError />, {
wrapper: ErrorBoundary,
Expand Down
13 changes: 13 additions & 0 deletions src/components/FlexContainer/FlexContainer.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { render, screen } from '@testing-library/react';
import { axe } from 'jest-axe';
import React from 'react';
import { create } from 'react-test-renderer';
import FlexContainer from './FlexContainer';
Expand All @@ -25,4 +26,16 @@ describe('FlexContainer', () => {
screen.getByText('FlexContainer')
);
});

test('should render accessibility guidelines (AXE)', async () => {
const { container } = render(
<FlexContainer role="main">
<h1>FlexContainer</h1>
</FlexContainer>
);

const a11y = await axe(container);

expect(a11y).toHaveNoViolations();
});
});
16 changes: 16 additions & 0 deletions src/components/Footer/Footer.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import MockData from '@MockData';
import { render } from '@testing-library/react';
import { axe } from 'jest-axe';
import React from 'react';
import { create } from 'react-test-renderer';
import Footer from './Footer';
Expand All @@ -19,4 +21,18 @@ describe('Footer', () => {

expect(tree).toMatchSnapshot();
});

test('should render accessibility guidelines (AXE)', async () => {
const { container } = render(
<Footer
buildTime={mockTime}
author={mockAuthor}
socialList={mockSocialList}
/>
);

const a11y = await axe(container);

expect(a11y).toHaveNoViolations();
});
});
36 changes: 35 additions & 1 deletion src/components/GithubCard/GithubCard.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import MockData from '@MockData';
import { render } from '@testing-library/react';
import { axe } from 'jest-axe';
import React from 'react';
import { create } from 'react-test-renderer';
import GithubCard from './GithubCard';
Expand Down Expand Up @@ -29,9 +31,41 @@ describe('GithubCard', () => {
expect(tree).toMatchSnapshot();
});

test('should render correctly when missing github data (snapshot)', () => {
test('should render correctly when missing GitHub data (snapshot)', () => {
const tree = create(<GithubCard email={mockEmail} />).toJSON();

expect(tree).toMatchSnapshot();
});

test('should render accessibility guidelines (AXE)', async () => {
const { container } = render(
<GithubCard email={mockEmail} profile={mockProfile} repos={mockRepos} />
);

const a11y = await axe(container);

expect(a11y).toHaveNoViolations();
});

test('should render accessibility guidelines when missing bio and location data (AXE)', async () => {
const { container } = render(
<GithubCard
email={mockEmail}
profile={mockBaseProfile}
repos={mockRepos}
/>
);

const a11y = await axe(container);

expect(a11y).toHaveNoViolations();
});

test('should render accessibility guidelines when missing GitHub data (AXE)', async () => {
const { container } = render(<GithubCard email={mockEmail} />);

const a11y = await axe(container);

expect(a11y).toHaveNoViolations();
});
});
2 changes: 1 addition & 1 deletion src/components/GithubCard/GithubCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const GithubCard = ({ email, profile, repos }: Props): JSX.Element => {
>
<Card hoverable title={<GithubCardHeader profile={profile} />}>
<Card.Meta
avatar={<Avatar src={profile.avatar} />}
avatar={<Avatar src={profile.avatar} alt={profile.username} />}
title={<GithubCardContent profile={profile} />}
description={`Joined in ${profile.createDate}`}
/>
Expand Down
50 changes: 26 additions & 24 deletions src/components/GithubCard/__snapshots__/GithubCard.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ exports[`GithubCard should render correctly (snapshot) 1`] = `
style={Object {}}
>
<img
alt="sabertazimi"
onError={[Function]}
src="https://avatars.githubusercontent.com/u/12670482?v=4"
/>
Expand Down Expand Up @@ -588,6 +589,30 @@ exports[`GithubCard should render correctly (snapshot) 1`] = `
</div>
`;

exports[`GithubCard should render correctly when missing GitHub data (snapshot) 1`] = `
<div
className="mx-auto my-0 text-center"
>
<h1
className="ant-typography"
onClick={null}
style={
Object {
"WebkitLineClamp": undefined,
}
}
>
Please mail to
<a
href="mailto:[email protected]"
>
me
</a>
.
</h1>
</div>
`;

exports[`GithubCard should render correctly when missing bio and location data (snapshot) 1`] = `
<div
className="ant-ribbon-wrapper"
Expand Down Expand Up @@ -651,6 +676,7 @@ exports[`GithubCard should render correctly when missing bio and location data (
style={Object {}}
>
<img
alt="sabertazimi"
onError={[Function]}
src="https://avatars.githubusercontent.com/u/12670482?v=4"
/>
Expand Down Expand Up @@ -1175,27 +1201,3 @@ exports[`GithubCard should render correctly when missing bio and location data (
</div>
</div>
`;

exports[`GithubCard should render correctly when missing github data (snapshot) 1`] = `
<div
className="mx-auto my-0 text-center"
>
<h1
className="ant-typography"
onClick={null}
style={
Object {
"WebkitLineClamp": undefined,
}
}
>
Please mail to
<a
href="mailto:[email protected]"
>
me
</a>
.
</h1>
</div>
`;
25 changes: 14 additions & 11 deletions src/components/Icons/Icons.test.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
import { render } from '@testing-library/react';
import { axe } from 'jest-axe';
import React from 'react';
import { create } from 'react-test-renderer';
import Close from './Close';
import Comment from './Comment';
import Hamburger from './Hamburger';

describe('Icons', () => {
test('should render [Close] icon correctly (snapshot)', () => {
const tree = create(<Close />).toJSON();
const Icons = [Close, Comment, Hamburger];

expect(tree).toMatchSnapshot();
});

test('should render [Comment] icon correctly (snapshot)', () => {
const tree = create(<Comment />).toJSON();
test.each(Icons)('should render %# icon correctly (snapshot)', Icon => {
const tree = create(<Icon />).toJSON();

expect(tree).toMatchSnapshot();
});

test('should render [Hamburger] icon correctly (snapshot)', () => {
const tree = create(<Hamburger />).toJSON();
test.each(Icons)(
'should render %# icon accessibility guidelines (AXE)',
async Icon => {
const { container } = render(<Icon />);

expect(tree).toMatchSnapshot();
});
const a11y = await axe(container);

expect(a11y).toHaveNoViolations();
}
);
});
6 changes: 3 additions & 3 deletions src/components/Icons/__snapshots__/Icons.test.tsx.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Icons should render [Close] icon correctly (snapshot) 1`] = `
exports[`Icons should render 0 icon correctly (snapshot) 1`] = `
<span
aria-label="Close"
className="anticon"
Expand All @@ -21,7 +21,7 @@ exports[`Icons should render [Close] icon correctly (snapshot) 1`] = `
</span>
`;

exports[`Icons should render [Comment] icon correctly (snapshot) 1`] = `
exports[`Icons should render 1 icon correctly (snapshot) 1`] = `
<span
aria-label="Comment"
className="anticon"
Expand Down Expand Up @@ -50,7 +50,7 @@ exports[`Icons should render [Comment] icon correctly (snapshot) 1`] = `
</span>
`;

exports[`Icons should render [Hamburger] icon correctly (snapshot) 1`] = `
exports[`Icons should render 2 icon correctly (snapshot) 1`] = `
<span
aria-label="Hamburger"
className="anticon"
Expand Down
Loading

0 comments on commit 15dfee9

Please sign in to comment.