to-have-description.js 1.5 KB

1234567891011121314151617181920212223242526272829303132
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.toHaveDescription = toHaveDescription;
  6. var _utils = require("./utils");
  7. // See algoritm: https://www.w3.org/TR/accname-1.1/#mapping_additional_nd_description
  8. function toHaveDescription(htmlElement, checkWith) {
  9. (0, _utils.deprecate)('toBeInTheDOM', 'Please use toBeInTheDocument for searching the entire document and toContainElement for searching a specific container.');
  10. (0, _utils.checkHtmlElement)(htmlElement, toHaveDescription, this);
  11. const expectsDescription = checkWith !== undefined;
  12. const descriptionIDRaw = htmlElement.getAttribute('aria-describedby') || '';
  13. const descriptionIDs = descriptionIDRaw.split(/\s+/).filter(Boolean);
  14. let description = '';
  15. if (descriptionIDs.length > 0) {
  16. const document = htmlElement.ownerDocument;
  17. const descriptionEls = descriptionIDs.map(descriptionID => document.getElementById(descriptionID)).filter(Boolean);
  18. description = (0, _utils.normalize)(descriptionEls.map(el => el.textContent).join(' '));
  19. }
  20. return {
  21. pass: expectsDescription ? checkWith instanceof RegExp ? checkWith.test(description) : this.equals(description, checkWith) : Boolean(description),
  22. message: () => {
  23. const to = this.isNot ? 'not to' : 'to';
  24. return (0, _utils.getMessage)(this, this.utils.matcherHint(`${this.isNot ? '.not' : ''}.toHaveDescription`, 'element', ''), `Expected the element ${to} have description`, this.utils.printExpected(checkWith), 'Received', this.utils.printReceived(description));
  25. }
  26. };
  27. }