to-have-attribute.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.toHaveAttribute = toHaveAttribute;
  6. var _utils = require("./utils");
  7. function printAttribute(stringify, name, value) {
  8. return value === undefined ? name : `${name}=${stringify(value)}`;
  9. }
  10. function getAttributeComment(stringify, name, value) {
  11. return value === undefined ? `element.hasAttribute(${stringify(name)})` : `element.getAttribute(${stringify(name)}) === ${stringify(value)}`;
  12. }
  13. function toHaveAttribute(htmlElement, name, expectedValue) {
  14. (0, _utils.checkHtmlElement)(htmlElement, toHaveAttribute, this);
  15. const isExpectedValuePresent = expectedValue !== undefined;
  16. const hasAttribute = htmlElement.hasAttribute(name);
  17. const receivedValue = htmlElement.getAttribute(name);
  18. return {
  19. pass: isExpectedValuePresent ? hasAttribute && this.equals(receivedValue, expectedValue) : hasAttribute,
  20. message: () => {
  21. const to = this.isNot ? 'not to' : 'to';
  22. const receivedAttribute = hasAttribute ? printAttribute(this.utils.stringify, name, receivedValue) : null;
  23. const matcher = this.utils.matcherHint(`${this.isNot ? '.not' : ''}.toHaveAttribute`, 'element', this.utils.printExpected(name), {
  24. secondArgument: isExpectedValuePresent ? this.utils.printExpected(expectedValue) : undefined,
  25. comment: getAttributeComment(this.utils.stringify, name, expectedValue)
  26. });
  27. return (0, _utils.getMessage)(this, matcher, `Expected the element ${to} have attribute`, printAttribute(this.utils.stringify, name, expectedValue), 'Received', receivedAttribute);
  28. }
  29. };
  30. }