Skip to content
This repository was archived by the owner on Mar 5, 2022. It is now read-only.

Commit 7613e48

Browse files
authored
Add a11y example (#260)
1 parent 29c60fe commit 7613e48

File tree

14 files changed

+132
-2
lines changed

14 files changed

+132
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ We have several subfolders in [examples](examples) folder that have complete pro
164164
<!-- prettier-ignore-start -->
165165
Folder Name | Description
166166
--- | ---
167+
[a11y](examples/a11y) | Testing components' accessibility using [cypress-axe](https://github.com/avanslaars/cypress-axe)
167168
[react-scripts](examples/react-scripts) | A project using `react-scripts` with component tests in `src` folder
168169
[react-scripts-folder](examples/react-scripts-folder) | A project using `react-scripts` with component tests in `cypress/component`
169170
[tailwind](examples/tailwind) | Testing styles built using [Tailwind CSS](https://tailwindcss.com/)

circle.yml

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ workflows:
3131
name: Build folder 🏗
3232
command: npm run build
3333

34+
- cypress/run:
35+
name: Example A11y
36+
requires:
37+
- Install
38+
# each example installs "cypress-react-unit-test" as a local dependency (symlink)
39+
install-command: npm install
40+
verify-command: echo 'Already verified'
41+
no-workspace: true
42+
working_directory: examples/a11y
43+
command: npm test
44+
store_artifacts: true
45+
3446
- cypress/run:
3547
name: Example Babel
3648
requires:
@@ -239,10 +251,11 @@ workflows:
239251
requires:
240252
- Install
241253
- Test
242-
- Example Sass
254+
- Example A11y
243255
- Example Babel
244-
- Example React Scripts
245256
- Example Component Folder
257+
- Example React Scripts
258+
- Example Sass
246259
- Example Tailwind
247260
- Example Webpack options
248261
- Visual Sudoku

examples/a11y/.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

examples/a11y/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# example: a11y
2+
3+
> Testing component accessibility
4+
5+
```shell
6+
npm install
7+
npm run cy:open
8+
# or just run headless tests
9+
npm test
10+
```
11+
12+
Testing components following the [React accessibility guide](https://reactjs.org/docs/accessibility.html) using [cypress-axe](https://github.com/avanslaars/cypress-axe) plugin.
13+
14+
See the spec file [cypress/component/spec.js](cypress/component/spec.js). For example, an `<input>` without a label is caught:
15+
16+
```js
17+
mount(<input type="text" value="John Smith" name="name" />)
18+
cy.checkA11y('input', {
19+
runOnly: {
20+
type: 'tag',
21+
values: ['wcag2a'],
22+
},
23+
})
24+
```
25+
26+
![Input without a label](images/missing-label.png)
27+
28+
You can click on the error to see more details in the DevTools console
29+
30+
![Error details](images/label-error.png)

examples/a11y/cypress.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"fixturesFolder": false,
3+
"testFiles": "**/*spec.js",
4+
"viewportWidth": 500,
5+
"viewportHeight": 500,
6+
"experimentalComponentTesting": true
7+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/// <reference types="cypress" />
2+
import React from 'react'
3+
import { mount } from 'cypress-react-unit-test'
4+
5+
describe('Accessibility', () => {
6+
before(() => {
7+
// https://github.com/avanslaars/cypress-axe
8+
cy.injectAxe()
9+
})
10+
11+
// https://www.w3.org/WAI/standards-guidelines/aria/
12+
context('aria', () => {
13+
// skip a failing test on purpose
14+
it.skip('catches missing aria-* label', () => {
15+
mount(<input type="text" value="John Smith" name="name" />)
16+
cy.checkA11y('input', {
17+
runOnly: {
18+
type: 'tag',
19+
values: ['wcag2a'],
20+
},
21+
})
22+
})
23+
24+
it('passes', () => {
25+
mount(
26+
<input
27+
type="text"
28+
aria-label="label text"
29+
aria-required="true"
30+
value="John Smith"
31+
name="name"
32+
/>,
33+
)
34+
cy.checkA11y('input', {
35+
runOnly: {
36+
type: 'tag',
37+
values: ['wcag2a'],
38+
},
39+
})
40+
})
41+
})
42+
})
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/// <reference types="cypress" />
2+
describe('integration spec', () => {
3+
it('works', () => {
4+
expect(1).to.equal(1)
5+
})
6+
})
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// load file preprocessor that comes with this plugin
2+
// https://github.com/bahmutov/cypress-react-unit-test#install
3+
const preprocessor = require('cypress-react-unit-test/plugins/react-scripts')
4+
module.exports = (on, config) => {
5+
preprocessor(on, config)
6+
// IMPORTANT to return the config object
7+
// with the any changed environment variables
8+
return config
9+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require('cypress-react-unit-test/support')
2+
require('cypress-axe')
372 KB
Loading

0 commit comments

Comments
 (0)