-
Notifications
You must be signed in to change notification settings - Fork 0
test: #17 axe-core over every state of the search page #60
Merged
+196
−2
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| import { describe, it, expect } from 'vitest'; | ||
| import { fireEvent, render, screen, waitFor } from '@testing-library/react'; | ||
| import { MockedProvider } from '@apollo/client/testing/react'; | ||
| import type { MockedResponse } from '@apollo/client/testing'; | ||
| import { GraphQLError } from 'graphql'; | ||
| import axe from 'axe-core'; | ||
|
|
||
| import App from './App.js'; | ||
| import { SEARCH_ALBUMS } from './api/searchAlbums.js'; | ||
| import type { | ||
| AlbumSummary, | ||
| SearchAlbumsData, | ||
| SearchAlbumsVariables, | ||
| } from './api/searchAlbums.js'; | ||
|
|
||
| const album: AlbumSummary = { | ||
| id: '1', | ||
| title: 'Abbey Road', | ||
| artists: [{ id: '1', name: 'The Beatles' }], | ||
| releaseYear: 1969, | ||
| genres: [{ id: '1', name: 'Rock' }], | ||
| averageRating: 4.8, | ||
| reviewCount: 1240, | ||
| }; | ||
|
|
||
| function mockSearch( | ||
| query: string, | ||
| albums: AlbumSummary[] = [album], | ||
| ): MockedResponse<SearchAlbumsData, SearchAlbumsVariables> { | ||
| return { | ||
| request: { | ||
| query: SEARCH_ALBUMS, | ||
| variables: { input: { query, sort: 'RELEVANCE' }, first: 20, after: null }, | ||
| }, | ||
| result: { | ||
| data: { | ||
| searchAlbums: { | ||
| edges: albums.map((node) => ({ cursor: node.id, node })), | ||
| totalCount: albums.length, | ||
| pageInfo: { hasNextPage: false, endCursor: null }, | ||
| }, | ||
| }, | ||
| }, | ||
| delay: 10, | ||
| }; | ||
| } | ||
|
|
||
| function renderApp( | ||
| mocks: MockedResponse<SearchAlbumsData, SearchAlbumsVariables>[] = [], | ||
| ) { | ||
| return render( | ||
| <MockedProvider mocks={mocks}> | ||
| <App /> | ||
| </MockedProvider>, | ||
| ); | ||
| } | ||
|
|
||
| function submitSearch(query: string) { | ||
| fireEvent.change(screen.getByRole('searchbox'), { target: { value: query } }); | ||
| fireEvent.submit(screen.getByRole('search')); | ||
| } | ||
|
|
||
| /** | ||
| * Runs axe over the rendered tree and fails with a readable list if anything | ||
| * is wrong. | ||
| * | ||
| * `color-contrast` is off because jsdom does no layout or painting, so axe | ||
| * has nothing to measure a rendered contrast ratio against and would report | ||
| * every pair as incomplete rather than passing. Contrast is verified by hand | ||
| * in a browser, which is where the numbers in the README come from. Every | ||
| * other rule axe can evaluate runs here. | ||
| */ | ||
| async function expectNoViolations(container: HTMLElement) { | ||
| const results = await axe.run(container, { | ||
| rules: { 'color-contrast': { enabled: false } }, | ||
| }); | ||
|
|
||
| // Guard against a false pass: if axe examined nothing it would report a | ||
| // clean tree whatever the markup looks like. | ||
| expect(results.passes.length).toBeGreaterThan(0); | ||
|
|
||
| const summary = results.violations.map( | ||
| (violation) => | ||
| `${violation.id}: ${violation.help} (${violation.nodes.length} node(s)) -> ${violation.nodes[0]?.html}`, | ||
| ); | ||
|
|
||
| expect(summary).toEqual([]); | ||
| } | ||
|
|
||
| describe('axe-core on the search page', () => { | ||
| it('is clean before anything is searched', async () => { | ||
| const { container } = renderApp(); | ||
| await expectNoViolations(container); | ||
| }); | ||
|
|
||
| it('is clean with results rendered', async () => { | ||
| const { container } = renderApp([mockSearch('Abbey')]); | ||
| submitSearch('Abbey'); | ||
|
|
||
| await screen.findByRole('region', { name: 'Search results' }); | ||
| await waitFor(() => | ||
| expect(screen.getByText(/Showing 1 of 1 albums/)).toBeInTheDocument(), | ||
| ); | ||
|
|
||
| await expectNoViolations(container); | ||
| }); | ||
|
|
||
| it('is clean with no results', async () => { | ||
| const { container } = renderApp([mockSearch('zzzz', [])]); | ||
| submitSearch('zzzz'); | ||
|
|
||
| await screen.findByRole('heading', { name: 'No albums found' }); | ||
| await expectNoViolations(container); | ||
| }); | ||
|
|
||
| it('is clean when the search fails', async () => { | ||
| const failing: MockedResponse<SearchAlbumsData, SearchAlbumsVariables> = { | ||
| request: { | ||
| query: SEARCH_ALBUMS, | ||
| variables: { | ||
| input: { query: 'boom', sort: 'RELEVANCE' }, | ||
| first: 20, | ||
| after: null, | ||
| }, | ||
| }, | ||
| result: { errors: [new GraphQLError('Something went wrong')] }, | ||
| }; | ||
| const { container } = renderApp([failing]); | ||
| submitSearch('boom'); | ||
|
|
||
| await screen.findByText('Could not load albums. Please try again.'); | ||
| await expectNoViolations(container); | ||
| }); | ||
|
|
||
| it('keeps the skip link as the first focusable element on every state', async () => { | ||
| const { container } = renderApp([mockSearch('Abbey')]); | ||
| submitSearch('Abbey'); | ||
| await screen.findByRole('region', { name: 'Search results' }); | ||
|
|
||
| const focusable = container.querySelectorAll<HTMLElement>( | ||
| 'a[href], button, input, [tabindex]', | ||
| ); | ||
| expect(focusable[0]?.classList.contains('skip-link')).toBe(true); | ||
| }); | ||
| }); | ||
This file contains hidden or 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 hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.