|
| 1 | +/* eslint-disable max-classes-per-file */ |
| 2 | + |
| 3 | +import { act, cleanup, fireEvent, render } from '@testing-library/react'; |
| 4 | +import { spyElementPrototypes } from 'rc-util/lib/test/domHook'; |
| 5 | +import React from 'react'; |
| 6 | +import ReactDOM from 'react-dom'; |
| 7 | +import Trigger from '../src'; |
| 8 | +import { placementAlignMap } from './util'; |
| 9 | + |
| 10 | +describe('Trigger.Portal', () => { |
| 11 | + beforeAll(() => { |
| 12 | + spyElementPrototypes(HTMLElement, { |
| 13 | + offsetParent: { |
| 14 | + get: () => document.body, |
| 15 | + }, |
| 16 | + }); |
| 17 | + }); |
| 18 | + |
| 19 | + beforeEach(() => { |
| 20 | + jest.useFakeTimers(); |
| 21 | + }); |
| 22 | + |
| 23 | + afterEach(() => { |
| 24 | + cleanup(); |
| 25 | + jest.useRealTimers(); |
| 26 | + }); |
| 27 | + |
| 28 | + it('no trigger with portal element', () => { |
| 29 | + const PortalBox = () => { |
| 30 | + return ReactDOM.createPortal( |
| 31 | + <div className="portal-box" />, |
| 32 | + document.body, |
| 33 | + ); |
| 34 | + }; |
| 35 | + |
| 36 | + const onPopupVisibleChange = jest.fn(); |
| 37 | + |
| 38 | + const { container } = render( |
| 39 | + <div className="holder"> |
| 40 | + <Trigger |
| 41 | + action={['hover']} |
| 42 | + popupAlign={placementAlignMap.left} |
| 43 | + onPopupVisibleChange={onPopupVisibleChange} |
| 44 | + popup={ |
| 45 | + <strong className="x-content"> |
| 46 | + tooltip2 |
| 47 | + <PortalBox /> |
| 48 | + </strong> |
| 49 | + } |
| 50 | + > |
| 51 | + <div className="target">hover</div> |
| 52 | + </Trigger> |
| 53 | + </div>, |
| 54 | + ); |
| 55 | + |
| 56 | + // Show the popup |
| 57 | + fireEvent.mouseEnter(container.querySelector('.target')); |
| 58 | + expect(onPopupVisibleChange).toHaveBeenCalledWith(true); |
| 59 | + fireEvent.mouseLeave(container.querySelector('.target')); |
| 60 | + |
| 61 | + // Mouse enter popup |
| 62 | + fireEvent.mouseEnter(document.querySelector('.x-content')); |
| 63 | + fireEvent.mouseLeave(document.querySelector('.x-content')); |
| 64 | + |
| 65 | + // To Portal |
| 66 | + fireEvent.mouseEnter(document.querySelector('.portal-box')); |
| 67 | + act(() => { |
| 68 | + jest.runAllTimers(); |
| 69 | + }); |
| 70 | + |
| 71 | + expect(onPopupVisibleChange).toHaveBeenCalledWith(false); |
| 72 | + }); |
| 73 | +}); |
0 commit comments