ant-design/components/collapse/__tests__/index.test.js

168 lines
4.8 KiB
JavaScript
Raw Normal View History

import { mount } from 'enzyme';
2022-06-22 14:57:09 +08:00
import React from 'react';
import { act } from 'react-dom/test-utils';
2022-06-22 14:57:09 +08:00
import { render, sleep } from '../../../tests/utils';
import { resetWarned } from '../../_util/warning';
describe('Collapse', () => {
// eslint-disable-next-line global-require
const Collapse = require('..').default;
2019-08-26 22:53:20 +08:00
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
beforeEach(() => {
resetWarned();
});
afterEach(() => {
errorSpy.mockReset();
});
afterAll(() => {
errorSpy.mockRestore();
});
it('should support remove expandIcon', () => {
const wrapper = mount(
<Collapse expandIcon={() => null}>
<Collapse.Panel header="header" />
2019-03-06 13:45:40 +08:00
</Collapse>,
);
expect(wrapper.render()).toMatchSnapshot();
});
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);
});
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>,
);
expect(wrapper.render()).toMatchSnapshot();
});
it('could be expand and collapse', async () => {
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);
wrapper.find('.ant-collapse-header').at(0).simulate('click');
wrapper.update();
await sleep(400);
wrapper.update();
expect(wrapper.find('.ant-collapse-item').hasClass('ant-collapse-item-active')).toBe(true);
});
it('could override default openMotion', () => {
const wrapper = mount(
<Collapse openMotion={{}}>
<Collapse.Panel header="This is panel header 1" key="1">
content
</Collapse.Panel>
</Collapse>,
);
wrapper.find('.ant-collapse-header').at(0).simulate('click');
expect(wrapper.render()).toMatchSnapshot();
});
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);
});
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();
});
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();
});
});
});
});