to-be-visible.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.toBeVisible = toBeVisible;
  6. var _utils = require("./utils");
  7. function isStyleVisible(element) {
  8. const {
  9. getComputedStyle
  10. } = element.ownerDocument.defaultView;
  11. const {
  12. display,
  13. visibility,
  14. opacity
  15. } = getComputedStyle(element);
  16. return display !== 'none' && visibility !== 'hidden' && visibility !== 'collapse' && opacity !== '0' && opacity !== 0;
  17. }
  18. function isAttributeVisible(element, previousElement) {
  19. return !element.hasAttribute('hidden') && (element.nodeName === 'DETAILS' && previousElement.nodeName !== 'SUMMARY' ? element.hasAttribute('open') : true);
  20. }
  21. function isElementVisible(element, previousElement) {
  22. return isStyleVisible(element) && isAttributeVisible(element, previousElement) && (!element.parentElement || isElementVisible(element.parentElement, element));
  23. }
  24. function toBeVisible(element) {
  25. (0, _utils.checkHtmlElement)(element, toBeVisible, this);
  26. const isInDocument = element.ownerDocument === element.getRootNode({
  27. composed: true
  28. });
  29. const isVisible = isInDocument && isElementVisible(element);
  30. return {
  31. pass: isVisible,
  32. message: () => {
  33. const is = isVisible ? 'is' : 'is not';
  34. return [this.utils.matcherHint(`${this.isNot ? '.not' : ''}.toBeVisible`, 'element', ''), '', `Received element ${is} visible${isInDocument ? '' : ' (element is not in the document)'}:`, ` ${this.utils.printReceived(element.cloneNode(false))}`].join('\n');
  35. }
  36. };
  37. }