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