|
| 1 | +import { act, fireEvent } from '@testing-library/react'; |
| 2 | +import { resetWarned } from 'rc-util/lib/warning'; |
| 3 | +import React from 'react'; |
| 4 | +import { createRoot } from 'react-dom/client'; |
| 5 | +import Trigger from '../src'; |
| 6 | +import { awaitFakeTimer } from './util'; |
| 7 | + |
| 8 | +describe('Trigger.Shadow', () => { |
| 9 | + beforeEach(() => { |
| 10 | + resetWarned(); |
| 11 | + jest.useFakeTimers(); |
| 12 | + }); |
| 13 | + |
| 14 | + afterEach(() => { |
| 15 | + jest.useRealTimers(); |
| 16 | + }); |
| 17 | + |
| 18 | + const Demo: React.FC = (props?: any) => ( |
| 19 | + <> |
| 20 | + <Trigger |
| 21 | + action={['click']} |
| 22 | + popup={<span className="bamboo" />} |
| 23 | + builtinPlacements={{ |
| 24 | + top: {}, |
| 25 | + }} |
| 26 | + popupPlacement="top" |
| 27 | + {...props} |
| 28 | + > |
| 29 | + <p className="target" /> |
| 30 | + </Trigger> |
| 31 | + |
| 32 | + {/* Placeholder element which not related with Trigger */} |
| 33 | + <div className="little" /> |
| 34 | + </> |
| 35 | + ); |
| 36 | + |
| 37 | + const renderShadow = (props?: any) => { |
| 38 | + const host = document.createElement('div'); |
| 39 | + document.body.appendChild(host); |
| 40 | + |
| 41 | + const shadowRoot = host.attachShadow({ |
| 42 | + mode: 'open', |
| 43 | + delegatesFocus: false, |
| 44 | + }); |
| 45 | + const container = document.createElement('div'); |
| 46 | + shadowRoot.appendChild(container); |
| 47 | + |
| 48 | + act(() => { |
| 49 | + createRoot(container).render(<Demo {...props} />); |
| 50 | + }); |
| 51 | + |
| 52 | + return shadowRoot; |
| 53 | + }; |
| 54 | + |
| 55 | + it('popup not in the same shadow', async () => { |
| 56 | + const errSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); |
| 57 | + const shadowRoot = renderShadow(); |
| 58 | + |
| 59 | + await awaitFakeTimer(); |
| 60 | + |
| 61 | + fireEvent.click(shadowRoot.querySelector('.target')); |
| 62 | + |
| 63 | + await awaitFakeTimer(); |
| 64 | + |
| 65 | + expect(errSpy).toHaveBeenCalledWith( |
| 66 | + `Warning: trigger element and popup element should in same shadow root.`, |
| 67 | + ); |
| 68 | + errSpy.mockRestore(); |
| 69 | + }); |
| 70 | + |
| 71 | + it('click in shadow should not close popup', async () => { |
| 72 | + const errSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); |
| 73 | + const shadowRoot = renderShadow({ |
| 74 | + getPopupContainer: (item: HTMLElement) => item.parentElement, |
| 75 | + autoDestroy: true, |
| 76 | + }); |
| 77 | + |
| 78 | + await awaitFakeTimer(); |
| 79 | + |
| 80 | + // Click to show |
| 81 | + fireEvent.click(shadowRoot.querySelector('.target')); |
| 82 | + await awaitFakeTimer(); |
| 83 | + expect(shadowRoot.querySelector('.bamboo')).toBeTruthy(); |
| 84 | + |
| 85 | + // Click outside to hide |
| 86 | + fireEvent.click(document.body.firstChild); |
| 87 | + await awaitFakeTimer(); |
| 88 | + expect(shadowRoot.querySelector('.bamboo')).toBeFalsy(); |
| 89 | + |
| 90 | + // Click to show again |
| 91 | + fireEvent.click(shadowRoot.querySelector('.target')); |
| 92 | + await awaitFakeTimer(); |
| 93 | + expect(shadowRoot.querySelector('.bamboo')).toBeTruthy(); |
| 94 | + |
| 95 | + // Click in side shadow to hide |
| 96 | + fireEvent.click(shadowRoot.querySelector('.little')); |
| 97 | + await awaitFakeTimer(); |
| 98 | + expect(shadowRoot.querySelector('.bamboo')).toBeFalsy(); |
| 99 | + |
| 100 | + expect(errSpy).not.toHaveBeenCalled(); |
| 101 | + errSpy.mockRestore(); |
| 102 | + }); |
| 103 | +}); |
0 commit comments