diff --git a/config/test/setupTests.ts b/config/test/setupTests.ts index e0f7cfd6..f4123ea9 100644 --- a/config/test/setupTests.ts +++ b/config/test/setupTests.ts @@ -1,7 +1,19 @@ +import 'vitest-canvas-mock'; import '@testing-library/jest-dom/vitest'; import { cleanup } from '@testing-library/react'; +import axe from 'axe-core'; import { afterEach } from 'vitest'; +axe.configure({ + checks: [ + { + // Disable color contrast checking, as it doesn't work in jsdom + id: 'color-contrast', + enabled: false, + }, + ], +}); + // Clear all mocks and cleanup DOM after every test afterEach(() => { vi.clearAllMocks(); diff --git a/package-lock.json b/package-lock.json index 49b0c5ed..9525711a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -53,6 +53,7 @@ "@vitejs/plugin-react": "^4.0.4", "@vitest/coverage-v8": "^0.34.5", "adm-zip": "^0.5.10", + "axe-core": "^4.8.2", "chalk": "^5.3.0", "eslint": "^8.50.0", "history": "^5.3.0", @@ -62,7 +63,8 @@ "typescript": "^5.2.2", "vite": "^4.4.9", "vite-plugin-pwa": "^0.16.5", - "vitest": "^0.34.5" + "vitest": "^0.34.5", + "vitest-canvas-mock": "^0.3.3" } }, "node_modules/@aashutoshrathi/word-wrap": { @@ -4261,10 +4263,10 @@ } }, "node_modules/axe-core": { - "version": "4.4.1", + "version": "4.8.2", + "resolved": "https://registry.npmjs.org/axe-core/-/axe-core-4.8.2.tgz", + "integrity": "sha512-/dlp0fxyM3R8YW7MFzaHWXrf4zzbr0vaYb23VBFCl83R7nWNPg/yaQw2Dc8jzCMmDVLhSdzH8MjrsuIUuvX+6g==", "dev": true, - "license": "MPL-2.0", - "peer": true, "engines": { "node": ">=4" } @@ -4798,6 +4800,12 @@ "node": ">=4" } }, + "node_modules/cssfontparser": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/cssfontparser/-/cssfontparser-1.2.1.tgz", + "integrity": "sha512-6tun4LoZnj7VN6YeegOVb67KBX/7JJsqvj+pv3ZA7F878/eN33AbGa5b/S/wXxS/tcp8nc40xRUrsPlxIyNUPg==", + "dev": true + }, "node_modules/cssstyle": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/cssstyle/-/cssstyle-3.0.0.tgz", @@ -6931,6 +6939,16 @@ "node": ">=8" } }, + "node_modules/jest-canvas-mock": { + "version": "2.5.2", + "resolved": "https://registry.npmjs.org/jest-canvas-mock/-/jest-canvas-mock-2.5.2.tgz", + "integrity": "sha512-vgnpPupjOL6+L5oJXzxTxFrlGEIbHdZqFU+LFNdtLxZ3lRDCl17FlTMM7IatoRQkrcyOTMlDinjUguqmQ6bR2A==", + "dev": true, + "dependencies": { + "cssfontparser": "^1.2.1", + "moo-color": "^1.0.2" + } + }, "node_modules/jest-worker": { "version": "26.6.2", "resolved": "https://registry.npmjs.org/jest-worker/-/jest-worker-26.6.2.tgz", @@ -7517,6 +7535,15 @@ "ufo": "^1.3.0" } }, + "node_modules/moo-color": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/moo-color/-/moo-color-1.0.3.tgz", + "integrity": "sha512-i/+ZKXMDf6aqYtBhuOcej71YSlbjT3wCO/4H1j8rPvxDJEifdwgg5MaFyu6iYAT8GBZJg2z0dkgK4YMzvURALQ==", + "dev": true, + "dependencies": { + "color-name": "^1.1.4" + } + }, "node_modules/ms": { "version": "2.0.0", "dev": true, @@ -10001,6 +10028,18 @@ } } }, + "node_modules/vitest-canvas-mock": { + "version": "0.3.3", + "resolved": "https://registry.npmjs.org/vitest-canvas-mock/-/vitest-canvas-mock-0.3.3.tgz", + "integrity": "sha512-3P968tYBpqYyzzOaVtqnmYjqbe13576/fkjbDEJSfQAkHtC5/UjuRHOhFEN/ZV5HVZIkaROBUWgazDKJ+Ibw+Q==", + "dev": true, + "dependencies": { + "jest-canvas-mock": "~2.5.2" + }, + "peerDependencies": { + "vitest": "*" + } + }, "node_modules/vitest/node_modules/debug": { "version": "4.3.4", "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz", @@ -13339,9 +13378,10 @@ "dev": true }, "axe-core": { - "version": "4.4.1", - "dev": true, - "peer": true + "version": "4.8.2", + "resolved": "https://registry.npmjs.org/axe-core/-/axe-core-4.8.2.tgz", + "integrity": "sha512-/dlp0fxyM3R8YW7MFzaHWXrf4zzbr0vaYb23VBFCl83R7nWNPg/yaQw2Dc8jzCMmDVLhSdzH8MjrsuIUuvX+6g==", + "dev": true }, "axobject-query": { "version": "2.2.0", @@ -13689,6 +13729,12 @@ "version": "3.0.0", "dev": true }, + "cssfontparser": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/cssfontparser/-/cssfontparser-1.2.1.tgz", + "integrity": "sha512-6tun4LoZnj7VN6YeegOVb67KBX/7JJsqvj+pv3ZA7F878/eN33AbGa5b/S/wXxS/tcp8nc40xRUrsPlxIyNUPg==", + "dev": true + }, "cssstyle": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/cssstyle/-/cssstyle-3.0.0.tgz", @@ -15114,6 +15160,16 @@ } } }, + "jest-canvas-mock": { + "version": "2.5.2", + "resolved": "https://registry.npmjs.org/jest-canvas-mock/-/jest-canvas-mock-2.5.2.tgz", + "integrity": "sha512-vgnpPupjOL6+L5oJXzxTxFrlGEIbHdZqFU+LFNdtLxZ3lRDCl17FlTMM7IatoRQkrcyOTMlDinjUguqmQ6bR2A==", + "dev": true, + "requires": { + "cssfontparser": "^1.2.1", + "moo-color": "^1.0.2" + } + }, "jest-worker": { "version": "26.6.2", "resolved": "https://registry.npmjs.org/jest-worker/-/jest-worker-26.6.2.tgz", @@ -15539,6 +15595,15 @@ "ufo": "^1.3.0" } }, + "moo-color": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/moo-color/-/moo-color-1.0.3.tgz", + "integrity": "sha512-i/+ZKXMDf6aqYtBhuOcej71YSlbjT3wCO/4H1j8rPvxDJEifdwgg5MaFyu6iYAT8GBZJg2z0dkgK4YMzvURALQ==", + "dev": true, + "requires": { + "color-name": "^1.1.4" + } + }, "ms": { "version": "2.0.0", "dev": true, @@ -17165,6 +17230,15 @@ } } }, + "vitest-canvas-mock": { + "version": "0.3.3", + "resolved": "https://registry.npmjs.org/vitest-canvas-mock/-/vitest-canvas-mock-0.3.3.tgz", + "integrity": "sha512-3P968tYBpqYyzzOaVtqnmYjqbe13576/fkjbDEJSfQAkHtC5/UjuRHOhFEN/ZV5HVZIkaROBUWgazDKJ+Ibw+Q==", + "dev": true, + "requires": { + "jest-canvas-mock": "~2.5.2" + } + }, "w3c-xmlserializer": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/w3c-xmlserializer/-/w3c-xmlserializer-4.0.0.tgz", diff --git a/package.json b/package.json index 35d50766..995cd191 100644 --- a/package.json +++ b/package.json @@ -69,6 +69,7 @@ "@vitejs/plugin-react": "^4.0.4", "@vitest/coverage-v8": "^0.34.5", "adm-zip": "^0.5.10", + "axe-core": "^4.8.2", "chalk": "^5.3.0", "eslint": "^8.50.0", "history": "^5.3.0", @@ -78,7 +79,8 @@ "typescript": "^5.2.2", "vite": "^4.4.9", "vite-plugin-pwa": "^0.16.5", - "vitest": "^0.34.5" + "vitest": "^0.34.5", + "vitest-canvas-mock": "^0.3.3" }, "browserslist": [ ">0.2%", diff --git a/src/servers/ServersListGroup.tsx b/src/servers/ServersListGroup.tsx index b9fd3f41..16dd7fd5 100644 --- a/src/servers/ServersListGroup.tsx +++ b/src/servers/ServersListGroup.tsx @@ -21,9 +21,10 @@ const ServerListItem = ({ id, name }: { id: string; name: string }) => ( export const ServersListGroup: FC = ({ servers, children, embedded = false }) => ( <> - {children &&
{children}
} + {children &&
{children}
} {servers.length > 0 && ( {servers.map(({ id, name }) => )} diff --git a/src/servers/helpers/ServerError.tsx b/src/servers/helpers/ServerError.tsx index 6b80fe45..81967073 100644 --- a/src/servers/helpers/ServerError.tsx +++ b/src/servers/helpers/ServerError.tsx @@ -36,17 +36,19 @@ const ServerError: FCWithDeps = ({ servers, s - These are the Shlink servers currently configured. Choose one of - them or add a new one. +

+ These are the Shlink servers currently configured. Choose one of + them or add a new one. +

{isServerWithId(selectedServer) && (
-
+

Alternatively, if you think you may have miss-configured this server, you can remove it or  edit it. -

+

)} diff --git a/test/__helpers__/accessibility.ts b/test/__helpers__/accessibility.ts new file mode 100644 index 00000000..79f070e3 --- /dev/null +++ b/test/__helpers__/accessibility.ts @@ -0,0 +1,14 @@ +import { screen } from '@testing-library/react'; +import { run } from 'axe-core'; + +type ContainerWrapper = { container: HTMLElement }; + +type AccessibilityTestSubject = ContainerWrapper | Promise; + +export const checkAccessibility = async (subject: AccessibilityTestSubject) => { + const { container } = await subject; + screen.debug(container); + const { violations } = await run(container); + + expect(violations).toStrictEqual([]); +}; diff --git a/test/app/App.test.tsx b/test/app/App.test.tsx index 2466c068..32e52a4d 100644 --- a/test/app/App.test.tsx +++ b/test/app/App.test.tsx @@ -3,6 +3,7 @@ import { fromPartial } from '@total-typescript/shoehorn'; import { createMemoryHistory } from 'history'; import { Router } from 'react-router-dom'; import { AppFactory } from '../../src/app/App'; +import { checkAccessibility } from '../__helpers__/accessibility'; describe('', () => { const App = AppFactory( @@ -34,6 +35,8 @@ describe('', () => { ); }; + it('passes a11y checks', () => checkAccessibility(setUp())); + it('renders children components', () => { setUp(); diff --git a/test/common/AppUpdateBanner.test.tsx b/test/common/AppUpdateBanner.test.tsx index 023ee27d..bdffe572 100644 --- a/test/common/AppUpdateBanner.test.tsx +++ b/test/common/AppUpdateBanner.test.tsx @@ -1,5 +1,6 @@ import { screen } from '@testing-library/react'; import { AppUpdateBanner } from '../../src/common/AppUpdateBanner'; +import { checkAccessibility } from '../__helpers__/accessibility'; import { renderWithEvents } from '../__helpers__/setUpTest'; describe('', () => { @@ -7,6 +8,8 @@ describe('', () => { const forceUpdate = vi.fn(); const setUp = () => renderWithEvents(); + it('passes a11y checks', () => checkAccessibility(setUp())); + it('renders initial state', () => { setUp(); diff --git a/test/common/ErrorHandler.test.tsx b/test/common/ErrorHandler.test.tsx index f95e48c9..2bbd85e2 100644 --- a/test/common/ErrorHandler.test.tsx +++ b/test/common/ErrorHandler.test.tsx @@ -1,7 +1,8 @@ -import { render, screen } from '@testing-library/react'; +import { screen } from '@testing-library/react'; import { fromPartial } from '@total-typescript/shoehorn'; -import type { PropsWithChildren } from 'react'; +import type { PropsWithChildren, ReactNode } from 'react'; import { ErrorHandler as BaseErrorHandler } from '../../src/common/ErrorHandler'; +import { checkAccessibility } from '../__helpers__/accessibility'; import { renderWithEvents } from '../__helpers__/setUpTest'; const ComponentWithError = () => { @@ -13,13 +14,16 @@ describe('', () => { const location = fromPartial({ reload }); const cons = fromPartial({ error: vi.fn() }); const ErrorHandler = (props: PropsWithChildren) => ; + const setUp = (children: ReactNode = 'Error') => renderWithEvents({children}); beforeEach(() => { vi.spyOn(console, 'error').mockImplementation(() => {}); // Silence react errors }); + it('passes a11y checks', () => checkAccessibility(setUp())); + it('renders children when no error has occurred', () => { - render(Foo); + setUp(Foo); expect(screen.getByText('Foo')).toBeInTheDocument(); expect(screen.queryByText('Oops! This is awkward :S')).not.toBeInTheDocument(); @@ -27,14 +31,14 @@ describe('', () => { }); it('renders error page when error has occurred', () => { - render(); + setUp(); expect(screen.getByText('Oops! This is awkward :S')).toBeInTheDocument(); expect(screen.getByRole('button')).toBeInTheDocument(); }); it('reloads page on button click', async () => { - const { user } = renderWithEvents(); + const { user } = setUp(); expect(reload).not.toHaveBeenCalled(); await user.click(screen.getByRole('button')); diff --git a/test/common/Home.test.tsx b/test/common/Home.test.tsx index 92a7e1d2..912594d3 100644 --- a/test/common/Home.test.tsx +++ b/test/common/Home.test.tsx @@ -3,6 +3,7 @@ import { fromPartial } from '@total-typescript/shoehorn'; import { MemoryRouter } from 'react-router'; import { Home } from '../../src/common/Home'; import type { ServersMap, ServerWithId } from '../../src/servers/data'; +import { checkAccessibility } from '../__helpers__/accessibility'; describe('', () => { const setUp = (servers: ServersMap = {}) => render( @@ -11,6 +12,10 @@ describe('', () => { , ); + it('passes a11y checks', () => checkAccessibility( + setUp({ '1a': fromPartial({ name: 'foo', id: '1' }) }), + )); + it('renders title', () => { setUp(); expect(screen.getByRole('heading', { name: 'Welcome!' })).toBeInTheDocument(); diff --git a/test/common/MainHeader.test.tsx b/test/common/MainHeader.test.tsx index f38fc2ee..a8629687 100644 --- a/test/common/MainHeader.test.tsx +++ b/test/common/MainHeader.test.tsx @@ -3,11 +3,13 @@ import { fromPartial } from '@total-typescript/shoehorn'; import { createMemoryHistory } from 'history'; import { Router } from 'react-router-dom'; import { MainHeaderFactory } from '../../src/common/MainHeader'; +import { checkAccessibility } from '../__helpers__/accessibility'; import { renderWithEvents } from '../__helpers__/setUpTest'; describe('', () => { const MainHeader = MainHeaderFactory(fromPartial({ - ServersDropdown: () => <>ServersDropdown, + // Fake this component as a li, as it gets rendered inside a ul + ServersDropdown: () =>
  • ServersDropdown
  • , })); const setUp = (pathname = '') => { const history = createMemoryHistory(); @@ -20,6 +22,8 @@ describe('', () => { ); }; + it('passes a11y checks', () => checkAccessibility(setUp())); + it('renders ServersDropdown', () => { setUp(); expect(screen.getByText('ServersDropdown')).toBeInTheDocument(); diff --git a/test/common/NotFound.test.tsx b/test/common/NotFound.test.tsx index ce5f7dc6..1e13e3c1 100644 --- a/test/common/NotFound.test.tsx +++ b/test/common/NotFound.test.tsx @@ -1,10 +1,13 @@ import { render, screen } from '@testing-library/react'; import { MemoryRouter } from 'react-router'; import { NotFound } from '../../src/common/NotFound'; +import { checkAccessibility } from '../__helpers__/accessibility'; describe('', () => { const setUp = (props = {}) => render(); + it('passes a11y checks', () => checkAccessibility(setUp())); + it('shows expected error title', () => { setUp(); expect(screen.getByText('Oops! We could not find requested route.')).toBeInTheDocument(); diff --git a/test/common/ScrollToTop.test.tsx b/test/common/ScrollToTop.test.tsx index e1d75933..2b22e383 100644 --- a/test/common/ScrollToTop.test.tsx +++ b/test/common/ScrollToTop.test.tsx @@ -1,14 +1,19 @@ import { render, screen } from '@testing-library/react'; import { MemoryRouter } from 'react-router'; import { ScrollToTop } from '../../src/common/ScrollToTop'; +import { checkAccessibility } from '../__helpers__/accessibility'; describe('', () => { + const setUp = (children = 'Foo') => render({children}); + + it('passes a11y checks', () => checkAccessibility(setUp())); + it.each([ ['Foobar'], ['Barfoo'], ['Something'], ])('just renders children', (children) => { - render({children}); + setUp(children); expect(screen.getByText(children)).toBeInTheDocument(); }); }); diff --git a/test/common/ShlinkVersions.test.tsx b/test/common/ShlinkVersions.test.tsx index e079e9c8..32634066 100644 --- a/test/common/ShlinkVersions.test.tsx +++ b/test/common/ShlinkVersions.test.tsx @@ -3,10 +3,15 @@ import { fromPartial } from '@total-typescript/shoehorn'; import type { ShlinkVersionsProps } from '../../src/common/ShlinkVersions'; import { ShlinkVersions } from '../../src/common/ShlinkVersions'; import type { NonReachableServer, NotFoundServer, ReachableServer } from '../../src/servers/data'; +import { checkAccessibility } from '../__helpers__/accessibility'; describe('', () => { const setUp = (props: ShlinkVersionsProps) => render(); + it('passes a11y checks', () => checkAccessibility( + setUp({ selectedServer: fromPartial({ version: '1.0.0', printableVersion: '1.0.0' }) }), + )); + it.each([ ['1.2.3', fromPartial({ version: '1.0.0', printableVersion: 'foo' }), 'v1.2.3', 'foo'], ['foo', fromPartial({ version: '1.0.0', printableVersion: '1.2.3' }), 'latest', '1.2.3'], diff --git a/test/common/ShlinkVersionsContainer.test.tsx b/test/common/ShlinkVersionsContainer.test.tsx index cdf6d34e..da46852b 100644 --- a/test/common/ShlinkVersionsContainer.test.tsx +++ b/test/common/ShlinkVersionsContainer.test.tsx @@ -3,9 +3,10 @@ import { fromPartial } from '@total-typescript/shoehorn'; import { createMemoryHistory } from 'history'; import { Router } from 'react-router-dom'; import { ShlinkVersionsContainer } from '../../src/common/ShlinkVersionsContainer'; +import { checkAccessibility } from '../__helpers__/accessibility'; describe('', () => { - const setUp = (activeRoute: string) => { + const setUp = (activeRoute: string = '') => { const history = createMemoryHistory(); history.push(activeRoute); @@ -16,6 +17,8 @@ describe('', () => { ); }; + it('passes a11y checks', () => checkAccessibility(setUp())); + it.each([ ['/something', 'text-center'], ['/server/foo/edit', 'text-center'], diff --git a/test/common/ShlinkWebComponentContainer.test.tsx b/test/common/ShlinkWebComponentContainer.test.tsx index 71438dbb..d39b00ea 100644 --- a/test/common/ShlinkWebComponentContainer.test.tsx +++ b/test/common/ShlinkWebComponentContainer.test.tsx @@ -3,6 +3,7 @@ import { fromPartial } from '@total-typescript/shoehorn'; import { useParams } from 'react-router-dom'; import { ShlinkWebComponentContainerFactory } from '../../src/common/ShlinkWebComponentContainer'; import type { NonReachableServer, NotFoundServer, SelectedServer } from '../../src/servers/data'; +import { checkAccessibility } from '../__helpers__/accessibility'; vi.mock('react-router-dom', async () => ({ ...(await vi.importActual('react-router-dom')), @@ -24,6 +25,8 @@ describe('', () => { (useParams as any).mockReturnValue({ serverId: 'abc123' }); }); + it('passes a11y checks', () => checkAccessibility(setUp(fromPartial({ version: '3.0.0' })))); + it('shows loading indicator while loading server', () => { setUp(null); diff --git a/test/common/img/ShlinkLogo.test.tsx b/test/common/img/ShlinkLogo.test.tsx index d6ce4597..99b9aee9 100644 --- a/test/common/img/ShlinkLogo.test.tsx +++ b/test/common/img/ShlinkLogo.test.tsx @@ -2,9 +2,12 @@ import { MAIN_COLOR } from '@shlinkio/shlink-frontend-kit'; import { render } from '@testing-library/react'; import type { ShlinkLogoProps } from '../../../src/common/img/ShlinkLogo'; import { ShlinkLogo } from '../../../src/common/img/ShlinkLogo'; +import { checkAccessibility } from '../../__helpers__/accessibility'; describe('', () => { - const setUp = (props: ShlinkLogoProps) => render(); + const setUp = (props: ShlinkLogoProps = {}) => render(); + + it('passes a11y checks', () => checkAccessibility(setUp())); it.each([ [undefined, MAIN_COLOR],