import React from 'react'; import { act } from 'react-dom/test-utils'; import { fireEvent, render, waitFakeTimer } from '../../../tests/utils'; import { resetWarned } from '../../_util/warning'; describe('Collapse', () => { // eslint-disable-next-line global-require const Collapse = require('..').default; const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); // fix React concurrent function triggerAllTimer() { for (let i = 0; i < 10; i += 1) { act(() => { jest.runAllTimers(); }); } } beforeEach(() => { resetWarned(); }); afterEach(() => { errorSpy.mockReset(); }); afterAll(() => { errorSpy.mockRestore(); }); it('should support remove expandIcon', () => { const { asFragment } = render( null}> , ); expect(asFragment().firstChild).toMatchSnapshot(); }); it('should be able to config size', () => { const { container: small } = render(); const { container: large } = render(); expect(small.querySelector('.ant-collapse')).toHaveClass('ant-collapse-small'); expect(large.querySelector('.ant-collapse')).toHaveClass('ant-collapse-large'); }); it('should keep the className of the expandIcon', () => { const { container } = render( ( )} > , ); expect(container.querySelectorAll('.custom-expandicon-classname').length).toBe(1); }); it('should render extra node of panel', () => { const { asFragment } = render( action} /> action} /> , ); expect(asFragment().firstChild).toMatchSnapshot(); }); it('could be expand and collapse', async () => { jest.useFakeTimers(); const { container } = render( content , ); expect( container.querySelector('.ant-collapse-item')?.classList.contains('ant-collapse-item-active'), ).toBe(false); fireEvent.click(container.querySelector('.ant-collapse-header')!); await waitFakeTimer(); expect( container.querySelector('.ant-collapse-item')?.classList.contains('ant-collapse-item-active'), ).toBe(true); jest.useRealTimers(); }); it('could override default openMotion', () => { const { container, asFragment } = render( content , ); fireEvent.click(container.querySelector('.ant-collapse-header')!); expect(asFragment().firstChild).toMatchSnapshot(); }); it('should trigger warning and keep compatibility when using disabled in Panel', () => { const { container } = render( content , ); expect(errorSpy).toHaveBeenCalledWith( 'Warning: [antd: Collapse.Panel] `disabled` is deprecated. Please use `collapsible="disabled"` instead.', ); expect(container.querySelectorAll('.ant-collapse-item-disabled').length).toBe(1); fireEvent.click(container.querySelector('.ant-collapse-header')!); expect(container.querySelectorAll('.ant-collapse-item-active').length).toBe(0); }); it('should not trigger warning when using items instead of children', () => { render( aaa

, }, { key: '2', label: 'This is panel header 2', children:

bbb

, }, { key: '3', label: 'This is panel header 3', children:

ccc

, }, ]} />, ); expect(errorSpy).not.toHaveBeenCalledWith( 'Warning: `children` will be removed in next major version. Please use `items` instead.', ); }); it('should end motion when set activeKey while hiding', async () => { jest.useFakeTimers(); const spiedRAF = jest .spyOn(window, 'requestAnimationFrame') .mockImplementation((cb) => setTimeout(cb, 16.66)); let setActiveKeyOuter: React.Dispatch>; const Test: React.FC = () => { const [activeKey, setActiveKey] = React.useState(); setActiveKeyOuter = setActiveKey; return ( ); }; const { container } = render(); await act(async () => { setActiveKeyOuter('1'); await Promise.resolve(); }); triggerAllTimer(); expect(container.querySelectorAll('.ant-motion-collapse').length).toBe(0); spiedRAF.mockRestore(); jest.useRealTimers(); }); it('ref should work', () => { const ref = React.createRef(); const panelRef1 = React.createRef(); const panelRef2 = React.createRef(); const { container } = render( 1 2 2 , ); expect(ref.current).toBe(container.firstChild); expect(panelRef1.current).toBe(document.querySelectorAll('.ant-collapse-item')[0]); expect(panelRef2.current).toBe(document.querySelectorAll('.ant-collapse-item')[1]); }); describe('expandIconPosition', () => { ['left', 'right'].forEach((pos) => { it(`warning for legacy '${pos}'`, () => { render( , ); expect(errorSpy).toHaveBeenCalledWith( 'Warning: [antd: Collapse] `expandIconPosition` with `left` or `right` is deprecated. Please use `start` or `end` instead.', ); }); it('position end', () => { const { container } = render( , ); expect(container.querySelector('.ant-collapse-icon-position-end')).toBeTruthy(); }); }); }); });