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

134 lines
3.7 KiB
JavaScript
Raw Normal View History

import React from 'react';
import { mount } from 'enzyme';
import Select from '..';
2018-11-15 14:43:54 +08:00
import Icon from '../../icon';
import focusTest from '../../../tests/shared/focusTest';
const { Option } = Select;
describe('Select', () => {
focusTest(Select);
beforeEach(() => {
jest.useFakeTimers();
});
afterEach(() => {
jest.useRealTimers();
});
it('should have default notFoundContent', () => {
2018-12-07 20:02:01 +08:00
const wrapper = mount(<Select mode="multiple" />);
wrapper.find('.ant-select').simulate('click');
jest.runAllTimers();
2018-12-07 20:02:01 +08:00
const dropdownWrapper = mount(
wrapper
.find('Trigger')
.instance()
.getComponent(),
);
expect(dropdownWrapper.find('MenuItem').length).toBe(1);
2018-12-07 20:02:01 +08:00
expect(
dropdownWrapper
.find('MenuItem')
.at(0)
.text(),
).toBe('Not Found');
});
it('should support set notFoundContent to null', () => {
2018-12-07 20:02:01 +08:00
const wrapper = mount(<Select mode="multiple" notFoundContent={null} />);
wrapper.find('.ant-select').simulate('click');
jest.runAllTimers();
2018-12-07 20:02:01 +08:00
const dropdownWrapper = mount(
wrapper
.find('Trigger')
.instance()
.getComponent(),
);
expect(dropdownWrapper.find('MenuItem').length).toBe(0);
});
it('should not have default notFoundContent when mode is combobox', () => {
2018-12-07 20:02:01 +08:00
const wrapper = mount(<Select mode={Select.SECRET_COMBOBOX_MODE_DO_NOT_USE} />);
wrapper.find('.ant-select').simulate('click');
jest.runAllTimers();
2018-12-07 20:02:01 +08:00
const dropdownWrapper = mount(
wrapper
.find('Trigger')
.instance()
.getComponent(),
);
expect(dropdownWrapper.find('MenuItem').length).toBe(0);
});
it('should not have notFoundContent when mode is combobox and notFoundContent is set', () => {
const wrapper = mount(
2018-12-07 20:02:01 +08:00
<Select mode={Select.SECRET_COMBOBOX_MODE_DO_NOT_USE} notFoundContent="not at all" />,
);
wrapper.find('.ant-select').simulate('click');
jest.runAllTimers();
2018-12-07 20:02:01 +08:00
const dropdownWrapper = mount(
wrapper
.find('Trigger')
.instance()
.getComponent(),
);
expect(dropdownWrapper.find('MenuItem').length).toBe(1);
2018-12-07 20:02:01 +08:00
expect(
dropdownWrapper
.find('MenuItem')
.at(0)
.text(),
).toBe('not at all');
});
it('should be controlled by open prop', () => {
const onDropdownVisibleChange = jest.fn();
const wrapper = mount(
<Select open onDropdownVisibleChange={onDropdownVisibleChange}>
<Option value="1">1</Option>
2018-12-07 20:02:01 +08:00
</Select>,
);
let dropdownWrapper = mount(
wrapper
.find('Trigger')
.instance()
.getComponent(),
);
expect(dropdownWrapper.props().visible).toBe(true);
wrapper.find('.ant-select').simulate('click');
expect(onDropdownVisibleChange).toHaveBeenLastCalledWith(false);
expect(dropdownWrapper.props().visible).toBe(true);
wrapper.setProps({ open: false });
2018-12-07 20:02:01 +08:00
dropdownWrapper = mount(
wrapper
.find('Trigger')
.instance()
.getComponent(),
);
expect(dropdownWrapper.props().visible).toBe(false);
wrapper.find('.ant-select').simulate('click');
expect(onDropdownVisibleChange).toHaveBeenLastCalledWith(true);
expect(dropdownWrapper.props().visible).toBe(false);
});
2018-11-12 12:18:21 +08:00
describe('Select Custom Icons', () => {
it('should support customized icons', () => {
const wrapper = mount(
<Select
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
);
wrapper.setProps({ count: 10 });
jest.runAllTimers();
2018-11-20 17:26:08 +08:00
expect(wrapper.render()).toMatchSnapshot();
2018-11-12 12:18:21 +08:00
});
});
});