|
1 | 1 | /* eslint-disable max-classes-per-file */ |
2 | 2 |
|
3 | | -import { act, cleanup, fireEvent, render } from '@testing-library/react'; |
| 3 | +import { |
| 4 | + act, |
| 5 | + cleanup, |
| 6 | + fireEvent, |
| 7 | + render, |
| 8 | +} from '@testing-library/react'; |
4 | 9 | import { spyElementPrototypes } from 'rc-util/lib/test/domHook'; |
5 | 10 | import React, { createRef, StrictMode } from 'react'; |
6 | 11 | import ReactDOM from 'react-dom'; |
7 | 12 | import Trigger from '../src'; |
8 | | -import { placementAlignMap } from './util'; |
| 13 | +import { placementAlignMap, awaitFakeTimer } from './util'; |
9 | 14 |
|
10 | 15 | describe('Trigger.Basic', () => { |
11 | 16 | beforeEach(() => { |
@@ -842,4 +847,48 @@ describe('Trigger.Basic', () => { |
842 | 847 | expect(errorSpy).not.toHaveBeenCalled(); |
843 | 848 | errorSpy.mockRestore(); |
844 | 849 | }); |
| 850 | + it('should trigger align when popupAlign had updated', async () => { |
| 851 | + const onPopupAlign = jest.fn(); |
| 852 | + const App = () => { |
| 853 | + const [placementAlign, setPlacementAlign] = React.useState( |
| 854 | + placementAlignMap.leftTop, |
| 855 | + ); |
| 856 | + const [open, setOpen] = React.useState(true); |
| 857 | + return ( |
| 858 | + <> |
| 859 | + <Trigger |
| 860 | + popupVisible={open} |
| 861 | + popupAlign={placementAlign} |
| 862 | + onPopupAlign={onPopupAlign} |
| 863 | + popup={<strong className="x-content">tooltip2</strong>} |
| 864 | + > |
| 865 | + <div> |
| 866 | + <div |
| 867 | + id="btn" |
| 868 | + onClick={() => { |
| 869 | + setPlacementAlign(prev => prev === placementAlignMap.left ? placementAlignMap.leftTop: placementAlignMap.left); |
| 870 | + }} |
| 871 | + > |
| 872 | + click |
| 873 | + </div> |
| 874 | + <div id="close" onClick={() => { |
| 875 | + setOpen(false); |
| 876 | + }}>close</div> |
| 877 | + </div> |
| 878 | + </Trigger> |
| 879 | + </> |
| 880 | + ); |
| 881 | + }; |
| 882 | + render(<App />); |
| 883 | + await awaitFakeTimer(); |
| 884 | + expect(onPopupAlign).toHaveBeenCalledTimes(1); |
| 885 | + fireEvent.click(document.querySelector('#btn')); |
| 886 | + await awaitFakeTimer(); |
| 887 | + expect(onPopupAlign).toHaveBeenCalledTimes(2); |
| 888 | + fireEvent.click(document.querySelector('#close')); |
| 889 | + await awaitFakeTimer(); |
| 890 | + fireEvent.click(document.querySelector('#btn')); |
| 891 | + await awaitFakeTimer(); |
| 892 | + expect(onPopupAlign).toHaveBeenCalledTimes(2); |
| 893 | + }); |
845 | 894 | }); |
0 commit comments