|
| 1 | +import { cleanup, fireEvent, render } from '@testing-library/react'; |
| 2 | +import React from 'react'; |
| 3 | +import Trigger, { UniqueProvider } from '../src'; |
| 4 | +import { awaitFakeTimer } from './util'; |
| 5 | + |
| 6 | +describe('Trigger.Unique', () => { |
| 7 | + beforeEach(() => { |
| 8 | + jest.useFakeTimers(); |
| 9 | + }); |
| 10 | + |
| 11 | + afterEach(() => { |
| 12 | + cleanup(); |
| 13 | + jest.useRealTimers(); |
| 14 | + }); |
| 15 | + |
| 16 | + it('moving will not hide the popup', async () => { |
| 17 | + const { container } = render( |
| 18 | + <UniqueProvider> |
| 19 | + <Trigger |
| 20 | + action={['hover']} |
| 21 | + popup={<strong className="x-content">tooltip1</strong>} |
| 22 | + unique |
| 23 | + > |
| 24 | + <div className="target1">hover1</div> |
| 25 | + </Trigger> |
| 26 | + <Trigger |
| 27 | + action={['hover']} |
| 28 | + popup={<strong className="x-content">tooltip2</strong>} |
| 29 | + unique |
| 30 | + > |
| 31 | + <div className="target2">hover2</div> |
| 32 | + </Trigger> |
| 33 | + </UniqueProvider>, |
| 34 | + ); |
| 35 | + |
| 36 | + // Initially no popup should be visible |
| 37 | + expect(document.querySelector('.rc-trigger-popup')).toBeFalsy(); |
| 38 | + |
| 39 | + // Hover first trigger |
| 40 | + fireEvent.mouseEnter(container.querySelector('.target1')); |
| 41 | + await awaitFakeTimer(); |
| 42 | + expect(document.querySelector('.x-content').textContent).toBe('tooltip1'); |
| 43 | + expect(document.querySelector('.rc-trigger-popup')).toBeTruthy(); |
| 44 | + |
| 45 | + // Move from first to second trigger - popup should not hide, but content should change |
| 46 | + fireEvent.mouseLeave(container.querySelector('.target1')); |
| 47 | + fireEvent.mouseEnter(container.querySelector('.target2')); |
| 48 | + await awaitFakeTimer(); |
| 49 | + |
| 50 | + // Popup should still be visible with new content |
| 51 | + expect(document.querySelector('.x-content').textContent).toBe('tooltip2'); |
| 52 | + expect(document.querySelector('.rc-trigger-popup')).toBeTruthy(); |
| 53 | + |
| 54 | + // There should only be one popup element |
| 55 | + expect(document.querySelectorAll('.rc-trigger-popup').length).toBe(1); |
| 56 | + }); |
| 57 | +}); |
0 commit comments