to-be-partially-checked.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.toBePartiallyChecked = toBePartiallyChecked;
  6. var _utils = require("./utils");
  7. function toBePartiallyChecked(element) {
  8. (0, _utils.checkHtmlElement)(element, toBePartiallyChecked, this);
  9. const isValidInput = () => {
  10. return element.tagName.toLowerCase() === 'input' && element.type === 'checkbox';
  11. };
  12. if (!isValidInput() && !(() => {
  13. return element.getAttribute('role') === 'checkbox';
  14. })()) {
  15. return {
  16. pass: false,
  17. message: () => 'only inputs with type="checkbox" or elements with role="checkbox" and a valid aria-checked attribute can be used with .toBePartiallyChecked(). Use .toHaveValue() instead'
  18. };
  19. }
  20. const isPartiallyChecked = () => {
  21. const isAriaMixed = element.getAttribute('aria-checked') === 'mixed';
  22. if (isValidInput()) {
  23. return element.indeterminate || isAriaMixed;
  24. }
  25. return isAriaMixed;
  26. };
  27. return {
  28. pass: isPartiallyChecked(),
  29. message: () => {
  30. const is = isPartiallyChecked() ? 'is' : 'is not';
  31. return [this.utils.matcherHint(`${this.isNot ? '.not' : ''}.toBePartiallyChecked`, 'element', ''), '', `Received element ${is} partially checked:`, ` ${this.utils.printReceived(element.cloneNode(false))}`].join('\n');
  32. }
  33. };
  34. }