-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathsetup-global-a11y-hooks-test.ts
181 lines (145 loc) · 4.46 KB
/
setup-global-a11y-hooks-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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import QUnit, { module, test } from 'qunit';
import { resolve } from 'rsvp';
import {
setupGlobalA11yHooks,
teardownGlobalA11yHooks,
setEnableA11yAudit,
} from 'ember-a11y-testing/test-support';
import { setupApplicationTest } from 'ember-qunit';
import { visit } from '@ember/test-helpers';
import { InvocationStrategy } from 'ember-a11y-testing/test-support/types';
function getRange(count: number) {
return Array(count)
.fill(0)
.map((_, i) => i + 1);
}
function getTestName(n: number, count: number, invocationStrategy: string) {
return n === count
? `it invokes correctly using ${invocationStrategy} invocation strategy`
: `IGNORE: test used to validate setupGlobalA11yHooks (${n})`;
}
module('setupGlobalA11yHooks with invokeAll', function (hooks) {
setupApplicationTest(hooks);
const TEST_INVOCATIONS_COUNT = 6;
const EXPECTED_AUDIT_INVOCATIONS_COUNT = 6;
let actualAuditInvocationsCount = 0;
let numInvoked = 0;
function invokeAll(): boolean {
return true;
}
function a11yAuditFake() {
actualAuditInvocationsCount += 1;
return resolve(undefined);
}
hooks.before(function () {
setupGlobalA11yHooks(invokeAll, a11yAuditFake);
setEnableA11yAudit(true);
});
hooks.after(function () {
teardownGlobalA11yHooks();
setEnableA11yAudit();
});
getRange(TEST_INVOCATIONS_COUNT).forEach((num) => {
test(getTestName(num, TEST_INVOCATIONS_COUNT, 'invokeAll'), async function (
assert
) {
assert.expect(0);
await visit('/');
numInvoked++;
if (numInvoked === TEST_INVOCATIONS_COUNT) {
assert.expect(1);
assert.equal(
actualAuditInvocationsCount,
EXPECTED_AUDIT_INVOCATIONS_COUNT
);
}
});
});
});
module('setupGlobalA11yHooks with invokeEveryN', function (hooks) {
setupApplicationTest(hooks);
const TEST_INVOCATIONS_COUNT = 6;
const EXPECTED_AUDIT_INVOCATIONS_COUNT = 2;
let actualAuditInvocationsCount = 0;
let numInvoked = 0;
function invokeEveryN(interval: number): InvocationStrategy {
let invocationCounter: number = 0;
return () => {
invocationCounter++;
return invocationCounter % interval === 0;
};
}
function a11yAuditFake() {
actualAuditInvocationsCount += 1;
return resolve(undefined);
}
hooks.before(function () {
setupGlobalA11yHooks(invokeEveryN(3), a11yAuditFake);
setEnableA11yAudit(true);
});
hooks.after(function () {
teardownGlobalA11yHooks();
setEnableA11yAudit();
});
getRange(TEST_INVOCATIONS_COUNT).forEach((num) => {
test(
getTestName(num, TEST_INVOCATIONS_COUNT, 'invokeEveryN'),
async function (assert) {
assert.expect(0);
await visit('/');
numInvoked++;
if (numInvoked === TEST_INVOCATIONS_COUNT) {
assert.expect(1);
assert.equal(
actualAuditInvocationsCount,
EXPECTED_AUDIT_INVOCATIONS_COUNT
);
}
}
);
});
});
module('setupGlobalA11yHooks with invokeWithExclusions', function (hooks) {
setupApplicationTest(hooks);
const TEST_INVOCATIONS_COUNT = 6;
let actualTestsRun: string[] = [];
let numInvoked = 0;
function invokeWithExclusions(): boolean {
const EXCLUDED_TESTS = [
'IGNORE: test used to validate setupGlobalA11yHooks (2)',
'it invokes correctly using invokeWithExclusions invocation strategy',
];
return !EXCLUDED_TESTS.includes(QUnit.config.current.testName);
}
function a11yAuditFake() {
actualTestsRun.push(QUnit.config.current.testName);
return resolve(undefined);
}
hooks.before(function () {
setupGlobalA11yHooks(invokeWithExclusions, a11yAuditFake);
setEnableA11yAudit(true);
});
hooks.after(function () {
teardownGlobalA11yHooks();
setEnableA11yAudit();
});
getRange(TEST_INVOCATIONS_COUNT).forEach((num) => {
test(
getTestName(num, TEST_INVOCATIONS_COUNT, 'invokeWithExclusions'),
async function (assert) {
assert.expect(0);
await visit('/');
numInvoked++;
if (numInvoked === TEST_INVOCATIONS_COUNT) {
assert.expect(1);
assert.deepEqual(actualTestsRun, [
'IGNORE: test used to validate setupGlobalA11yHooks (1)',
'IGNORE: test used to validate setupGlobalA11yHooks (3)',
'IGNORE: test used to validate setupGlobalA11yHooks (4)',
'IGNORE: test used to validate setupGlobalA11yHooks (5)',
]);
}
}
);
});
});