-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha11y.ts
75 lines (63 loc) · 1.83 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
import { fail } from 'assert';
const pa11y = require('pa11y');
import * as supertest from 'supertest';
import { app } from '../../main/app';
const agent = supertest.agent(app);
class Pa11yResult {
documentTitle: string;
pageUrl: string;
issues: PallyIssue[];
}
class PallyIssue {
code: string;
context: string;
message: string;
selector: string;
type: string;
typeCode: number;
}
beforeAll((done /* call it or remove it*/) => {
done(); // calling it
});
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): 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)}`;
fail(`There are accessibility issues: \n${errorsAsJson}\n`);
}
}
function testAccessibility(url: string): void {
describe(`Page ${url}`, () => {
test('should have no accessibility errors', done => {
ensurePageCallWillSucceed(url)
.then(() => runPally(agent.get(url).url))
.then((result: Pa11yResult) => {
expectNoErrors(result.issues);
done();
})
.catch((err: Error) => done(err));
});
});
}
describe('Accessibility', () => {
// testing accessibility of the home page
testAccessibility('/');
// TODO: include each path of your application in accessibility checks
});