-
Notifications
You must be signed in to change notification settings - Fork 0
/
headingoffset-polyfill.test.html
289 lines (282 loc) · 11.8 KB
/
headingoffset-polyfill.test.html
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<html>
<head>
<script type="module" src="../headingoffset-polyfill.js"></script>
</head>
<body>
<div id="container"></div>
<script type="module">
import { expect } from "@esm-bundle/chai";
import { runTests } from "@web/test-runner-mocha";
async function setup({ headingOffset, innerHTML } = { innerHTML: "" }) {
// Delete existing container element
container.parentNode.removeChild(container);
// Create new container element
const newContainer = document.createElement("div");
newContainer.id = "container";
if (headingOffset) {
newContainer.headingOffset = headingOffset;
}
if (typeof innerHTML === "string") {
newContainer.innerHTML = innerHTML;
}
document.body.appendChild(newContainer);
// Wait for mutation observer callback to finish
await new Promise((resolve) => setTimeout(resolve, 0));
}
runTests(async () => {
// Reset headingOffset and innerHTML before each test
beforeEach(async () => {
await setup();
});
describe("reflection", () => {
it("'headingOffset' property exists", () => {
expect("headingOffset" in container).to.equal(true);
});
it("default value when unset", () => {
expect(container.headingOffset).to.equal(0);
});
it("setting via JS affects HTML", () => {
container.headingOffset = 1;
expect(container.getAttribute("headingoffset")).to.equal("1");
container.headingOffset = undefined;
expect(container.hasAttribute("headingoffset")).to.equal(false);
container.headingOffset = null;
expect(container.hasAttribute("headingoffset")).to.equal(false);
container.headingOffset = "";
expect(container.hasAttribute("headingoffset")).to.equal(false);
});
it("setting via HTML affects JS", () => {
container.setAttribute("headingoffset", "2");
expect(container.headingOffset).to.equal(2);
});
});
describe("expected values", async () => {
it("unnested (all heading levels)", async () => {
await setup({
innerHTML: `
<h1 id="h1">Level 1</h1>
<h2 id="h2">Level 2</h2>
<h3 id="h3">Level 3</h3>
<h4 id="h4">Level 4</h4>
<h5 id="h5">Level 5</h5>
<h6 id="h6">Level 6</h6>`,
});
expect(h1.hasAttribute("aria-level")).to.equal(false);
expect(h2.hasAttribute("aria-level")).to.equal(false);
expect(h3.hasAttribute("aria-level")).to.equal(false);
expect(h4.hasAttribute("aria-level")).to.equal(false);
expect(h5.hasAttribute("aria-level")).to.equal(false);
expect(h6.hasAttribute("aria-level")).to.equal(false);
});
it("nested, 1 offset (3 of the same heading level)", async () => {
await setup({
headingOffset: 1,
innerHTML: `
<h2 id="h2A">Level 3</h2>
<h2 id="h2B">Level 3</h2>
<h2 id="h2C">Level 3</h2>`,
});
expect(h2A.getAttribute("aria-level")).to.equal("3");
expect(h2B.getAttribute("aria-level")).to.equal("3");
expect(h2C.getAttribute("aria-level")).to.equal("3");
});
it("nested, 1 offset (all heading levels)", async () => {
await setup({
headingOffset: 1,
innerHTML: `
<h1 id="h1">Level 2</h1>
<h2 id="h2">Level 3</h2>
<h3 id="h3">Level 4</h3>
<h4 id="h4">Level 5</h4>
<h5 id="h5">Level 6</h5>
<h6 id="h6">Level 7</h6>`,
});
expect(h1.getAttribute("aria-level")).to.equal("2");
expect(h2.getAttribute("aria-level")).to.equal("3");
expect(h3.getAttribute("aria-level")).to.equal("4");
expect(h4.getAttribute("aria-level")).to.equal("5");
expect(h5.getAttribute("aria-level")).to.equal("6");
expect(h6.getAttribute("aria-level")).to.equal("7");
});
it("nested, 6 offset (doesn’t overflow past supported levels)", async () => {
await setup({
headingOffset: 6,
innerHTML: `
<h1 id="h1">Level 7</h1>
<h2 id="h2">Level 8</h2>
<h3 id="h3">Level 9</h3>
<h4 id="h4">Level 4</h4>
<h5 id="h5">Level 5</h5>
<h6 id="h6">Level 6</h6>`,
});
expect(h1.getAttribute("aria-level")).to.equal("7");
expect(h2.getAttribute("aria-level")).to.equal("8");
expect(h3.getAttribute("aria-level")).to.equal("9");
expect(h4.hasAttribute("aria-level")).to.equal(false);
expect(h5.hasAttribute("aria-level")).to.equal(false);
expect(h6.hasAttribute("aria-level")).to.equal(false);
});
it("nested, 2 offset (ignores 'aria-level' and 'role=heading')", async () => {
await setup({
headingOffset: 2,
innerHTML: `
<h1 id="h1" aria-level="1">Level 1</h1>
<div id="div" role="heading">Level 2</div>`,
});
expect(h1.getAttribute("aria-level")).to.equal("1");
expect(div.hasAttribute("aria-level")).to.equal(false);
});
it("nested, 1 offset (adds another heading later)", async () => {
await setup({
headingOffset: 1,
innerHTML: `
<h1 id="h1">Level 2</h1>`,
});
expect(h1.getAttribute("aria-level")).to.equal("2");
const h2 = document.createElement("h2");
h2.id = "h2";
container.appendChild(h2);
await new Promise((resolve) => setTimeout(resolve, 0));
expect(h2.getAttribute("aria-level")).to.equal("3");
});
it("deeply nested, 1 offset (grandchild has the attribute)", async () => {
await setup({
innerHTML: `
<div>
<div headingoffset="1">
<h1 id="h1">Level 2</h1>
</div>
</div>`,
});
expect(h1.getAttribute("aria-level")).to.equal("2");
});
it("nested, 1 offset (offset updated later)", async () => {
await setup({
headingOffset: 1,
innerHTML: `
<h1 id="h1A" aria-level="1">Level 1</h1>
<h1 id="h1B">Level 2</h1>`,
});
expect(h1A.getAttribute("aria-level")).to.equal("1");
expect(h1B.getAttribute("aria-level")).to.equal("2");
container.headingOffset = 2;
await new Promise((resolve) => setTimeout(resolve, 0));
expect(h1A.getAttribute("aria-level")).to.equal("1");
expect(h1B.getAttribute("aria-level")).to.equal("3");
});
it("nested, 1 offset (offset removed later)", async () => {
await setup({
headingOffset: 1,
innerHTML: `
<h1 id="h1A" aria-level="1">Level 1</h1>
<h1 id="h1B">Level 2</h1>`,
});
expect(h1A.getAttribute("aria-level")).to.equal("1");
expect(h1B.getAttribute("aria-level")).to.equal("2");
container.headingOffset = 0;
await new Promise((resolve) => setTimeout(resolve, 0));
expect(h1A.getAttribute("aria-level")).to.equal("1");
expect(h1B.hasAttribute("aria-level")).to.equal(false);
});
it("nested, 1 offset (offset removed later, and then re-added even later)", async () => {
await setup({
headingOffset: 1,
innerHTML: `
<h1 id="h1A" aria-level="1">Level 1</h1>
<h1 id="h1B">Level 2</h1>`,
});
expect(h1A.getAttribute("aria-level")).to.equal("1");
expect(h1B.getAttribute("aria-level")).to.equal("2");
container.headingOffset = 0;
await new Promise((resolve) => setTimeout(resolve, 0));
expect(h1A.getAttribute("aria-level")).to.equal("1");
expect(h1B.hasAttribute("aria-level")).to.equal(false);
container.headingOffset = 3;
await new Promise((resolve) => setTimeout(resolve, 0));
expect(h1A.getAttribute("aria-level")).to.equal("1");
expect(h1B.getAttribute("aria-level")).to.equal("4");
});
it("nested, 1 offset (aria-level updated later (not by polyfill))", async () => {
await setup({
headingOffset: 1,
innerHTML: `
<h1 id="h1A" aria-level="1">Level 1</h1>
<h1 id="h1B">Level 2</h1>`,
});
expect(h1A.getAttribute("aria-level")).to.equal("1");
expect(h1B.getAttribute("aria-level")).to.equal("2");
h1A.setAttribute("aria-level", "6");
h1B.setAttribute("aria-level", "6");
container.headingOffset = 2;
await new Promise((resolve) => setTimeout(resolve, 0));
expect(h1A.getAttribute("aria-level")).to.equal("6");
expect(h1B.getAttribute("aria-level")).to.equal("6");
});
it("nested, 1 offset (aria-level removed later (not by polyfill))", async () => {
await setup({
headingOffset: 1,
innerHTML: `
<h1 id="h1A" aria-level="1">Level 1</h1>`,
});
expect(h1A.getAttribute("aria-level")).to.equal("1");
h1A.removeAttribute("aria-level");
await new Promise((resolve) => setTimeout(resolve, 0));
expect(h1A.getAttribute("aria-level")).to.equal("2");
});
});
describe("weird values", async () => {
it("nested, 0 offset", async () => {
await setup({
headingOffset: 0,
innerHTML: `
<h1 id="h1">Level 1</h1>
<h2 id="h2">Level 2</h2>`,
});
expect(h1.hasAttribute("aria-level")).to.equal(false);
expect(h2.hasAttribute("aria-level")).to.equal(false);
});
it("nested, -1 offset", async () => {
await setup({
headingOffset: -1,
innerHTML: `
<h1 id="h1">Level 1</h1>
<h2 id="h2">Level 2</h2>`,
});
expect(h1.hasAttribute("aria-level")).to.equal(false);
expect(h2.hasAttribute("aria-level")).to.equal(false);
});
it("nested, 10 offset", async () => {
await setup({
headingOffset: 10,
innerHTML: `
<h1 id="h1">Level 1</h1>
<h2 id="h2">Level 2</h2>`,
});
expect(h1.hasAttribute("aria-level")).to.equal(false);
expect(h2.hasAttribute("aria-level")).to.equal(false);
});
it("nested, NaN offset", async () => {
await setup({
headingOffset: NaN,
innerHTML: `
<h1 id="h1">Level 1</h1>
<h2 id="h2">Level 2</h2>`,
});
expect(h1.hasAttribute("aria-level")).to.equal(false);
expect(h2.hasAttribute("aria-level")).to.equal(false);
});
it("nested, Infinity offset", async () => {
await setup({
headingOffset: Infinity,
innerHTML: `
<h1 id="h1">Level 1</h1>
<h2 id="h2">Level 2</h2>`,
});
expect(h1.hasAttribute("aria-level")).to.equal(false);
expect(h2.hasAttribute("aria-level")).to.equal(false);
});
});
});
</script>
</body>
</html>