2017-02-26 16:48:42 +08:00
|
|
|
import React from 'react';
|
2021-08-31 15:51:02 +08:00
|
|
|
import { mount } from 'enzyme';
|
2017-03-28 13:51:58 +08:00
|
|
|
import KeyCode from 'rc-util/lib/KeyCode';
|
2017-02-26 16:48:42 +08:00
|
|
|
import Cascader from '..';
|
2020-01-02 19:10:16 +08:00
|
|
|
import ConfigProvider from '../../config-provider';
|
2021-08-31 15:51:02 +08:00
|
|
|
import excludeAllWarning from '../../../tests/shared/excludeWarning';
|
2017-11-19 01:41:40 +08:00
|
|
|
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';
|
2021-08-31 15:51:02 +08:00
|
|
|
|
|
|
|
function toggleOpen(wrapper) {
|
|
|
|
wrapper.find('.ant-select-selector').simulate('mousedown');
|
|
|
|
}
|
|
|
|
|
|
|
|
function isOpen(wrapper) {
|
|
|
|
return !!wrapper.find('Trigger').props().popupVisible;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getDropdown(wrapper) {
|
|
|
|
return wrapper.find('.ant-select-dropdown');
|
|
|
|
}
|
|
|
|
|
|
|
|
function clickOption(wrapper, menuIndex, itemIndex, type = 'click') {
|
|
|
|
const menu = wrapper.find('ul.ant-cascader-menu').at(menuIndex);
|
|
|
|
const itemList = menu.find('li.ant-cascader-menu-item');
|
|
|
|
|
|
|
|
itemList.at(itemIndex).simulate(type);
|
|
|
|
}
|
2017-02-26 16:48:42 +08:00
|
|
|
|
2018-12-07 16:17:45 +08:00
|
|
|
const options = [
|
|
|
|
{
|
|
|
|
value: 'zhejiang',
|
|
|
|
label: 'Zhejiang',
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
value: 'hangzhou',
|
|
|
|
label: 'Hangzhou',
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
value: 'xihu',
|
|
|
|
label: 'West Lake',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: 'jiangsu',
|
|
|
|
label: 'Jiangsu',
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
value: 'nanjing',
|
|
|
|
label: 'Nanjing',
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
value: 'zhonghuamen',
|
|
|
|
label: 'Zhong Hua Men',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
];
|
2017-02-26 16:48:42 +08:00
|
|
|
|
2018-05-25 19:31:34 +08:00
|
|
|
function filter(inputValue, path) {
|
2018-12-07 16:17:45 +08:00
|
|
|
return path.some(option => option.label.toLowerCase().indexOf(inputValue.toLowerCase()) > -1);
|
2018-05-25 19:31:34 +08:00
|
|
|
}
|
|
|
|
|
2017-02-26 16:48:42 +08:00
|
|
|
describe('Cascader', () => {
|
2021-08-31 15:51:02 +08:00
|
|
|
excludeAllWarning();
|
|
|
|
|
|
|
|
focusTest(Cascader, { refFocus: true });
|
2019-08-26 22:53:20 +08:00
|
|
|
mountTest(Cascader);
|
2020-01-02 19:10:16 +08:00
|
|
|
rtlTest(Cascader);
|
2017-11-19 01:41:40 +08:00
|
|
|
|
2017-02-26 16:48:42 +08:00
|
|
|
it('popup correctly when panel is hidden', () => {
|
2018-12-07 16:17:45 +08:00
|
|
|
const wrapper = mount(<Cascader options={options} />);
|
2021-08-31 15:51:02 +08:00
|
|
|
expect(isOpen(wrapper)).toBeFalsy();
|
2017-02-26 16:48:42 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('popup correctly when panel is open', () => {
|
2018-05-25 19:31:34 +08:00
|
|
|
const onPopupVisibleChange = jest.fn();
|
2017-02-26 16:48:42 +08:00
|
|
|
const wrapper = mount(
|
2018-12-07 16:17:45 +08:00
|
|
|
<Cascader options={options} onPopupVisibleChange={onPopupVisibleChange} />,
|
2017-02-26 16:48:42 +08:00
|
|
|
);
|
2021-08-31 15:51:02 +08:00
|
|
|
toggleOpen(wrapper);
|
|
|
|
expect(isOpen(wrapper)).toBeTruthy();
|
2018-05-25 19:31:34 +08:00
|
|
|
expect(onPopupVisibleChange).toHaveBeenCalledWith(true);
|
2017-02-26 16:48:42 +08:00
|
|
|
});
|
|
|
|
|
2018-03-23 19:19:29 +08:00
|
|
|
it('support controlled mode', () => {
|
2018-12-07 16:17:45 +08:00
|
|
|
const wrapper = mount(<Cascader options={options} />);
|
2018-03-23 19:19:29 +08:00
|
|
|
wrapper.setProps({
|
|
|
|
value: ['zhejiang', 'hangzhou', 'xihu'],
|
|
|
|
});
|
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
2017-02-26 16:48:42 +08:00
|
|
|
it('popup correctly with defaultValue', () => {
|
2018-12-07 16:17:45 +08:00
|
|
|
const wrapper = mount(<Cascader options={options} defaultValue={['zhejiang', 'hangzhou']} />);
|
2021-08-31 15:51:02 +08:00
|
|
|
toggleOpen(wrapper);
|
|
|
|
expect(getDropdown(wrapper).render()).toMatchSnapshot();
|
2017-02-26 16:48:42 +08:00
|
|
|
});
|
|
|
|
|
2018-05-25 19:31:34 +08:00
|
|
|
it('should support popupVisible', () => {
|
2018-12-07 16:17:45 +08:00
|
|
|
const wrapper = mount(<Cascader options={options} defaultValue={['zhejiang', 'hangzhou']} />);
|
2021-08-31 15:51:02 +08:00
|
|
|
expect(isOpen(wrapper)).toBeFalsy();
|
2018-05-25 19:31:34 +08:00
|
|
|
wrapper.setProps({ popupVisible: true });
|
2021-08-31 15:51:02 +08:00
|
|
|
expect(isOpen(wrapper)).toBeTruthy();
|
2018-05-25 19:31:34 +08:00
|
|
|
});
|
|
|
|
|
2017-02-26 16:48:42 +08:00
|
|
|
it('can be selected', () => {
|
2018-05-25 19:31:34 +08:00
|
|
|
const onChange = jest.fn();
|
|
|
|
const wrapper = mount(<Cascader options={options} onChange={onChange} />);
|
2021-08-31 15:51:02 +08:00
|
|
|
|
|
|
|
toggleOpen(wrapper);
|
|
|
|
expect(isOpen(wrapper)).toBeTruthy();
|
|
|
|
|
|
|
|
clickOption(wrapper, 0, 0);
|
|
|
|
expect(getDropdown(wrapper).render()).toMatchSnapshot();
|
|
|
|
|
|
|
|
clickOption(wrapper, 1, 0);
|
|
|
|
expect(getDropdown(wrapper).render()).toMatchSnapshot();
|
|
|
|
|
|
|
|
clickOption(wrapper, 2, 0);
|
|
|
|
expect(getDropdown(wrapper).render()).toMatchSnapshot();
|
|
|
|
|
|
|
|
expect(onChange).toHaveBeenCalledTimes(1);
|
2018-05-25 19:31:34 +08:00
|
|
|
expect(onChange).toHaveBeenCalledWith(['zhejiang', 'hangzhou', 'xihu'], expect.anything());
|
2017-02-26 16:48:42 +08:00
|
|
|
});
|
2017-03-28 13:51:58 +08:00
|
|
|
|
|
|
|
it('backspace should work with `Cascader[showSearch]`', () => {
|
|
|
|
const wrapper = mount(<Cascader options={options} showSearch />);
|
|
|
|
wrapper.find('input').simulate('change', { target: { value: '123' } });
|
2021-08-31 15:51:02 +08:00
|
|
|
expect(isOpen(wrapper)).toBeTruthy();
|
|
|
|
|
|
|
|
wrapper.find('input').simulate('keydown', { which: KeyCode.BACKSPACE });
|
|
|
|
expect(isOpen(wrapper)).toBeTruthy();
|
|
|
|
|
|
|
|
wrapper.find('input').simulate('change', { target: { value: '' } });
|
|
|
|
expect(isOpen(wrapper)).toBeTruthy();
|
|
|
|
|
|
|
|
wrapper.find('input').simulate('keydown', { which: KeyCode.BACKSPACE });
|
|
|
|
expect(isOpen(wrapper)).toBeFalsy();
|
2017-03-28 13:51:58 +08:00
|
|
|
});
|
2018-05-25 19:31:34 +08:00
|
|
|
|
|
|
|
it('should highlight keyword and filter when search in Cascader', () => {
|
|
|
|
const wrapper = mount(<Cascader options={options} showSearch={{ filter }} />);
|
|
|
|
wrapper.find('input').simulate('change', { target: { value: 'z' } });
|
2021-08-31 15:51:02 +08:00
|
|
|
expect(getDropdown(wrapper).render()).toMatchSnapshot();
|
2018-05-25 19:31:34 +08:00
|
|
|
});
|
|
|
|
|
2020-01-07 13:41:41 +08:00
|
|
|
it('should highlight keyword and filter when search in Cascader with same field name of label and value', () => {
|
|
|
|
const customOptions = [
|
|
|
|
{
|
|
|
|
name: 'Zhejiang',
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
name: 'Hangzhou',
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
name: 'West Lake',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Xia Sha',
|
|
|
|
disabled: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
];
|
|
|
|
function customFilter(inputValue, path) {
|
|
|
|
return path.some(option => option.name.toLowerCase().indexOf(inputValue.toLowerCase()) > -1);
|
|
|
|
}
|
|
|
|
const wrapper = mount(
|
|
|
|
<Cascader
|
|
|
|
options={customOptions}
|
|
|
|
fieldNames={{ label: 'name', value: 'name' }}
|
|
|
|
showSearch={{ filter: customFilter }}
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
wrapper.find('input').simulate('change', { target: { value: 'z' } });
|
2021-08-31 15:51:02 +08:00
|
|
|
expect(getDropdown(wrapper).render()).toMatchSnapshot();
|
2020-01-07 13:41:41 +08:00
|
|
|
});
|
|
|
|
|
2018-05-25 19:31:34 +08:00
|
|
|
it('should render not found content', () => {
|
|
|
|
const wrapper = mount(<Cascader options={options} showSearch={{ filter }} />);
|
|
|
|
wrapper.find('input').simulate('change', { target: { value: '__notfoundkeyword__' } });
|
2021-08-31 15:51:02 +08:00
|
|
|
expect(getDropdown(wrapper).render()).toMatchSnapshot();
|
2018-05-25 19:31:34 +08:00
|
|
|
});
|
|
|
|
|
2021-08-31 15:51:02 +08:00
|
|
|
it('should support to clear selection', () => {
|
2018-05-25 19:31:34 +08:00
|
|
|
const wrapper = mount(<Cascader options={options} defaultValue={['zhejiang', 'hangzhou']} />);
|
2021-08-31 15:51:02 +08:00
|
|
|
expect(wrapper.find('.ant-select-selection-item').text()).toEqual('Zhejiang / Hangzhou');
|
|
|
|
wrapper.find('.ant-select-clear').at(0).simulate('mouseDown');
|
|
|
|
expect(wrapper.exists('.ant-select-selection-item')).toBeFalsy();
|
2018-05-25 19:31:34 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should clear search input when clear selection', () => {
|
|
|
|
const wrapper = mount(
|
2018-12-07 16:17:45 +08:00
|
|
|
<Cascader options={options} defaultValue={['zhejiang', 'hangzhou']} showSearch />,
|
2018-05-25 19:31:34 +08:00
|
|
|
);
|
|
|
|
wrapper.find('input').simulate('change', { target: { value: 'xxx' } });
|
|
|
|
|
2021-08-31 15:51:02 +08:00
|
|
|
wrapper.find('.ant-select-clear').at(0).simulate('mouseDown');
|
|
|
|
expect(wrapper.find('input').props().value).toEqual('');
|
2018-05-25 19:31:34 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should change filtered item when options are changed', () => {
|
|
|
|
const wrapper = mount(<Cascader options={options} showSearch={{ filter }} />);
|
|
|
|
wrapper.find('input').simulate('change', { target: { value: 'a' } });
|
|
|
|
expect(wrapper.find('.ant-cascader-menu-item').length).toBe(2);
|
|
|
|
wrapper.setProps({ options: [options[0]] });
|
|
|
|
expect(wrapper.find('.ant-cascader-menu-item').length).toBe(1);
|
|
|
|
});
|
2018-07-28 13:29:39 +08:00
|
|
|
|
2020-03-19 15:31:14 +08:00
|
|
|
it('should select item immediately when searching and pressing down arrow key', () => {
|
|
|
|
const wrapper = mount(<Cascader options={options} showSearch={{ filter }} />);
|
|
|
|
wrapper.find('input').simulate('change', { target: { value: 'a' } });
|
|
|
|
expect(wrapper.find('.ant-cascader-menu-item').length).toBe(2);
|
|
|
|
expect(wrapper.find('.ant-cascader-menu-item-active').length).toBe(0);
|
2021-08-31 15:51:02 +08:00
|
|
|
|
2020-03-19 15:31:14 +08:00
|
|
|
wrapper.find('input').simulate('keyDown', {
|
2021-08-31 15:51:02 +08:00
|
|
|
which: KeyCode.DOWN,
|
2020-03-19 15:31:14 +08:00
|
|
|
});
|
|
|
|
expect(wrapper.find('.ant-cascader-menu-item-active').length).toBe(1);
|
|
|
|
});
|
|
|
|
|
2018-07-28 13:29:39 +08:00
|
|
|
it('can use fieldNames', () => {
|
2018-12-07 16:17:45 +08:00
|
|
|
const customerOptions = [
|
|
|
|
{
|
|
|
|
code: 'zhejiang',
|
|
|
|
name: 'Zhejiang',
|
|
|
|
items: [
|
|
|
|
{
|
|
|
|
code: 'hangzhou',
|
|
|
|
name: 'Hangzhou',
|
|
|
|
items: [
|
|
|
|
{
|
|
|
|
code: 'xihu',
|
|
|
|
name: 'West Lake',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
code: 'jiangsu',
|
|
|
|
name: 'Jiangsu',
|
|
|
|
items: [
|
|
|
|
{
|
|
|
|
code: 'nanjing',
|
|
|
|
name: 'Nanjing',
|
|
|
|
items: [
|
|
|
|
{
|
|
|
|
code: 'zhonghuamen',
|
|
|
|
name: 'Zhong Hua Men',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
];
|
2021-08-31 15:51:02 +08:00
|
|
|
|
|
|
|
const onChange = jest.fn();
|
|
|
|
|
2018-11-06 12:36:03 +08:00
|
|
|
const wrapper = mount(
|
|
|
|
<Cascader
|
|
|
|
options={customerOptions}
|
2021-08-31 15:51:02 +08:00
|
|
|
onChange={onChange}
|
2018-11-06 12:36:03 +08:00
|
|
|
fieldNames={{
|
|
|
|
children: 'items',
|
|
|
|
label: 'name',
|
|
|
|
value: 'code',
|
|
|
|
}}
|
2021-08-31 15:51:02 +08:00
|
|
|
open
|
2018-12-07 16:17:45 +08:00
|
|
|
/>,
|
2018-11-06 12:36:03 +08:00
|
|
|
);
|
2021-08-31 15:51:02 +08:00
|
|
|
|
|
|
|
clickOption(wrapper, 0, 0);
|
|
|
|
clickOption(wrapper, 1, 0);
|
|
|
|
clickOption(wrapper, 2, 0);
|
|
|
|
expect(wrapper.find('.ant-select-selection-item').text()).toEqual(
|
|
|
|
'Zhejiang / Hangzhou / West Lake',
|
|
|
|
);
|
|
|
|
expect(onChange).toHaveBeenCalledWith(['zhejiang', 'hangzhou', 'xihu'], expect.anything());
|
2018-11-06 12:36:03 +08:00
|
|
|
});
|
|
|
|
|
2019-07-09 16:29:42 +08:00
|
|
|
it('should show not found content when options.length is 0', () => {
|
|
|
|
const customerOptions = [];
|
|
|
|
const wrapper = mount(<Cascader options={customerOptions} />);
|
2021-08-31 15:51:02 +08:00
|
|
|
toggleOpen(wrapper);
|
|
|
|
expect(getDropdown(wrapper).render()).toMatchSnapshot();
|
2019-07-09 16:29:42 +08:00
|
|
|
});
|
|
|
|
|
2021-08-31 15:51:02 +08:00
|
|
|
it('not found content should be disabled', () => {
|
|
|
|
const wrapper = mount(<Cascader options={[]} open />);
|
2020-11-30 11:31:22 +08:00
|
|
|
expect(wrapper.find('.ant-cascader-menu-item-disabled').length).toBe(1);
|
|
|
|
});
|
|
|
|
|
2018-11-26 11:29:16 +08:00
|
|
|
describe('limit filtered item count', () => {
|
|
|
|
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
errorSpy.mockRestore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('limit with positive number', () => {
|
|
|
|
const wrapper = mount(<Cascader options={options} showSearch={{ filter, limit: 1 }} />);
|
|
|
|
wrapper.find('input').simulate('change', { target: { value: 'a' } });
|
2021-08-31 15:51:02 +08:00
|
|
|
expect(wrapper.find('.ant-cascader-menu-item')).toHaveLength(1);
|
2018-11-26 11:29:16 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('not limit', () => {
|
|
|
|
const wrapper = mount(<Cascader options={options} showSearch={{ filter, limit: false }} />);
|
|
|
|
wrapper.find('input').simulate('change', { target: { value: 'a' } });
|
2021-08-31 15:51:02 +08:00
|
|
|
expect(wrapper.find('.ant-cascader-menu-item')).toHaveLength(2);
|
2018-11-26 11:29:16 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('negative limit', () => {
|
|
|
|
const wrapper = mount(<Cascader options={options} showSearch={{ filter, limit: -1 }} />);
|
|
|
|
wrapper.find('input').simulate('click');
|
|
|
|
wrapper.find('input').simulate('change', { target: { value: 'a' } });
|
2021-08-31 15:51:02 +08:00
|
|
|
expect(wrapper.find('.ant-cascader-menu-item')).toHaveLength(2);
|
2018-11-26 11:29:16 +08:00
|
|
|
});
|
|
|
|
});
|
2019-07-08 10:57:34 +08:00
|
|
|
|
2021-08-31 15:51:02 +08:00
|
|
|
// FIXME: Move to `rc-tree-select` instead
|
2021-10-12 13:14:26 +08:00
|
|
|
// eslint-disable-next-line jest/no-disabled-tests
|
|
|
|
it.skip('should warning if not find `value` in `options`', () => {
|
|
|
|
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
|
|
|
mount(<Cascader options={[{ label: 'a', value: 'a', children: [{ label: 'b' }] }]} />);
|
|
|
|
expect(errorSpy).toHaveBeenCalledWith(
|
|
|
|
'Warning: [antd: Cascader] Not found `value` in `options`.',
|
|
|
|
);
|
|
|
|
errorSpy.mockRestore();
|
|
|
|
});
|
2019-07-20 12:20:00 +08:00
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/17690
|
|
|
|
it('should not breaks when children is null', () => {
|
|
|
|
const optionsWithChildrenNull = [
|
|
|
|
{
|
|
|
|
value: 'zhejiang',
|
|
|
|
label: 'Zhejiang',
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
value: 'hangzhou',
|
|
|
|
label: 'Hangzhou',
|
|
|
|
children: null,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
];
|
|
|
|
expect(() => {
|
|
|
|
mount(<Cascader options={optionsWithChildrenNull} />);
|
|
|
|
}).not.toThrow();
|
|
|
|
});
|
2019-08-17 22:29:02 +08:00
|
|
|
|
2019-10-15 19:23:26 +08:00
|
|
|
it('placeholder works correctly', () => {
|
|
|
|
const wrapper = mount(<Cascader options={[]} />);
|
2021-08-31 15:51:02 +08:00
|
|
|
expect(wrapper.find('.ant-select-selection-placeholder').text()).toEqual('');
|
2019-10-15 19:23:26 +08:00
|
|
|
|
|
|
|
const customPlaceholder = 'Custom placeholder';
|
|
|
|
wrapper.setProps({
|
|
|
|
placeholder: customPlaceholder,
|
|
|
|
});
|
2021-08-31 15:51:02 +08:00
|
|
|
expect(wrapper.find('.ant-select-selection-placeholder').text()).toEqual(customPlaceholder);
|
2019-10-15 19:23:26 +08:00
|
|
|
});
|
2020-03-20 15:48:44 +08:00
|
|
|
|
2020-01-02 19:10:16 +08:00
|
|
|
it('popup correctly with defaultValue RTL', () => {
|
|
|
|
const wrapper = mount(
|
|
|
|
<ConfigProvider direction="rtl">
|
2021-08-31 15:51:02 +08:00
|
|
|
<Cascader options={options} defaultValue={['zhejiang', 'hangzhou']} open />
|
2020-01-02 19:10:16 +08:00
|
|
|
</ConfigProvider>,
|
|
|
|
);
|
2021-08-31 15:51:02 +08:00
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
2020-01-02 19:10:16 +08:00
|
|
|
});
|
2020-03-20 15:48:44 +08:00
|
|
|
|
2020-01-02 19:10:16 +08:00
|
|
|
it('can be selected in RTL direction', () => {
|
|
|
|
const options2 = [
|
|
|
|
{
|
|
|
|
value: 'zhejiang',
|
|
|
|
label: 'Zhejiang',
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
value: 'hangzhou',
|
|
|
|
label: 'Hangzhou',
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
value: 'xihu',
|
|
|
|
label: 'West Lake',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: 'jiangsu',
|
|
|
|
label: 'Jiangsu',
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
value: 'nanjing',
|
|
|
|
label: 'Nanjing',
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
value: 'zhonghuamen',
|
|
|
|
label: 'Zhong Hua Men',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
];
|
|
|
|
const onChange = jest.fn();
|
|
|
|
const wrapper = mount(
|
|
|
|
<ConfigProvider direction="rtl">
|
|
|
|
<Cascader
|
|
|
|
options={options2}
|
|
|
|
defaultValue={['zhejiang', 'hangzhou']}
|
|
|
|
onChange={onChange}
|
|
|
|
popupPlacement="bottomRight"
|
|
|
|
/>
|
|
|
|
</ConfigProvider>,
|
|
|
|
);
|
|
|
|
|
2021-08-31 15:51:02 +08:00
|
|
|
toggleOpen(wrapper);
|
|
|
|
clickOption(wrapper, 0, 0);
|
|
|
|
expect(getDropdown(wrapper).render()).toMatchSnapshot();
|
|
|
|
|
|
|
|
toggleOpen(wrapper);
|
|
|
|
clickOption(wrapper, 1, 0);
|
|
|
|
expect(getDropdown(wrapper).render()).toMatchSnapshot();
|
|
|
|
|
|
|
|
toggleOpen(wrapper);
|
|
|
|
clickOption(wrapper, 2, 0);
|
|
|
|
expect(getDropdown(wrapper).render()).toMatchSnapshot();
|
|
|
|
|
|
|
|
expect(onChange).toHaveBeenCalledTimes(1);
|
2020-01-02 19:10:16 +08:00
|
|
|
expect(onChange).toHaveBeenCalledWith(['zhejiang', 'hangzhou', 'xihu'], expect.anything());
|
|
|
|
});
|
2020-05-12 10:34:24 +08:00
|
|
|
|
|
|
|
it('defaultValue works correctly when no match options', () => {
|
|
|
|
const wrapper = mount(<Cascader options={options} defaultValue={['options1', 'options2']} />);
|
2021-08-31 15:51:02 +08:00
|
|
|
expect(wrapper.find('.ant-select-selection-item').text()).toEqual('options1 / options2');
|
2020-05-12 10:34:24 +08:00
|
|
|
});
|
2020-05-24 23:52:25 +08:00
|
|
|
|
|
|
|
it('can be selected when showSearch', () => {
|
|
|
|
const onChange = jest.fn();
|
|
|
|
const wrapper = mount(<Cascader options={options} onChange={onChange} showSearch />);
|
|
|
|
wrapper.find('input').simulate('change', { target: { value: 'Zh' } });
|
2021-08-31 15:51:02 +08:00
|
|
|
expect(wrapper.find('.ant-cascader-menu').length).toBe(1);
|
|
|
|
clickOption(wrapper, 0, 0);
|
2020-05-24 23:52:25 +08:00
|
|
|
expect(onChange).toHaveBeenCalledWith(['zhejiang', 'hangzhou', 'xihu'], expect.anything());
|
|
|
|
});
|
2020-08-19 10:43:54 +08:00
|
|
|
|
|
|
|
it('options should open after press esc and then search', () => {
|
|
|
|
const wrapper = mount(<Cascader options={options} showSearch />);
|
|
|
|
wrapper.find('input').simulate('change', { target: { value: 'jin' } });
|
2021-08-31 15:51:02 +08:00
|
|
|
expect(isOpen(wrapper)).toBeTruthy();
|
|
|
|
wrapper.find('input').simulate('keydown', { which: KeyCode.ESC });
|
|
|
|
expect(isOpen(wrapper)).toBeFalsy();
|
2020-08-19 10:43:54 +08:00
|
|
|
wrapper.find('input').simulate('change', { target: { value: 'jin' } });
|
2021-08-31 15:51:02 +08:00
|
|
|
expect(isOpen(wrapper)).toBeTruthy();
|
2020-08-19 10:43:54 +08:00
|
|
|
});
|
2020-09-04 10:59:10 +08:00
|
|
|
|
|
|
|
it('onChange works correctly when the label of fieldNames is the same as value', () => {
|
|
|
|
const onChange = jest.fn();
|
2021-08-31 15:51:02 +08:00
|
|
|
const sameNames = { label: 'label', value: 'label' };
|
2020-09-04 10:59:10 +08:00
|
|
|
const wrapper = mount(
|
|
|
|
<Cascader options={options} onChange={onChange} showSearch fieldNames={sameNames} />,
|
|
|
|
);
|
|
|
|
wrapper.find('input').simulate('change', { target: { value: 'est' } });
|
2021-08-31 15:51:02 +08:00
|
|
|
clickOption(wrapper, 0, 0);
|
2020-09-04 10:59:10 +08:00
|
|
|
expect(onChange).toHaveBeenCalledWith(['Zhejiang', 'Hangzhou', 'West Lake'], expect.anything());
|
|
|
|
});
|
2021-09-13 15:59:57 +08:00
|
|
|
|
2022-01-21 11:49:07 +08:00
|
|
|
it('rtl should work well with placement', () => {
|
|
|
|
const wrapper = mount(<Cascader options={options} direction="rtl" />);
|
|
|
|
|
|
|
|
expect(wrapper.find('Trigger').prop('popupPlacement')).toEqual('bottomRight');
|
2020-09-04 10:59:10 +08:00
|
|
|
});
|
2021-09-13 15:59:57 +08:00
|
|
|
|
2021-11-30 17:26:51 +08:00
|
|
|
describe('legacy props', () => {
|
|
|
|
it('popupClassName', () => {
|
|
|
|
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
2022-01-30 15:21:41 +08:00
|
|
|
const wrapper = mount(<Cascader open popupPlacement="topRight" popupClassName="mock-cls" />);
|
2021-09-13 15:59:57 +08:00
|
|
|
|
2021-11-30 17:26:51 +08:00
|
|
|
expect(wrapper.exists('.mock-cls')).toBeTruthy();
|
2022-01-30 15:21:41 +08:00
|
|
|
expect(wrapper.find('Trigger').prop('popupPlacement')).toEqual('topRight');
|
2021-09-13 15:59:57 +08:00
|
|
|
|
2021-11-30 17:26:51 +08:00
|
|
|
expect(errorSpy).toHaveBeenCalledWith(
|
|
|
|
'Warning: [antd: Cascader] `popupClassName` is deprecated. Please use `dropdownClassName` instead.',
|
|
|
|
);
|
2021-09-13 15:59:57 +08:00
|
|
|
|
2021-11-30 17:26:51 +08:00
|
|
|
errorSpy.mockRestore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('displayRender & multiple', () => {
|
|
|
|
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
|
|
|
mount(<Cascader multiple displayRender={() => null} />);
|
|
|
|
|
|
|
|
expect(errorSpy).toHaveBeenCalledWith(
|
|
|
|
'Warning: [antd: Cascader] `displayRender` not work on `multiple`. Please use `tagRender` instead.',
|
|
|
|
);
|
|
|
|
|
|
|
|
errorSpy.mockRestore();
|
|
|
|
});
|
2021-09-13 15:59:57 +08:00
|
|
|
});
|
2017-02-26 16:48:42 +08:00
|
|
|
});
|