to-be-invalid.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.toBeInvalid = toBeInvalid;
  6. exports.toBeValid = toBeValid;
  7. var _utils = require("./utils");
  8. const FORM_TAGS = ['form', 'input', 'select', 'textarea'];
  9. function isElementHavingAriaInvalid(element) {
  10. return element.hasAttribute('aria-invalid') && element.getAttribute('aria-invalid') !== 'false';
  11. }
  12. function isSupportsValidityMethod(element) {
  13. return FORM_TAGS.includes((0, _utils.getTag)(element));
  14. }
  15. function isElementInvalid(element) {
  16. const isHaveAriaInvalid = isElementHavingAriaInvalid(element);
  17. if (isSupportsValidityMethod(element)) {
  18. return isHaveAriaInvalid || !element.checkValidity();
  19. } else {
  20. return isHaveAriaInvalid;
  21. }
  22. }
  23. function toBeInvalid(element) {
  24. (0, _utils.checkHtmlElement)(element, toBeInvalid, this);
  25. const isInvalid = isElementInvalid(element);
  26. return {
  27. pass: isInvalid,
  28. message: () => {
  29. const is = isInvalid ? 'is' : 'is not';
  30. return [this.utils.matcherHint(`${this.isNot ? '.not' : ''}.toBeInvalid`, 'element', ''), '', `Received element ${is} currently invalid:`, ` ${this.utils.printReceived(element.cloneNode(false))}`].join('\n');
  31. }
  32. };
  33. }
  34. function toBeValid(element) {
  35. (0, _utils.checkHtmlElement)(element, toBeValid, this);
  36. const isValid = !isElementInvalid(element);
  37. return {
  38. pass: isValid,
  39. message: () => {
  40. const is = isValid ? 'is' : 'is not';
  41. return [this.utils.matcherHint(`${this.isNot ? '.not' : ''}.toBeValid`, 'element', ''), '', `Received element ${is} currently valid:`, ` ${this.utils.printReceived(element.cloneNode(false))}`].join('\n');
  42. }
  43. };
  44. }