Skip to content

Commit 95b68fa

Browse files
authored
test(context): cover NodeList for include/exclude and fix doc examples (#5178)
The `context.md` examples labeled "Test a Node list" and "Test everything except these DOM nodes" used `document.querySelector(...)`, which only ever returns a single node, so they didn't actually show a NodeList. Switched both to `document.querySelectorAll(...)` and tweaked the comments so the examples demonstrate the NodeList support that already works. Also added two `Context` unit tests: one for a NodeList passed as the bare context, and one for NodeLists passed via `include`/`exclude`. (`doc/API.md` already lists NodeList as an accepted context, so it didn't need a change.) Heads up: I couldn't get the browser test runner going in my environment, so the new assertions were checked against the built `axe.js` bundle under jsdom rather than web-test-runner — prettier and eslint are clean. Worth a CI run to confirm in-browser. Closes: #3348
1 parent 26acf27 commit 95b68fa

2 files changed

Lines changed: 25 additions & 4 deletions

File tree

doc/context.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ const img = document.createElement('img');
4343
document.body.appendChild(img);
4444
await axe.run(img);
4545

46-
// Test a Node list:
47-
const nodes = document.querySelector('main, header');
46+
// Test a NodeList, such as one returned by document.querySelectorAll:
47+
const nodes = document.querySelectorAll('main, header');
4848
await axe.run(nodes);
4949

5050
// Test an array of nodes:
@@ -73,8 +73,8 @@ There are often areas of a page that as a developer you have no control over. Yo
7373
// Test everything except the ad banners:
7474
await axe.run({ exclude: '.ad-banner' });
7575

76-
// Test everything except these DOM nodes:
77-
const youtubeVids = document.querySelector('iframe[src^="youtube.com"]');
76+
// Test everything except these DOM nodes (a NodeList works too):
77+
const youtubeVids = document.querySelectorAll('iframe[src^="youtube.com"]');
7878
await axe.run({ exclude: youtubeVids });
7979
```
8080

test/core/base/context.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,16 @@ describe('Context', () => {
196196
assert.deepEqual(selectors(result.include), ['#foo', '#bar', '#baz']);
197197
});
198198

199+
it('accepts a NodeList', () => {
200+
fixture.innerHTML =
201+
'<div id="foo"></div><div id="bar"></div><div id="baz"></div>';
202+
const nodeList = fixture.querySelectorAll('div');
203+
204+
const result = new Context(nodeList);
205+
assert.deepEqual(selectors(result.include), ['#foo', '#bar', '#baz']);
206+
assert.isEmpty(result.exclude);
207+
});
208+
199209
describe('throwing errors', () => {
200210
let isInFrame;
201211

@@ -261,6 +271,17 @@ describe('Context', () => {
261271
assert.isAtLeast(context.flatTree.length, 1);
262272
});
263273

274+
it('accepts a NodeList for the include and exclude properties', () => {
275+
fixture.innerHTML =
276+
'<div id="foo"></div><div id="bar"></div><div id="baz"></div>';
277+
const result = new Context({
278+
include: fixture.querySelectorAll('#foo, #bar'),
279+
exclude: fixture.querySelectorAll('#baz')
280+
});
281+
assert.deepEqual(selectors(result.include), ['#foo', '#bar']);
282+
assert.deepEqual(selectors(result.exclude), ['#baz']);
283+
});
284+
264285
it('should disregard bad input, non-matching selectors', () => {
265286
const flatTree = axe.utils.getFlattenedTree(document);
266287
const context = new Context({

0 commit comments

Comments
 (0)