diff --git a/components/dropdown/__tests__/__snapshots__/demo.test.js.snap b/components/dropdown/__tests__/__snapshots__/demo.test.ts.snap similarity index 100% rename from components/dropdown/__tests__/__snapshots__/demo.test.js.snap rename to components/dropdown/__tests__/__snapshots__/demo.test.ts.snap diff --git a/components/dropdown/__tests__/__snapshots__/dropdown-button.test.js.snap b/components/dropdown/__tests__/__snapshots__/dropdown-button.test.tsx.snap similarity index 100% rename from components/dropdown/__tests__/__snapshots__/dropdown-button.test.js.snap rename to components/dropdown/__tests__/__snapshots__/dropdown-button.test.tsx.snap diff --git a/components/dropdown/__tests__/__snapshots__/index.test.js.snap b/components/dropdown/__tests__/__snapshots__/index.test.tsx.snap similarity index 100% rename from components/dropdown/__tests__/__snapshots__/index.test.js.snap rename to components/dropdown/__tests__/__snapshots__/index.test.tsx.snap diff --git a/components/dropdown/__tests__/demo.test.js b/components/dropdown/__tests__/demo.test.ts similarity index 100% rename from components/dropdown/__tests__/demo.test.js rename to components/dropdown/__tests__/demo.test.ts diff --git a/components/dropdown/__tests__/dropdown-button.test.js b/components/dropdown/__tests__/dropdown-button.test.js deleted file mode 100644 index 0d6485ddbc..0000000000 --- a/components/dropdown/__tests__/dropdown-button.test.js +++ /dev/null @@ -1,106 +0,0 @@ -import { mount } from 'enzyme'; -import React from 'react'; -import Dropdown from '..'; -import mountTest from '../../../tests/shared/mountTest'; -import rtlTest from '../../../tests/shared/rtlTest'; -import Menu from '../../menu'; - -describe('DropdownButton', () => { - mountTest(Dropdown.Button); - rtlTest(Dropdown.Button); - - it('pass appropriate props to Dropdown', () => { - const props = { - align: { - offset: [10, 20], - }, - overlay: ( - - foo - - ), - disabled: false, - trigger: ['hover'], - visible: true, - onVisibleChange: () => {}, - }; - - const wrapper = mount(); - const dropdownProps = wrapper.find(Dropdown).props(); - - Object.keys(props).forEach(key => { - expect(dropdownProps[key]).toBe(props[key]); - }); - }); - - it("don't pass visible to Dropdown if it's not exits", () => { - const menu = ( - - foo - - ); - const wrapper = mount(); - const dropdownProps = wrapper.find(Dropdown).props(); - - expect('visible' in dropdownProps).toBe(false); - }); - - it('should support href like Button', () => { - const menu = ( - - foo - - ); - const wrapper = mount(); - expect(wrapper.render()).toMatchSnapshot(); - }); - - it('have static property for type detecting', () => { - const menu = ( - - foo - - ); - const wrapper = mount(); - expect(wrapper.find(Dropdown.Button).type().__ANT_BUTTON).toBe(true); - }); - - it('should pass mouseEnterDelay and mouseLeaveDelay to Dropdown', () => { - const menu = ( - - foo - - ); - const wrapper = mount( - , - ); - expect(wrapper.find('Dropdown').props().mouseEnterDelay).toBe(1); - expect(wrapper.find('Dropdown').props().mouseLeaveDelay).toBe(2); - }); - - it('should support overlayClassName and overlayStyle', () => { - const menu = ( - - foo - - ); - const wrapper = mount( - , - ); - expect(wrapper.find('.ant-dropdown').getDOMNode().className).toContain('className'); - expect(wrapper.find('.ant-dropdown').getDOMNode().style.color).toContain('red'); - }); - - it('should support loading', () => { - const wrapper = mount(); - - expect(wrapper.find('.ant-dropdown-button .ant-btn-loading').getDOMNode().className).toContain( - 'ant-btn', - ); - }); -}); diff --git a/components/dropdown/__tests__/dropdown-button.test.tsx b/components/dropdown/__tests__/dropdown-button.test.tsx new file mode 100644 index 0000000000..c2683b1660 --- /dev/null +++ b/components/dropdown/__tests__/dropdown-button.test.tsx @@ -0,0 +1,117 @@ +import React from 'react'; +import DropdownButton from '../dropdown-button'; +import mountTest from '../../../tests/shared/mountTest'; +import rtlTest from '../../../tests/shared/rtlTest'; +import Menu from '../../menu'; +import type { DropdownProps } from '../dropdown'; +import { render } from '../../../tests/utils'; + +let dropdownProps: DropdownProps; +jest.mock('../dropdown', () => { + const ActualDropdown = jest.requireActual('../dropdown'); + const ActualDropdownComponent = ActualDropdown.default; + const h: typeof React = jest.requireActual('react'); + + const mockedDropdown = (props: DropdownProps) => { + dropdownProps = props; + const { children, ...restProps } = props; + return h.createElement(ActualDropdownComponent, { ...restProps }, children); + }; + mockedDropdown.defaultProps = ActualDropdownComponent.defaultProps; + mockedDropdown.Button = ActualDropdownComponent.Button; + + return { + ...ActualDropdown, + __esModule: true, + default: mockedDropdown, + }; +}); + +describe('DropdownButton', () => { + mountTest(DropdownButton); + rtlTest(DropdownButton); + + it('pass appropriate props to Dropdown', () => { + const props: DropdownProps = { + align: { + offset: [10, 20], + }, + overlay: ( + + foo + + ), + disabled: false, + trigger: ['hover'], + visible: true, + onVisibleChange: () => {}, + }; + + render(); + + Object.keys(props).forEach((key: keyof DropdownProps) => { + expect(dropdownProps[key]).toBe(props[key]); + }); + }); + + it("don't pass visible to Dropdown if it's not exits", () => { + const menu = ( + + foo + + ); + render(); + expect('visible' in dropdownProps).toBe(false); + }); + + it('should support href like Button', () => { + const menu = ( + + foo + + ); + const { asFragment } = render(); + expect(asFragment().firstChild).toMatchSnapshot(); + }); + + it('have static property for type detecting', () => { + expect(DropdownButton.__ANT_BUTTON).toBe(true); + }); + + it('should pass mouseEnterDelay and mouseLeaveDelay to Dropdown', () => { + const menu = ( + + foo + + ); + render(); + expect(dropdownProps.mouseEnterDelay).toBe(1); + expect(dropdownProps.mouseLeaveDelay).toBe(2); + }); + + it('should support overlayClassName and overlayStyle', () => { + const menu = ( + + foo + + ); + const { container } = render( + , + ); + expect(container.querySelector('.ant-dropdown')?.classList).toContain('className'); + expect((container.querySelector('.ant-dropdown') as HTMLElement).style.color).toContain('red'); + }); + + it('should support loading', () => { + const { container } = render(} loading />); + + expect(container.querySelector('.ant-dropdown-button .ant-btn-loading')?.classList).toContain( + 'ant-btn', + ); + }); +}); diff --git a/components/dropdown/__tests__/index.test.js b/components/dropdown/__tests__/index.test.tsx similarity index 71% rename from components/dropdown/__tests__/index.test.js rename to components/dropdown/__tests__/index.test.tsx index cf1a3dc31d..e8f0fdba40 100644 --- a/components/dropdown/__tests__/index.test.js +++ b/components/dropdown/__tests__/index.test.tsx @@ -1,44 +1,61 @@ -import { mount } from 'enzyme'; import React from 'react'; +import type { TriggerProps } from 'rc-trigger'; import Dropdown from '..'; +import type { DropDownProps } from '..'; import mountTest from '../../../tests/shared/mountTest'; import rtlTest from '../../../tests/shared/rtlTest'; import { act, fireEvent, render, sleep } from '../../../tests/utils'; import Menu from '../../menu'; +let triggerProps: TriggerProps; + +jest.mock('rc-trigger', () => { + let Trigger = jest.requireActual('rc-trigger/lib/mock'); + Trigger = Trigger.default || Trigger; + const h: typeof React = jest.requireActual('react'); + + return { + default: h.forwardRef((props, ref) => { + triggerProps = props; + return h.createElement(Trigger, { ref, ...props }); + }), + __esModule: true, + }; +}); + describe('Dropdown', () => { mountTest(() => ( - }> + }> )); rtlTest(() => ( - }> + }> )); it('overlay is function and has custom transitionName', () => { - const wrapper = mount( + const { asFragment } = render(
menu
} transitionName="move-up" visible>
, ); - expect(wrapper.render()).toMatchSnapshot(); + expect(Array.from(asFragment().childNodes)).toMatchSnapshot(); }); it('overlay is string', () => { - const wrapper = mount( - + const { asFragment } = render( + , ); - expect(wrapper.render()).toMatchSnapshot(); + expect(Array.from(asFragment().childNodes)).toMatchSnapshot(); }); it('support Menu expandIcon', async () => { - const props = { + const props: DropDownProps = { overlay: ( }> foo @@ -51,23 +68,23 @@ describe('Dropdown', () => { getPopupContainer: node => node, }; - const wrapper = mount( + const { container } = render( , ); await sleep(500); - expect(wrapper.find(Dropdown).find('#customExpandIcon').length).toBe(1); + expect(container.querySelectorAll('#customExpandIcon').length).toBe(1); }); it('should warn if use topCenter or bottomCenter', () => { const error = jest.spyOn(console, 'error'); - mount( + render(
- + - +
, @@ -82,13 +99,13 @@ describe('Dropdown', () => { // zombieJ: when replaced with react test lib, it may be mock fully content it('dropdown should support auto adjust placement', () => { - const wrapper = mount( + render( menu} visible> , ); - expect(wrapper.find('Trigger').prop('builtinPlacements')).toEqual( + expect(triggerProps.builtinPlacements).toEqual( expect.objectContaining({ bottomLeft: expect.objectContaining({ overflow: { @@ -104,7 +121,7 @@ describe('Dropdown', () => { jest.useFakeTimers(); const { container } = render( { ); // Open - fireEvent.click(container.querySelector('a')); + fireEvent.click(container.querySelector('a')!); act(() => { jest.runAllTimers(); }); // Close - fireEvent.click(container.querySelector('.ant-dropdown-menu-item')); + fireEvent.click(container.querySelector('.ant-dropdown-menu-item')!); // Force Motion move on for (let i = 0; i < 10; i += 1) { @@ -143,7 +160,7 @@ describe('Dropdown', () => { } // Motion End - fireEvent.animationEnd(container.querySelector('.ant-slide-up-leave-active')); + fireEvent.animationEnd(container.querySelector('.ant-slide-up-leave-active')!); expect(container.querySelector('.ant-dropdown-hidden')).toBeTruthy();