fire-event.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.fireEvent = void 0;
  6. var _dom = require("@testing-library/dom");
  7. // react-testing-library's version of fireEvent will call
  8. // dom-testing-library's version of fireEvent. The reason
  9. // we make this distinction however is because we have
  10. // a few extra events that work a bit differently
  11. const fireEvent = (...args) => (0, _dom.fireEvent)(...args);
  12. exports.fireEvent = fireEvent;
  13. Object.keys(_dom.fireEvent).forEach(key => {
  14. fireEvent[key] = (...args) => _dom.fireEvent[key](...args);
  15. }); // React event system tracks native mouseOver/mouseOut events for
  16. // running onMouseEnter/onMouseLeave handlers
  17. // @link https://github.com/facebook/react/blob/b87aabdfe1b7461e7331abb3601d9e6bb27544bc/packages/react-dom/src/events/EnterLeaveEventPlugin.js#L24-L31
  18. const mouseEnter = fireEvent.mouseEnter;
  19. const mouseLeave = fireEvent.mouseLeave;
  20. fireEvent.mouseEnter = (...args) => {
  21. mouseEnter(...args);
  22. return fireEvent.mouseOver(...args);
  23. };
  24. fireEvent.mouseLeave = (...args) => {
  25. mouseLeave(...args);
  26. return fireEvent.mouseOut(...args);
  27. };
  28. const pointerEnter = fireEvent.pointerEnter;
  29. const pointerLeave = fireEvent.pointerLeave;
  30. fireEvent.pointerEnter = (...args) => {
  31. pointerEnter(...args);
  32. return fireEvent.pointerOver(...args);
  33. };
  34. fireEvent.pointerLeave = (...args) => {
  35. pointerLeave(...args);
  36. return fireEvent.pointerOut(...args);
  37. };
  38. const select = fireEvent.select;
  39. fireEvent.select = (node, init) => {
  40. select(node, init); // React tracks this event only on focused inputs
  41. node.focus(); // React creates this event when one of the following native events happens
  42. // - contextMenu
  43. // - mouseUp
  44. // - dragEnd
  45. // - keyUp
  46. // - keyDown
  47. // so we can use any here
  48. // @link https://github.com/facebook/react/blob/b87aabdfe1b7461e7331abb3601d9e6bb27544bc/packages/react-dom/src/events/SelectEventPlugin.js#L203-L224
  49. fireEvent.keyUp(node, init);
  50. }; // React event system tracks native focusout/focusin events for
  51. // running blur/focus handlers
  52. // @link https://github.com/facebook/react/pull/19186
  53. const blur = fireEvent.blur;
  54. const focus = fireEvent.focus;
  55. fireEvent.blur = (...args) => {
  56. fireEvent.focusOut(...args);
  57. return blur(...args);
  58. };
  59. fireEvent.focus = (...args) => {
  60. fireEvent.focusIn(...args);
  61. return focus(...args);
  62. };