2022-06-22 14:57:09 +08:00
|
|
|
import React from 'react';
|
2023-08-21 10:03:15 +08:00
|
|
|
|
2021-05-18 13:02:17 +08:00
|
|
|
import { act } from 'react-dom/test-utils';
|
2023-08-21 10:03:15 +08:00
|
|
|
|
2023-06-07 10:03:35 +08:00
|
|
|
import { fireEvent, render, waitFakeTimer } from '../../../tests/utils';
|
2022-05-10 15:43:29 +08:00
|
|
|
import { resetWarned } from '../../_util/warning';
|
2019-01-23 10:41:07 +08:00
|
|
|
|
|
|
|
describe('Collapse', () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
// eslint-disable-next-line global-require
|
|
|
|
const Collapse = require('..').default;
|
|
|
|
|
|
|
|
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
2020-11-30 15:20:42 +08:00
|
|
|
|
2022-07-21 11:18:14 +08:00
|
|
|
// fix React concurrent
|
|
|
|
function triggerAllTimer() {
|
|
|
|
for (let i = 0; i < 10; i += 1) {
|
|
|
|
act(() => {
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.runAllTimers();
|
2022-07-21 11:18:14 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-27 16:07:32 +08:00
|
|
|
beforeEach(() => {
|
|
|
|
resetWarned();
|
|
|
|
});
|
|
|
|
|
2020-11-30 15:20:42 +08:00
|
|
|
afterEach(() => {
|
|
|
|
errorSpy.mockReset();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
errorSpy.mockRestore();
|
|
|
|
});
|
|
|
|
|
2019-01-23 10:41:07 +08:00
|
|
|
it('should support remove expandIcon', () => {
|
2022-07-21 11:18:14 +08:00
|
|
|
const { asFragment } = render(
|
2019-01-23 10:41:07 +08:00
|
|
|
<Collapse expandIcon={() => null}>
|
2023-06-07 21:59:21 +08:00
|
|
|
<Collapse.Panel header="header" />
|
2019-03-06 13:45:40 +08:00
|
|
|
</Collapse>,
|
2019-01-23 10:41:07 +08:00
|
|
|
);
|
2022-07-21 11:18:14 +08:00
|
|
|
expect(asFragment().firstChild).toMatchSnapshot();
|
2019-01-23 10:41:07 +08:00
|
|
|
});
|
2019-02-13 19:32:49 +08:00
|
|
|
|
2023-01-21 18:45:09 +08:00
|
|
|
it('should be able to config size', () => {
|
|
|
|
const { container: small } = render(<Collapse size="small" />);
|
|
|
|
const { container: large } = render(<Collapse size="large" />);
|
|
|
|
|
|
|
|
expect(small.querySelector('.ant-collapse')).toHaveClass('ant-collapse-small');
|
|
|
|
expect(large.querySelector('.ant-collapse')).toHaveClass('ant-collapse-large');
|
|
|
|
});
|
|
|
|
|
2019-10-11 10:23:41 +08:00
|
|
|
it('should keep the className of the expandIcon', () => {
|
2022-07-21 11:18:14 +08:00
|
|
|
const { container } = render(
|
2019-10-11 10:23:41 +08:00
|
|
|
<Collapse
|
|
|
|
expandIcon={() => (
|
|
|
|
<button type="button" className="custom-expandicon-classname">
|
|
|
|
action
|
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
>
|
2023-06-07 21:59:21 +08:00
|
|
|
<Collapse.Panel header="header" />
|
2019-10-11 10:23:41 +08:00
|
|
|
</Collapse>,
|
|
|
|
);
|
|
|
|
|
2022-07-21 11:18:14 +08:00
|
|
|
expect(container.querySelectorAll('.custom-expandicon-classname').length).toBe(1);
|
2019-10-11 10:23:41 +08:00
|
|
|
});
|
|
|
|
|
2019-02-13 19:32:49 +08:00
|
|
|
it('should render extra node of panel', () => {
|
2022-07-21 11:18:14 +08:00
|
|
|
const { asFragment } = render(
|
2019-02-13 19:32:49 +08:00
|
|
|
<Collapse>
|
2023-06-07 21:59:21 +08:00
|
|
|
<Collapse.Panel header="header" extra={<button type="button">action</button>} />
|
|
|
|
<Collapse.Panel header="header" extra={<button type="button">action</button>} />
|
2019-03-06 13:45:40 +08:00
|
|
|
</Collapse>,
|
2019-02-13 19:32:49 +08:00
|
|
|
);
|
2022-07-21 11:18:14 +08:00
|
|
|
expect(asFragment().firstChild).toMatchSnapshot();
|
2019-02-13 19:32:49 +08:00
|
|
|
});
|
2019-09-27 23:24:02 +08:00
|
|
|
|
2020-03-20 15:07:47 +08:00
|
|
|
it('could be expand and collapse', async () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.useFakeTimers();
|
2022-07-21 11:18:14 +08:00
|
|
|
const { container } = render(
|
2019-09-27 23:24:02 +08:00
|
|
|
<Collapse>
|
|
|
|
<Collapse.Panel header="This is panel header 1" key="1">
|
|
|
|
content
|
|
|
|
</Collapse.Panel>
|
|
|
|
</Collapse>,
|
|
|
|
);
|
2022-07-21 11:18:14 +08:00
|
|
|
expect(
|
|
|
|
container.querySelector('.ant-collapse-item')?.classList.contains('ant-collapse-item-active'),
|
|
|
|
).toBe(false);
|
|
|
|
fireEvent.click(container.querySelector('.ant-collapse-header')!);
|
2022-10-21 15:09:21 +08:00
|
|
|
await waitFakeTimer();
|
2022-07-21 11:18:14 +08:00
|
|
|
expect(
|
|
|
|
container.querySelector('.ant-collapse-item')?.classList.contains('ant-collapse-item-active'),
|
|
|
|
).toBe(true);
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.useRealTimers();
|
2019-09-27 23:24:02 +08:00
|
|
|
});
|
2020-03-18 20:44:18 +08:00
|
|
|
|
2020-11-30 15:20:42 +08:00
|
|
|
it('could override default openMotion', () => {
|
2022-07-21 11:18:14 +08:00
|
|
|
const { container, asFragment } = render(
|
2023-06-07 21:59:21 +08:00
|
|
|
<Collapse openMotion={{}}>
|
2020-03-18 20:44:18 +08:00
|
|
|
<Collapse.Panel header="This is panel header 1" key="1">
|
|
|
|
content
|
|
|
|
</Collapse.Panel>
|
|
|
|
</Collapse>,
|
|
|
|
);
|
2022-07-21 11:18:14 +08:00
|
|
|
fireEvent.click(container.querySelector('.ant-collapse-header')!);
|
|
|
|
expect(asFragment().firstChild).toMatchSnapshot();
|
2020-03-18 20:44:18 +08:00
|
|
|
});
|
2020-11-30 15:20:42 +08:00
|
|
|
|
|
|
|
it('should trigger warning and keep compatibility when using disabled in Panel', () => {
|
2022-07-21 11:18:14 +08:00
|
|
|
const { container } = render(
|
2020-11-30 15:20:42 +08:00
|
|
|
<Collapse>
|
|
|
|
<Collapse.Panel disabled header="This is panel header 1" key="1">
|
|
|
|
content
|
|
|
|
</Collapse.Panel>
|
|
|
|
</Collapse>,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(errorSpy).toHaveBeenCalledWith(
|
|
|
|
'Warning: [antd: Collapse.Panel] `disabled` is deprecated. Please use `collapsible="disabled"` instead.',
|
|
|
|
);
|
|
|
|
|
2022-07-21 11:18:14 +08:00
|
|
|
expect(container.querySelectorAll('.ant-collapse-item-disabled').length).toBe(1);
|
2020-11-30 15:20:42 +08:00
|
|
|
|
2022-07-21 11:18:14 +08:00
|
|
|
fireEvent.click(container.querySelector('.ant-collapse-header')!);
|
|
|
|
expect(container.querySelectorAll('.ant-collapse-item-active').length).toBe(0);
|
2020-11-30 15:20:42 +08:00
|
|
|
});
|
2021-05-18 13:02:17 +08:00
|
|
|
|
2023-06-07 10:03:35 +08:00
|
|
|
it('should not trigger warning when using items instead of children', () => {
|
|
|
|
render(
|
|
|
|
<Collapse
|
|
|
|
items={[
|
|
|
|
{
|
|
|
|
key: '1',
|
|
|
|
label: 'This is panel header 1',
|
|
|
|
children: <p>aaa</p>,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: '2',
|
|
|
|
label: 'This is panel header 2',
|
|
|
|
children: <p>bbb</p>,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: '3',
|
|
|
|
label: 'This is panel header 3',
|
|
|
|
children: <p>ccc</p>,
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(errorSpy).not.toHaveBeenCalledWith(
|
|
|
|
'Warning: `children` will be removed in next major version. Please use `items` instead.',
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-05-18 13:02:17 +08:00
|
|
|
it('should end motion when set activeKey while hiding', async () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.useFakeTimers();
|
|
|
|
const spiedRAF = jest
|
2022-07-21 11:18:14 +08:00
|
|
|
.spyOn(window, 'requestAnimationFrame')
|
2022-11-19 13:47:33 +08:00
|
|
|
.mockImplementation((cb) => setTimeout(cb, 16.66));
|
2021-05-18 13:02:17 +08:00
|
|
|
|
2023-01-16 09:55:52 +08:00
|
|
|
let setActiveKeyOuter: React.Dispatch<React.SetStateAction<React.Key>>;
|
|
|
|
const Test: React.FC = () => {
|
|
|
|
const [activeKey, setActiveKey] = React.useState<React.Key>();
|
2021-05-18 13:02:17 +08:00
|
|
|
setActiveKeyOuter = setActiveKey;
|
|
|
|
return (
|
|
|
|
<div hidden>
|
|
|
|
<Collapse activeKey={activeKey}>
|
|
|
|
<Collapse.Panel header="header" key="1">
|
|
|
|
content
|
|
|
|
</Collapse.Panel>
|
|
|
|
</Collapse>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-07-21 11:18:14 +08:00
|
|
|
const { container } = render(<Test />);
|
2021-05-18 13:02:17 +08:00
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
setActiveKeyOuter('1');
|
|
|
|
await Promise.resolve();
|
|
|
|
});
|
|
|
|
|
2022-07-21 11:18:14 +08:00
|
|
|
triggerAllTimer();
|
|
|
|
|
|
|
|
expect(container.querySelectorAll('.ant-motion-collapse').length).toBe(0);
|
2021-05-18 13:02:17 +08:00
|
|
|
|
2022-07-21 11:18:14 +08:00
|
|
|
spiedRAF.mockRestore();
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.useRealTimers();
|
2021-05-18 13:02:17 +08:00
|
|
|
});
|
2022-05-27 16:07:32 +08:00
|
|
|
|
2023-02-01 22:55:41 +08:00
|
|
|
it('ref should work', () => {
|
|
|
|
const ref = React.createRef<HTMLDivElement>();
|
|
|
|
const panelRef1 = React.createRef<HTMLDivElement>();
|
|
|
|
const panelRef2 = React.createRef<HTMLDivElement>();
|
|
|
|
|
|
|
|
const { container } = render(
|
|
|
|
<Collapse ref={ref}>
|
|
|
|
<Collapse.Panel ref={panelRef1} header="panel header 1" key="1">
|
|
|
|
1
|
|
|
|
</Collapse.Panel>
|
|
|
|
<Collapse.Panel ref={panelRef2} header="panel header 2" key="2">
|
|
|
|
2
|
|
|
|
</Collapse.Panel>
|
|
|
|
<Collapse.Panel header="panel header 3" key="3">
|
|
|
|
2
|
|
|
|
</Collapse.Panel>
|
|
|
|
</Collapse>,
|
|
|
|
);
|
|
|
|
|
|
|
|
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]);
|
|
|
|
});
|
|
|
|
|
2022-05-27 16:07:32 +08:00
|
|
|
describe('expandIconPosition', () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
['left', 'right'].forEach((pos) => {
|
|
|
|
it(`warning for legacy '${pos}'`, () => {
|
|
|
|
render(
|
|
|
|
<Collapse expandIconPosition={pos}>
|
|
|
|
<Collapse.Panel header="header" key="1" />
|
|
|
|
</Collapse>,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(errorSpy).toHaveBeenCalledWith(
|
|
|
|
'Warning: [antd: Collapse] `expandIconPosition` with `left` or `right` is deprecated. Please use `start` or `end` instead.',
|
|
|
|
);
|
|
|
|
});
|
2022-05-27 16:07:32 +08:00
|
|
|
|
2023-06-07 21:59:21 +08:00
|
|
|
it('position end', () => {
|
|
|
|
const { container } = render(
|
|
|
|
<Collapse expandIconPosition="end">
|
|
|
|
<Collapse.Panel header="header" key="1" />
|
|
|
|
</Collapse>,
|
|
|
|
);
|
2022-05-27 16:07:32 +08:00
|
|
|
|
2023-06-07 21:59:21 +08:00
|
|
|
expect(container.querySelector('.ant-collapse-icon-position-end')).toBeTruthy();
|
|
|
|
});
|
2022-05-27 16:07:32 +08:00
|
|
|
});
|
|
|
|
});
|
2023-06-07 10:53:40 +08:00
|
|
|
|
|
|
|
it('Collapse.Panel usage', () => {
|
|
|
|
const { container } = render(
|
|
|
|
<Collapse bordered={false}>
|
|
|
|
<Collapse.Panel key="test panel1" header="test panel1">
|
|
|
|
test1
|
|
|
|
</Collapse.Panel>
|
|
|
|
<Collapse.Panel key="test panel2" header="test panel2">
|
|
|
|
test2
|
|
|
|
</Collapse.Panel>
|
|
|
|
</Collapse>,
|
|
|
|
);
|
|
|
|
expect(container.firstChild).toMatchSnapshot();
|
|
|
|
});
|
2019-01-23 10:41:07 +08:00
|
|
|
});
|