to-have-accessible-name.js 1.2 KB

123456789101112131415161718192021222324252627282930313233
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.toHaveAccessibleName = toHaveAccessibleName;
  6. var _domAccessibilityApi = require("dom-accessibility-api");
  7. var _utils = require("./utils");
  8. function toHaveAccessibleName(htmlElement, expectedAccessibleName) {
  9. (0, _utils.checkHtmlElement)(htmlElement, toHaveAccessibleName, this);
  10. const actualAccessibleName = (0, _domAccessibilityApi.computeAccessibleName)(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 name, whatever it may be.
  16. pass = actualAccessibleName !== '';
  17. } else {
  18. pass = expectedAccessibleName instanceof RegExp ? expectedAccessibleName.test(actualAccessibleName) : this.equals(actualAccessibleName, expectedAccessibleName);
  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' : ''}.${toHaveAccessibleName.name}`, 'element', ''), `Expected element ${to} have accessible name`, expectedAccessibleName, 'Received', actualAccessibleName);
  25. }
  26. };
  27. }