-
Notifications
You must be signed in to change notification settings - Fork 1
/
a11y.ts
91 lines (75 loc) · 2.34 KB
/
a11y.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import * as supertest from 'supertest';
import { app } from '../../main/app';
const pa11y = require('pa11y');
const agent = supertest.agent(app);
class Pa11yResult {
documentTitle: string;
pageUrl: string;
issues: PallyIssue[];
constructor(documentTitle: string, pageUrl: string, issues: PallyIssue[]) {
this.documentTitle = documentTitle;
this.pageUrl = pageUrl;
this.issues = issues;
}
}
class PallyIssue {
code: string;
context: string;
message: string;
selector: string;
type: string;
typeCode: number;
constructor(code: string, context: string, message: string, selector: string, type: string, typeCode: number) {
this.code = code;
this.context = context;
this.message = message;
this.selector = selector;
this.type = type;
this.typeCode = typeCode;
}
}
async function ensurePageCallWillSucceed(url: string): Promise<void> {
return agent.get(url).then((res: supertest.Response) => {
if (res.redirect) {
throw new Error(`Call to ${url} resulted in a redirect to ${res.get('Location')}`);
}
if (res.serverError) {
throw new Error(`Call to ${url} resulted in internal server error`);
}
});
}
function runPally(url: string): Promise<Pa11yResult> {
return pa11y(url, {
hideElements: '.govuk-footer__licence-logo, .govuk-header__logotype-crown',
});
}
function expectNoErrors(messages: PallyIssue[]): void {
const errors = messages.filter(m => m.type === 'error');
if (errors.length > 0) {
const errorsAsJson = `${JSON.stringify(errors, null, 2)}`;
throw new Error(`There are accessibility issues: \n${errorsAsJson}\n`);
}
}
function testAccessibility(url: string): void {
describe(`Page ${url}`, () => {
test('should have no accessibility errors', async () => {
await ensurePageCallWillSucceed(url);
const result = await runPally(agent.get(url).url);
expect(result.issues).toEqual(expect.any(Array));
expectNoErrors(result.issues);
});
});
}
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
global.fetch = jest.fn(() =>
Promise.resolve({
json: () => Promise.resolve({ recipes: [] }),
})
);
jest.setTimeout(20000);
describe('Accessibility', () => {
// testing accessibility of the home page
testAccessibility('/');
// TODO: include each path of your application in accessibility checks
});