UNPKG

1.84 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.toBeChecked = toBeChecked;
7
8var _ariaQuery = require("aria-query");
9
10var _jestMatcherUtils = require("jest-matcher-utils");
11
12var _utils = require("./utils");
13
14function toBeChecked(element) {
15 (0, _utils.checkHtmlElement)(element, toBeChecked, this);
16
17 const isValidInput = () => {
18 return element.tagName.toLowerCase() === 'input' && ['checkbox', 'radio'].includes(element.type);
19 };
20
21 if (!isValidInput() && !(() => {
22 return roleSupportsChecked(element.getAttribute('role')) && ['true', 'false'].includes(element.getAttribute('aria-checked'));
23 })()) {
24 return {
25 pass: false,
26 message: () => `only inputs with type="checkbox" or type="radio" or elements with ${supportedRolesSentence()} and a valid aria-checked attribute can be used with .toBeChecked(). Use .toHaveValue() instead`
27 };
28 }
29
30 const isChecked = () => {
31 if (isValidInput()) return element.checked;
32 return element.getAttribute('aria-checked') === 'true';
33 };
34
35 return {
36 pass: isChecked(),
37 message: () => {
38 const is = isChecked() ? 'is' : 'is not';
39 return [(0, _jestMatcherUtils.matcherHint)(`${this.isNot ? '.not' : ''}.toBeChecked`, 'element', ''), '', `Received element ${is} checked:`, ` ${(0, _jestMatcherUtils.printReceived)(element.cloneNode(false))}`].join('\n');
40 }
41 };
42}
43
44function supportedRolesSentence() {
45 return (0, _utils.toSentence)(supportedRoles().map(role => `role="${role}"`), {
46 lastWordConnector: ' or '
47 });
48}
49
50function supportedRoles() {
51 return Array.from(_ariaQuery.roles.keys()).filter(roleSupportsChecked);
52}
53
54function roleSupportsChecked(role) {
55 var _roles$get;
56
57 return ((_roles$get = _ariaQuery.roles.get(role)) == null ? void 0 : _roles$get.props['aria-checked']) !== undefined;
58}
\No newline at end of file