2019-01-23 10:41:07 +08:00
|
|
|
import { mount } from 'enzyme';
|
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-06-22 14:57:09 +08:00
|
|
|
import { render, sleep } 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-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', () => {
|
|
|
|
const wrapper = mount(
|
|
|
|
<Collapse expandIcon={() => null}>
|
|
|
|
<Collapse.Panel header="header" />
|
2019-03-06 13:45:40 +08:00
|
|
|
</Collapse>,
|
2019-01-23 10:41:07 +08:00
|
|
|
);
|
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
|
|
|
});
|
2019-02-13 19:32:49 +08:00
|
|
|
|
2019-10-11 10:23:41 +08:00
|
|
|
it('should keep the className of the expandIcon', () => {
|
|
|
|
const wrapper = mount(
|
|
|
|
<Collapse
|
|
|
|
expandIcon={() => (
|
|
|
|
<button type="button" className="custom-expandicon-classname">
|
|
|
|
action
|
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
<Collapse.Panel header="header" />
|
|
|
|
</Collapse>,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(wrapper.find('.custom-expandicon-classname').exists()).toBe(true);
|
|
|
|
});
|
|
|
|
|
2019-02-13 19:32:49 +08:00
|
|
|
it('should render extra node of panel', () => {
|
|
|
|
const wrapper = mount(
|
|
|
|
<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
|
|
|
);
|
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
|
|
|
});
|
2019-09-27 23:24:02 +08:00
|
|
|
|
2020-03-20 15:07:47 +08:00
|
|
|
it('could be expand and collapse', async () => {
|
2019-09-27 23:24:02 +08:00
|
|
|
const wrapper = mount(
|
|
|
|
<Collapse>
|
|
|
|
<Collapse.Panel header="This is panel header 1" key="1">
|
|
|
|
content
|
|
|
|
</Collapse.Panel>
|
|
|
|
</Collapse>,
|
|
|
|
);
|
|
|
|
expect(wrapper.find('.ant-collapse-item').hasClass('ant-collapse-item-active')).toBe(false);
|
2020-05-24 00:12:29 +08:00
|
|
|
wrapper.find('.ant-collapse-header').at(0).simulate('click');
|
|
|
|
wrapper.update();
|
2020-03-20 15:07:47 +08:00
|
|
|
await sleep(400);
|
2020-05-24 00:12:29 +08:00
|
|
|
wrapper.update();
|
2019-09-27 23:24:02 +08:00
|
|
|
expect(wrapper.find('.ant-collapse-item').hasClass('ant-collapse-item-active')).toBe(true);
|
|
|
|
});
|
2020-03-18 20:44:18 +08:00
|
|
|
|
2020-11-30 15:20:42 +08:00
|
|
|
it('could override default openMotion', () => {
|
2020-03-18 20:44:18 +08:00
|
|
|
const wrapper = mount(
|
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>,
|
|
|
|
);
|
2020-05-24 00:12:29 +08:00
|
|
|
wrapper.find('.ant-collapse-header').at(0).simulate('click');
|
2020-03-18 20:44:18 +08:00
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
|
|
|
});
|
2020-11-30 15:20:42 +08:00
|
|
|
|
|
|
|
it('should trigger warning and keep compatibility when using disabled in Panel', () => {
|
|
|
|
const wrapper = mount(
|
|
|
|
<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.',
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(wrapper.find('.ant-collapse-item-disabled').length).toBe(1);
|
|
|
|
|
|
|
|
wrapper.find('.ant-collapse-header').simulate('click');
|
|
|
|
expect(wrapper.find('.ant-collapse-item-active').length).toBe(0);
|
|
|
|
});
|
2021-05-18 13:02:17 +08:00
|
|
|
|
|
|
|
it('should end motion when set activeKey while hiding', async () => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
jest.spyOn(window, 'requestAnimationFrame').mockImplementation(cb => {
|
|
|
|
setTimeout(cb, 16.66);
|
|
|
|
});
|
|
|
|
|
|
|
|
let setActiveKeyOuter;
|
|
|
|
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>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const wrapper = mount(<Test />);
|
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
setActiveKeyOuter('1');
|
|
|
|
await Promise.resolve();
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(wrapper.render().find('.ant-motion-collapse').length).toBe(0);
|
|
|
|
|
|
|
|
window.requestAnimationFrame.mockRestore();
|
|
|
|
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
|
|
|
});
|