hover.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.hover = hover;
  6. exports.unhover = unhover;
  7. var _dom = require("@testing-library/dom");
  8. var _utils = require("./utils");
  9. // includes `element`
  10. function getParentElements(element) {
  11. const parentElements = [element];
  12. let currentElement = element;
  13. while ((currentElement = currentElement.parentElement) != null) {
  14. parentElements.push(currentElement);
  15. }
  16. return parentElements;
  17. }
  18. function hover(element, init) {
  19. if ((0, _utils.isLabelWithInternallyDisabledControl)(element)) return;
  20. const parentElements = getParentElements(element).reverse();
  21. _dom.fireEvent.pointerOver(element, init);
  22. for (const el of parentElements) {
  23. _dom.fireEvent.pointerEnter(el, init);
  24. }
  25. if (!element.disabled) {
  26. _dom.fireEvent.mouseOver(element, (0, _utils.getMouseEventOptions)('mouseover', init));
  27. for (const el of parentElements) {
  28. _dom.fireEvent.mouseEnter(el, (0, _utils.getMouseEventOptions)('mouseenter', init));
  29. }
  30. }
  31. _dom.fireEvent.pointerMove(element, init);
  32. if (!element.disabled) {
  33. _dom.fireEvent.mouseMove(element, (0, _utils.getMouseEventOptions)('mousemove', init));
  34. }
  35. }
  36. function unhover(element, init) {
  37. if ((0, _utils.isLabelWithInternallyDisabledControl)(element)) return;
  38. const parentElements = getParentElements(element);
  39. _dom.fireEvent.pointerMove(element, init);
  40. if (!element.disabled) {
  41. _dom.fireEvent.mouseMove(element, (0, _utils.getMouseEventOptions)('mousemove', init));
  42. }
  43. _dom.fireEvent.pointerOut(element, init);
  44. for (const el of parentElements) {
  45. _dom.fireEvent.pointerLeave(el, init);
  46. }
  47. if (!element.disabled) {
  48. _dom.fireEvent.mouseOut(element, (0, _utils.getMouseEventOptions)('mouseout', init));
  49. for (const el of parentElements) {
  50. _dom.fireEvent.mouseLeave(el, (0, _utils.getMouseEventOptions)('mouseleave', init));
  51. }
  52. }
  53. }