-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Install and implement Jest-Axe for accessibility testing in unit tests
- Loading branch information
1 parent
77bab1a
commit 8f537f8
Showing
12 changed files
with
278 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
import '@testing-library/jest-dom'; | ||
import { toHaveNoViolations } from 'jest-axe'; | ||
|
||
jest.mock('./src/providers/RedbackUiThemeProvider/GlobalStyle.tsx', () => ({ | ||
GlobalStyle: () => null | ||
})); | ||
|
||
// Ref: https://github.com/NickColley/jest-axe?tab=readme-ov-file#testing-react-with-react-testing-library | ||
expect.extend(toHaveNoViolations); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,3 +20,4 @@ export const renderWithDeps = (component: ReactNode) => { | |
</RedbackUiThemeProvider> | ||
</DiProvider>); | ||
}; | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,31 @@ | ||
import { screen } from '@testing-library/react'; | ||
import { renderWithDeps } from '../../../jest.utils'; | ||
import { Alert } from './Alert'; | ||
import { axe } from 'jest-axe'; | ||
|
||
describe('<Alert />', () => { | ||
it('renders', () => { | ||
renderWithDeps(<Alert>Example alert</Alert>); | ||
|
||
expect( screen.getByText('Example alert')).toBeInTheDocument(); | ||
}); | ||
|
||
it('has no accessibility violations', async () => { | ||
const { container } = renderWithDeps(<Alert>Example alert</Alert>); | ||
const results = await axe(container); | ||
|
||
expect(results).toHaveNoViolations(); | ||
}); | ||
|
||
describe('default theme accessibility', () => { | ||
['success', 'info', 'warning', 'error'].forEach(type => { | ||
it(`has no accessibility violations for type "${type}" in default theme`, async () => { | ||
// @ts-expect-error TS2322: Type string is not assignable to type 'success' | 'info' | 'warning' | 'error' | undefined | ||
const { container } = renderWithDeps(<Alert type={type}>Example alert</Alert>); | ||
const results = await axe(container); | ||
|
||
expect(results).toHaveNoViolations(); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,31 @@ | ||
import { screen } from '@testing-library/react'; | ||
import { renderWithDeps } from '../../../jest.utils'; | ||
import { Label } from './Label'; | ||
import { axe } from 'jest-axe'; | ||
|
||
describe('<Label />', () => { | ||
it('renders', () => { | ||
renderWithDeps(<Label type="info" text="Test label"/>); | ||
|
||
expect(screen.getByText('Test label')).toBeVisible(); | ||
}); | ||
|
||
it('has no accessibility violations', async () => { | ||
const { container } = renderWithDeps(<Label type="info" text="Test label"/>); | ||
const results = await axe(container); | ||
|
||
expect(results).toHaveNoViolations(); | ||
}); | ||
|
||
describe('default theme accessibility', () => { | ||
['success', 'info', 'warning', 'error'].forEach(type => { | ||
it(`has no accessibility violations for type "${type}" in default theme`, async () => { | ||
// @ts-expect-error TS2322: Type string is not assignable to type 'success' | 'info' | 'warning' | 'error' | undefined | ||
const { container } = renderWithDeps(<Label type={type} text="Test label"/>); | ||
const results = await axe(container); | ||
|
||
expect(results).toHaveNoViolations(); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,41 @@ | ||
import { screen } from '@testing-library/react'; | ||
import { renderWithDeps } from '../../../jest.utils'; | ||
import { LinkButton } from './LinkButton'; | ||
import { axe } from 'jest-axe'; | ||
import { themes } from '../../themes'; | ||
import { ThemeColor } from '../../types'; | ||
|
||
describe('<LinkButton />', () => { | ||
it('renders', () => { | ||
renderWithDeps(<LinkButton label="Test link" href="#" />); | ||
|
||
expect(screen.getByRole('link', { name: 'Test link' })).toBeVisible(); | ||
}); | ||
|
||
it('has no accessibility violations (default props)', async () => { | ||
const { container } = renderWithDeps(<LinkButton label="Test link" href="#" />); | ||
const results = await axe(container); | ||
|
||
expect(results).toHaveNoViolations(); | ||
}); | ||
|
||
describe('default theme accessibility', () => { | ||
Object.keys(themes.default.colors).forEach((color: ThemeColor) => { | ||
it(`has no accessibility violations for "${String(color)}" colour in default theme - solid style`, async () => { | ||
const { container } = renderWithDeps(<LinkButton color={color} appearance="solid" label="Test link" href="#" />); | ||
const results = await axe(container); | ||
|
||
expect(results).toHaveNoViolations(); | ||
}); | ||
}); | ||
|
||
Object.keys(themes.default.colors).forEach((color: ThemeColor) => { | ||
it(`has no accessibility violations for "${String(color)}" colour in default theme - outline stype`, async () => { | ||
const { container } = renderWithDeps(<LinkButton color={color} appearance="outline" label="Test link" href="#" />); | ||
const results = await axe(container); | ||
|
||
expect(results).toHaveNoViolations(); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.