|
1 | 1 | import React from 'react';
|
2 | 2 | import { mount } from 'enzyme';
|
| 3 | +import { act } from 'react-dom/test-utils'; |
3 | 4 | import { spyElementPrototypes } from 'rc-util/lib/test/domHook';
|
4 | 5 | import Portal from 'rc-util/lib/Portal';
|
5 | 6 | import Trigger from '../src';
|
@@ -683,4 +684,60 @@ describe('Trigger.Basic', () => {
|
683 | 684 | expect(popupDomNode.style.color).toBe(style.color);
|
684 | 685 | expect(popupDomNode.style.top).toBe(`${style.top}px`);
|
685 | 686 | });
|
| 687 | + |
| 688 | + describe('getContainer', () => { |
| 689 | + it('not trigger when dom not ready', () => { |
| 690 | + const getPopupContainer = jest.fn(node => node.parentElement); |
| 691 | + |
| 692 | + function Demo() { |
| 693 | + return ( |
| 694 | + <Trigger |
| 695 | + popupVisible |
| 696 | + getPopupContainer={getPopupContainer} |
| 697 | + popup={<strong className="x-content">tooltip2</strong>} |
| 698 | + > |
| 699 | + <div className="target">click</div> |
| 700 | + </Trigger> |
| 701 | + ); |
| 702 | + } |
| 703 | + |
| 704 | + const wrapper = mount(<Demo />); |
| 705 | + |
| 706 | + expect(getPopupContainer).toHaveReturnedTimes(0); |
| 707 | + |
| 708 | + act(() => { |
| 709 | + jest.runAllTimers(); |
| 710 | + }); |
| 711 | + expect(getPopupContainer).toHaveReturnedTimes(1); |
| 712 | + expect(getPopupContainer).toHaveBeenCalledWith( |
| 713 | + wrapper.find('.target').instance(), |
| 714 | + ); |
| 715 | + }); |
| 716 | + |
| 717 | + it('not trigger when dom no need', () => { |
| 718 | + let triggerTimes = 0; |
| 719 | + const getPopupContainer = () => { |
| 720 | + triggerTimes += 1; |
| 721 | + return document.body; |
| 722 | + }; |
| 723 | + |
| 724 | + function Demo() { |
| 725 | + return ( |
| 726 | + <Trigger |
| 727 | + popupVisible |
| 728 | + getPopupContainer={getPopupContainer} |
| 729 | + popup={<strong className="x-content">tooltip2</strong>} |
| 730 | + > |
| 731 | + <div className="target">click</div> |
| 732 | + </Trigger> |
| 733 | + ); |
| 734 | + } |
| 735 | + |
| 736 | + const wrapper = mount(<Demo />); |
| 737 | + |
| 738 | + expect(triggerTimes).toEqual(1); |
| 739 | + |
| 740 | + wrapper.unmount(); |
| 741 | + }); |
| 742 | + }); |
686 | 743 | });
|
0 commit comments