2018-01-04 20:43:56 +08:00
|
|
|
import React from 'react';
|
2017-11-19 01:41:40 +08:00
|
|
|
import Select from '..';
|
|
|
|
import focusTest from '../../../tests/shared/focusTest';
|
2019-08-26 22:53:20 +08:00
|
|
|
import mountTest from '../../../tests/shared/mountTest';
|
2020-01-02 19:10:16 +08:00
|
|
|
import rtlTest from '../../../tests/shared/rtlTest';
|
2022-08-08 13:31:55 +08:00
|
|
|
import { fireEvent, render, act } from '../../../tests/utils';
|
2022-05-23 14:37:16 +08:00
|
|
|
import Icon from '../../icon';
|
2017-11-19 01:41:40 +08:00
|
|
|
|
2018-08-25 23:41:30 +08:00
|
|
|
const { Option } = Select;
|
|
|
|
|
2017-11-19 01:41:40 +08:00
|
|
|
describe('Select', () => {
|
2020-10-24 20:33:18 +08:00
|
|
|
focusTest(Select, { refFocus: true });
|
2019-08-26 22:53:20 +08:00
|
|
|
mountTest(Select);
|
2020-01-02 19:10:16 +08:00
|
|
|
rtlTest(Select);
|
2018-01-04 20:43:56 +08:00
|
|
|
|
2022-05-19 16:27:24 +08:00
|
|
|
function toggleOpen(container) {
|
2022-08-08 13:31:55 +08:00
|
|
|
fireEvent.mouseDown(container.querySelector('.ant-select-selector'));
|
2019-09-12 20:15:17 +08:00
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-01-04 20:43:56 +08:00
|
|
|
beforeEach(() => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should have default notFoundContent', () => {
|
2022-05-19 16:27:24 +08:00
|
|
|
const { container } = render(<Select mode="multiple" />);
|
|
|
|
toggleOpen(container);
|
|
|
|
expect(container.querySelectorAll('.ant-select-item-option').length).toBe(0);
|
|
|
|
expect(container.querySelectorAll('.ant-empty').length).toBeTruthy();
|
2018-01-04 20:43:56 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should support set notFoundContent to null', () => {
|
2022-05-19 16:27:24 +08:00
|
|
|
const { container } = render(<Select mode="multiple" notFoundContent={null} />);
|
|
|
|
toggleOpen(container);
|
|
|
|
expect(container.querySelectorAll('.ant-empty').length).toBe(0);
|
2018-01-04 20:43:56 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should not have default notFoundContent when mode is combobox', () => {
|
2022-05-19 16:27:24 +08:00
|
|
|
const { container } = render(<Select mode={Select.SECRET_COMBOBOX_MODE_DO_NOT_USE} />);
|
|
|
|
toggleOpen(container);
|
|
|
|
expect(container.querySelector('.ant-empty')).toBeFalsy();
|
2018-01-04 20:43:56 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should not have notFoundContent when mode is combobox and notFoundContent is set', () => {
|
2022-05-19 16:27:24 +08:00
|
|
|
const { container } = render(
|
2018-12-07 20:02:01 +08:00
|
|
|
<Select mode={Select.SECRET_COMBOBOX_MODE_DO_NOT_USE} notFoundContent="not at all" />,
|
2018-01-04 20:43:56 +08:00
|
|
|
);
|
2022-05-19 16:27:24 +08:00
|
|
|
toggleOpen(container);
|
|
|
|
expect(container.querySelector('.ant-select-item-option')).toBeFalsy();
|
|
|
|
expect(container.querySelector('.ant-select-item-empty')).toHaveTextContent('not at all');
|
2018-01-04 20:43:56 +08:00
|
|
|
});
|
2018-08-25 23:41:30 +08:00
|
|
|
|
|
|
|
it('should be controlled by open prop', () => {
|
|
|
|
const onDropdownVisibleChange = jest.fn();
|
2022-05-20 12:18:27 +08:00
|
|
|
const TestComponent = () => {
|
|
|
|
const [open, setOpen] = React.useState(false);
|
|
|
|
const handleChange = value => {
|
|
|
|
onDropdownVisibleChange(value);
|
|
|
|
setOpen(value);
|
|
|
|
};
|
|
|
|
return (
|
|
|
|
<Select open={open} onDropdownVisibleChange={handleChange}>
|
|
|
|
<Option value="1">1</Option>
|
|
|
|
</Select>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
const { container } = render(<TestComponent />);
|
|
|
|
expect(container.querySelector('.ant-select-dropdown')).toBeFalsy();
|
2022-05-19 16:27:24 +08:00
|
|
|
toggleOpen(container);
|
|
|
|
expect(container.querySelectorAll('.ant-select-dropdown').length).toBe(1);
|
2018-08-25 23:41:30 +08:00
|
|
|
expect(onDropdownVisibleChange).toHaveBeenLastCalledWith(true);
|
|
|
|
});
|
2018-11-12 12:18:21 +08:00
|
|
|
|
2020-06-11 18:01:45 +08:00
|
|
|
it('should show search icon when showSearch and open', () => {
|
2022-05-19 16:27:24 +08:00
|
|
|
jest.useFakeTimers();
|
|
|
|
const { container } = render(
|
2020-06-11 18:01:45 +08:00
|
|
|
<Select showSearch>
|
|
|
|
<Option value="1">1</Option>
|
|
|
|
</Select>,
|
|
|
|
);
|
2022-05-19 16:27:24 +08:00
|
|
|
expect(container.querySelectorAll('.anticon-down').length).toBe(1);
|
|
|
|
expect(container.querySelectorAll('.anticon-search').length).toBe(0);
|
|
|
|
toggleOpen(container);
|
|
|
|
expect(container.querySelectorAll('.anticon-down').length).toBe(0);
|
|
|
|
expect(container.querySelectorAll('.anticon-search').length).toBe(1);
|
2020-06-11 18:01:45 +08:00
|
|
|
});
|
2022-08-05 11:21:07 +08:00
|
|
|
|
|
|
|
it('should show warning when use dropdownClassName', () => {
|
|
|
|
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
|
|
|
render(<Select dropdownClassName="myCustomClassName" />);
|
|
|
|
expect(errorSpy).toHaveBeenCalledWith(
|
|
|
|
'Warning: [antd: Select] `dropdownClassName` is deprecated which will be removed in next major version. Please use `popupClassName` instead.',
|
|
|
|
);
|
|
|
|
errorSpy.mockRestore();
|
|
|
|
});
|
|
|
|
|
2022-05-19 16:27:24 +08:00
|
|
|
//
|
2018-11-12 12:18:21 +08:00
|
|
|
describe('Select Custom Icons', () => {
|
|
|
|
it('should support customized icons', () => {
|
2022-05-19 16:27:24 +08:00
|
|
|
const { rerender, asFragment } = render(
|
|
|
|
<Select
|
|
|
|
removeIcon={<Icon type="close" />}
|
|
|
|
clearIcon={<Icon type="close" />}
|
|
|
|
menuItemSelectedIcon={<Icon type="close" />}
|
|
|
|
>
|
|
|
|
<Option value="1">1</Option>
|
|
|
|
</Select>,
|
|
|
|
);
|
|
|
|
rerender(
|
2018-11-12 12:18:21 +08:00
|
|
|
<Select
|
2022-05-19 16:27:24 +08:00
|
|
|
count={10}
|
2018-11-12 12:18:21 +08:00
|
|
|
removeIcon={<Icon type="close" />}
|
|
|
|
clearIcon={<Icon type="close" />}
|
|
|
|
menuItemSelectedIcon={<Icon type="close" />}
|
|
|
|
>
|
|
|
|
<Option value="1">1</Option>
|
2018-12-07 20:02:01 +08:00
|
|
|
</Select>,
|
2018-11-12 12:18:21 +08:00
|
|
|
);
|
2022-08-08 13:31:55 +08:00
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
2022-05-19 16:27:24 +08:00
|
|
|
expect(asFragment().firstChild).toMatchSnapshot();
|
2018-11-12 12:18:21 +08:00
|
|
|
});
|
|
|
|
});
|
2020-06-11 18:01:45 +08:00
|
|
|
|
|
|
|
describe('Deprecated', () => {
|
|
|
|
it('should ignore mode="combobox"', () => {
|
2022-05-19 16:27:24 +08:00
|
|
|
const { asFragment } = render(
|
2020-06-11 18:01:45 +08:00
|
|
|
<Select mode="combobox">
|
|
|
|
<Option value="1">1</Option>
|
|
|
|
</Select>,
|
|
|
|
);
|
2022-05-19 16:27:24 +08:00
|
|
|
expect(asFragment().firstChild).toMatchSnapshot();
|
2020-06-11 18:01:45 +08:00
|
|
|
});
|
|
|
|
});
|
2017-11-19 01:41:40 +08:00
|
|
|
});
|