to-be-partially-checked.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  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. const isValidAriaElement = () => {
  13. return element.getAttribute('role') === 'checkbox';
  14. };
  15. if (!isValidInput() && !isValidAriaElement()) {
  16. return {
  17. pass: false,
  18. 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'
  19. };
  20. }
  21. const isPartiallyChecked = () => {
  22. const isAriaMixed = element.getAttribute('aria-checked') === 'mixed';
  23. if (isValidInput()) {
  24. return element.indeterminate || isAriaMixed;
  25. }
  26. return isAriaMixed;
  27. };
  28. return {
  29. pass: isPartiallyChecked(),
  30. message: () => {
  31. const is = isPartiallyChecked() ? 'is' : 'is not';
  32. return [this.utils.matcherHint(`${this.isNot ? '.not' : ''}.toBePartiallyChecked`, 'element', ''), '', `Received element ${is} partially checked:`, ` ${this.utils.printReceived(element.cloneNode(false))}`].join('\n');
  33. }
  34. };
  35. }