2022-06-22 14:57:09 +08:00
|
|
|
import React from 'react';
|
2021-05-18 13:02:17 +08:00
|
|
|
import { act } from 'react-dom/test-utils';
|
2022-07-21 11:18:14 +08:00
|
|
|
import { sleep, render, fireEvent } 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', () => {
|
2019-09-27 23:24:02 +08:00
|
|
|
// eslint-disable-next-line global-require
|
|
|
|
const Collapse = require('..').default;
|
2019-08-26 22:53:20 +08:00
|
|
|
|
2020-11-30 15:20:42 +08:00
|
|
|
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
|
|
|
|
2022-07-21 11:18:14 +08:00
|
|
|
// fix React concurrent
|
|
|
|
function triggerAllTimer() {
|
|
|
|
for (let i = 0; i < 10; i += 1) {
|
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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}>
|
|
|
|
<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
|
|
|
|
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>
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
<Collapse.Panel header="header" />
|
|
|
|
</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>
|
|
|
|
<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 () => {
|
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')!);
|
2020-03-20 15:07:47 +08:00
|
|
|
await sleep(400);
|
2022-07-21 11:18:14 +08:00
|
|
|
expect(
|
|
|
|
container.querySelector('.ant-collapse-item')?.classList.contains('ant-collapse-item-active'),
|
|
|
|
).toBe(true);
|
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(
|
2020-11-30 15:20:42 +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
|
|
|
|
|
|
|
it('should end motion when set activeKey while hiding', async () => {
|
|
|
|
jest.useFakeTimers();
|
2022-07-21 11:18:14 +08:00
|
|
|
const spiedRAF = jest
|
|
|
|
.spyOn(window, 'requestAnimationFrame')
|
|
|
|
.mockImplementation(cb => setTimeout(cb, 16.66));
|
2021-05-18 13:02:17 +08:00
|
|
|
|
2022-07-21 11:18:14 +08:00
|
|
|
let setActiveKeyOuter: React.Dispatch<React.SetStateAction<React.Key | undefined>>;
|
2021-05-18 13:02:17 +08:00
|
|
|
const Test = () => {
|
|
|
|
const [activeKey, setActiveKey] = React.useState();
|
|
|
|
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();
|
2021-05-18 13:02:17 +08:00
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
2022-05-27 16:07:32 +08:00
|
|
|
|
|
|
|
describe('expandIconPosition', () => {
|
|
|
|
['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.',
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('position end', () => {
|
|
|
|
const { container } = render(
|
|
|
|
<Collapse expandIconPosition="end">
|
|
|
|
<Collapse.Panel header="header" key="1" />
|
|
|
|
</Collapse>,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(container.querySelector('.ant-collapse-icon-position-end')).toBeTruthy();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2019-01-23 10:41:07 +08:00
|
|
|
});
|