forked from ariakit/ariakit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jest.setup.js
41 lines (38 loc) · 1.21 KB
/
jest.setup.js
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
const jestDOMMatchers = require("@testing-library/jest-dom/matchers");
const { toHaveNoViolations: axeMatchers } = require("jest-axe");
const failOnConsole = require("jest-fail-on-console");
const {
matcherHint,
printReceived,
printExpected,
} = require("jest-matcher-utils");
failOnConsole();
/**
* Consider [aria-activedescendant="${id}"] #${id} as the focused element.
* @param {HTMLElement} element
*/
function toHaveFocus(element) {
const result = matchers.toHaveFocus.call(this, element);
const { activeElement } = element.ownerDocument;
const activeId =
activeElement && activeElement.getAttribute("aria-activedescendant");
return {
...result,
pass: result.pass || activeId === element.id,
message: () => {
if (activeId) {
return [
matcherHint(`${this.isNot ? ".not" : ""}.toHaveFocus`, "element", ""),
"",
"Expected:",
` ${printExpected(element)}`,
"Received:",
` ${printReceived(element.ownerDocument.getElementById(activeId))}`,
].join("\n");
}
return result.message();
},
};
}
const { __esModule, ...matchers } = jestDOMMatchers;
expect.extend({ ...matchers, ...axeMatchers, toHaveFocus });