to-have-accessible-description.js 1.4 KB

123456789101112131415161718192021222324252627282930313233
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.toHaveAccessibleDescription = toHaveAccessibleDescription;
  6. var _domAccessibilityApi = require("dom-accessibility-api");
  7. var _utils = require("./utils");
  8. function toHaveAccessibleDescription(htmlElement, expectedAccessibleDescription) {
  9. (0, _utils.checkHtmlElement)(htmlElement, toHaveAccessibleDescription, this);
  10. const actualAccessibleDescription = (0, _domAccessibilityApi.computeAccessibleDescription)(htmlElement);
  11. const missingExpectedValue = arguments.length === 1;
  12. let pass = false;
  13. if (missingExpectedValue) {
  14. // When called without an expected value we only want to validate that the element has an
  15. // accessible description, whatever it may be.
  16. pass = actualAccessibleDescription !== '';
  17. } else {
  18. pass = expectedAccessibleDescription instanceof RegExp ? expectedAccessibleDescription.test(actualAccessibleDescription) : this.equals(actualAccessibleDescription, expectedAccessibleDescription);
  19. }
  20. return {
  21. pass,
  22. message: () => {
  23. const to = this.isNot ? 'not to' : 'to';
  24. return (0, _utils.getMessage)(this, this.utils.matcherHint(`${this.isNot ? '.not' : ''}.${toHaveAccessibleDescription.name}`, 'element', ''), `Expected element ${to} have accessible description`, expectedAccessibleDescription, 'Received', actualAccessibleDescription);
  25. }
  26. };
  27. }