-
Notifications
You must be signed in to change notification settings - Fork 22
/
format.test.ts
79 lines (70 loc) · 3.31 KB
/
format.test.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
/*
* Copyright (c) 2020, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import * as axe from 'axe-core';
import { beforeEachSetup, domWithA11yIssues, domWithNoA11yIssues } from '@sa11y/test-utils';
import { AxeResults } from '@sa11y/common';
import { A11yError } from '../src';
import { expect } from '@jest/globals';
// TODO (refactor): Move to common test-utils
// - without creating circular dep due to "A11yError"
// eslint-disable-next-line jest/no-export
export async function getA11yError(dom: string = domWithA11yIssues): Promise<A11yError> {
document.body.innerHTML = dom;
const violations = await axe.run(document).then((results) => results.violations);
try {
A11yError.checkAndThrow(violations);
} catch (e) {
return e as A11yError;
}
// If there are no violations and no A11yError thrown
return new A11yError([], []);
}
// TODO (refactor): Move to common test-utils
// - without creating circular dep due to "A11yError"
// eslint-disable-next-line jest/no-export
export async function getViolations(dom = domWithA11yIssues): Promise<AxeResults> {
const a11yError = await getA11yError(dom);
return a11yError.violations;
}
beforeEach(() => {
beforeEachSetup();
});
describe('a11y Results Formatter', () => {
it.each([domWithA11yIssues, domWithNoA11yIssues])(
'should format a11y issues as expected with default options for dom %#',
async (dom) => {
const a11yError = await getA11yError(dom);
expect(a11yError.format()).toMatchSnapshot();
expect(a11yError.length).toMatchSnapshot();
expect(a11yError.message).toMatchSnapshot();
}
);
it.each([{ formatter: JSON.stringify }, { highlighter: (text: string) => `"${text}"` }, {}, undefined, null])(
'should format using specified options: %#',
async (formatOptions) => {
expect((await getA11yError(domWithA11yIssues)).format(formatOptions)).toMatchSnapshot();
}
);
it('should not throw error when no violations are present', async () => {
const a11yError = await getA11yError(domWithNoA11yIssues);
expect(() => A11yError.checkAndThrow(a11yError.violations)).not.toThrow();
});
it('should throw error when violations are present', async () => {
const a11yError = await getA11yError(domWithA11yIssues);
expect(() => A11yError.checkAndThrow(a11yError.violations)).toThrowErrorMatchingSnapshot();
});
it('should not throw error for repeated violations with consolidation', async () => {
const a11yError = await getA11yError(domWithA11yIssues);
const violations = a11yError.violations;
// Should throw error for the first time
expect(() => A11yError.checkAndThrow(violations, { deduplicate: true })).toThrowErrorMatchingSnapshot();
// Should not throw error for repeated violations with consolidation
expect(() => A11yError.checkAndThrow(violations, { deduplicate: true })).not.toThrow();
// Should throw error for repeated violations without consolidation
expect(() => A11yError.checkAndThrow(violations, { deduplicate: false })).toThrowErrorMatchingSnapshot();
});
});