to-be-checked.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.toBeChecked = toBeChecked;
  6. var _ariaQuery = require("aria-query");
  7. var _utils = require("./utils");
  8. function toBeChecked(element) {
  9. (0, _utils.checkHtmlElement)(element, toBeChecked, this);
  10. const isValidInput = () => {
  11. return element.tagName.toLowerCase() === 'input' && ['checkbox', 'radio'].includes(element.type);
  12. };
  13. if (!isValidInput() && !(() => {
  14. return roleSupportsChecked(element.getAttribute('role')) && ['true', 'false'].includes(element.getAttribute('aria-checked'));
  15. })()) {
  16. return {
  17. pass: false,
  18. 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`
  19. };
  20. }
  21. const isChecked = () => {
  22. if (isValidInput()) return element.checked;
  23. return element.getAttribute('aria-checked') === 'true';
  24. };
  25. return {
  26. pass: isChecked(),
  27. message: () => {
  28. const is = isChecked() ? 'is' : 'is not';
  29. return [this.utils.matcherHint(`${this.isNot ? '.not' : ''}.toBeChecked`, 'element', ''), '', `Received element ${is} checked:`, ` ${this.utils.printReceived(element.cloneNode(false))}`].join('\n');
  30. }
  31. };
  32. }
  33. function supportedRolesSentence() {
  34. return (0, _utils.toSentence)(supportedRoles().map(role => `role="${role}"`), {
  35. lastWordConnector: ' or '
  36. });
  37. }
  38. function supportedRoles() {
  39. return Array.from(_ariaQuery.roles.keys()).filter(roleSupportsChecked);
  40. }
  41. function roleSupportsChecked(role) {
  42. var _roles$get;
  43. return ((_roles$get = _ariaQuery.roles.get(role)) == null ? void 0 : _roles$get.props['aria-checked']) !== undefined;
  44. }