generated from timelessco/react-components-template
-
Notifications
You must be signed in to change notification settings - Fork 8
/
jest.setup.js
34 lines (32 loc) · 1.04 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
const {
matcherHint,
printReceived,
printExpected,
} = require("jest-matcher-utils");
const { toHaveNoViolations: axeMatchers } = require("jest-axe");
const matchers = require("@testing-library/jest-dom/matchers");
// Consider [aria-activedescendant="${id}"] #${id} as the focused 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();
},
};
}
expect.extend({ ...matchers, ...axeMatchers, toHaveFocus });