forked from ariakit/ariakit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jest.setup.js
33 lines (31 loc) · 974 Bytes
/
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
const {
matcherHint,
printReceived,
printExpected,
} = require("jest-matcher-utils");
const matchers = require("@testing-library/jest-dom/matchers");
// Consider [aria-activedescendant="${id}"] #${id} as the focused element.
function toHaveFocus(element) {
const result = matchers.toHaveFocus(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();
},
};
}
expect.extend({ ...matchers, toHaveFocus });