ant-design-vue/components/cascader/__tests__/index.test.js

306 lines
7.9 KiB
JavaScript
Raw Normal View History

2019-01-12 03:33:27 +00:00
import { asyncExpect } from '@/tests/utils';
import { mount } from '@vue/test-utils';
import KeyCode from '../../_util/KeyCode';
import Cascader from '..';
import focusTest from '../../../tests/shared/focusTest';
function $$(className) {
return document.body.querySelectorAll(className);
2019-01-03 12:47:39 +00:00
}
2019-01-12 03:33:27 +00: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',
},
],
},
],
},
];
function filter(inputValue, path) {
return path.some(option => option.label.toLowerCase().indexOf(inputValue.toLowerCase()) > -1);
2019-01-03 12:47:39 +00:00
}
2018-06-10 12:42:59 +00:00
describe('Cascader', () => {
2019-01-12 03:33:27 +00:00
focusTest(Cascader);
2018-06-10 12:42:59 +00:00
it('popup correctly when panel is hidden', async () => {
2019-01-12 03:33:27 +00:00
const wrapper = mount(Cascader, { propsData: { options }, sync: false });
const CascaderWrapper = mount(
{
render() {
return wrapper.find({ name: 'Trigger' }).vm.getComponent();
},
2018-06-10 12:42:59 +00:00
},
2019-01-12 03:33:27 +00:00
{ sync: false },
);
2018-06-10 12:42:59 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
expect(CascaderWrapper.html()).toMatchSnapshot();
});
});
2018-06-10 12:42:59 +00:00
it('popup correctly when panel is open', async () => {
2019-01-12 03:33:27 +00:00
const wrapper = mount(Cascader, { propsData: { options }, sync: false });
2018-06-10 12:42:59 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
wrapper.find('input').trigger('click');
});
let CascaderWrapper = null;
2018-06-10 12:42:59 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
CascaderWrapper = mount(
{
render() {
return wrapper.find({ name: 'Trigger' }).vm.getComponent();
},
2018-06-10 12:42:59 +00:00
},
2019-01-12 03:33:27 +00:00
{ sync: false },
);
});
2018-06-10 12:42:59 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
expect(CascaderWrapper.html()).toMatchSnapshot();
});
});
2018-06-10 12:42:59 +00:00
it('support controlled mode', async () => {
2019-01-12 03:33:27 +00:00
const wrapper = mount(Cascader, { propsData: { options }, sync: false });
2018-06-10 12:42:59 +00:00
await asyncExpect(() => {
wrapper.setProps({
value: ['zhejiang', 'hangzhou', 'xihu'],
2019-01-12 03:33:27 +00:00
});
});
2018-06-10 12:42:59 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
expect(wrapper.html()).toMatchSnapshot();
});
});
2018-06-10 12:42:59 +00:00
it('popup correctly with defaultValue', async () => {
const wrapper = mount(Cascader, {
propsData: {
options,
defaultValue: ['zhejiang', 'hangzhou'],
2019-01-12 03:33:27 +00:00
},
sync: false,
});
2018-06-10 12:42:59 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
wrapper.find('input').trigger('click');
});
let CascaderWrapper = null;
2018-06-10 12:42:59 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
CascaderWrapper = mount(
{
render() {
return wrapper.find({ name: 'Trigger' }).vm.getComponent();
},
2018-06-10 12:42:59 +00:00
},
2019-01-12 03:33:27 +00:00
{ sync: false },
);
});
2018-06-10 12:42:59 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
expect(CascaderWrapper.html()).toMatchSnapshot();
});
});
2018-06-10 12:42:59 +00:00
it('can be selected', async () => {
2019-01-12 03:33:27 +00:00
const wrapper = mount(Cascader, { propsData: { options }, sync: false });
2018-06-10 12:42:59 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
wrapper.find('input').trigger('click');
});
let popupWrapper = null;
2018-06-10 12:42:59 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
popupWrapper = mount(
{
render() {
return wrapper.find({ name: 'Trigger' }).vm.getComponent();
},
2018-06-10 12:42:59 +00:00
},
2019-01-12 03:33:27 +00:00
{ sync: false },
);
});
2018-06-10 12:42:59 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
popupWrapper
.findAll('.ant-cascader-menu')
.at(0)
.findAll('.ant-cascader-menu-item')
.at(0)
.trigger('click');
});
2018-06-10 12:42:59 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
popupWrapper = mount(
{
render() {
return wrapper.find({ name: 'Trigger' }).vm.getComponent();
},
2018-06-10 12:42:59 +00:00
},
2019-01-12 03:33:27 +00:00
{ sync: false },
);
});
2018-06-10 12:42:59 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
expect(popupWrapper.html()).toMatchSnapshot();
});
2018-06-10 12:42:59 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
popupWrapper = mount(
{
render() {
return wrapper.find({ name: 'Trigger' }).vm.getComponent();
},
2018-06-10 12:42:59 +00:00
},
2019-01-12 03:33:27 +00:00
{ sync: false },
);
});
2018-06-10 12:42:59 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
popupWrapper
.findAll('.ant-cascader-menu')
.at(1)
.findAll('.ant-cascader-menu-item')
.at(0)
.trigger('click');
});
2018-06-10 12:42:59 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
popupWrapper = mount(
{
render() {
return wrapper.find({ name: 'Trigger' }).vm.getComponent();
},
2018-06-10 12:42:59 +00:00
},
2019-01-12 03:33:27 +00:00
{ sync: false },
);
});
2018-06-10 12:42:59 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
expect(popupWrapper.html()).toMatchSnapshot();
});
2018-06-10 12:42:59 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
popupWrapper = mount(
{
render() {
return wrapper.find({ name: 'Trigger' }).vm.getComponent();
},
2018-06-10 12:42:59 +00:00
},
2019-01-12 03:33:27 +00:00
{ sync: false },
);
});
2018-06-10 12:42:59 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
popupWrapper
.findAll('.ant-cascader-menu')
.at(2)
.findAll('.ant-cascader-menu-item')
.at(0)
.trigger('click');
});
2018-06-10 12:42:59 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
popupWrapper = mount(
{
render() {
return wrapper.find({ name: 'Trigger' }).vm.getComponent();
},
2018-06-10 12:42:59 +00:00
},
2019-01-12 03:33:27 +00:00
{ sync: false },
);
});
2018-06-10 12:42:59 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
expect(popupWrapper.html()).toMatchSnapshot();
});
});
2018-06-10 12:42:59 +00:00
it('backspace should work with `Cascader[showSearch]`', async () => {
2019-01-12 03:33:27 +00:00
const wrapper = mount(Cascader, { propsData: { options, showSearch: true }, sync: false });
2018-06-10 12:42:59 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
wrapper.find('input').element.value = '123';
wrapper.find('input').trigger('input');
});
2018-06-10 12:42:59 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
expect(wrapper.vm.inputValue).toBe('123');
});
2018-06-10 12:42:59 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
wrapper.find('input').element.keyCode = KeyCode.BACKSPACE;
wrapper.find('input').trigger('keydown');
});
2018-06-10 12:42:59 +00:00
await asyncExpect(() => {
// trigger onKeyDown will not trigger onChange by default, so the value is still '123'
2019-01-12 03:33:27 +00:00
expect(wrapper.vm.inputValue).toBe('123');
});
});
2019-01-03 12:47:39 +00:00
describe('limit filtered item count', () => {
beforeEach(() => {
2019-01-12 03:33:27 +00:00
document.body.outerHTML = '';
});
2019-01-03 12:47:39 +00:00
afterEach(() => {
2019-01-12 03:33:27 +00:00
document.body.outerHTML = '';
});
2019-01-03 12:47:39 +00:00
it('limit with positive number', async () => {
const wrapper = mount(Cascader, {
2019-01-12 03:33:27 +00:00
propsData: { options, showSearch: { filter, limit: 1 } },
2019-01-03 12:47:39 +00:00
sync: false,
attachToDocument: true,
2019-01-12 03:33:27 +00:00
});
wrapper.find('input').trigger('click');
wrapper.find('input').element.value = 'a';
wrapper.find('input').trigger('input');
2019-01-03 12:47:39 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
expect($$('.ant-cascader-menu-item').length).toBe(1);
}, 0);
});
2019-01-03 12:47:39 +00:00
it('not limit', async () => {
const wrapper = mount(Cascader, {
2019-01-12 03:33:27 +00:00
propsData: { options, showSearch: { filter, limit: false } },
2019-01-03 12:47:39 +00:00
sync: false,
attachToDocument: true,
2019-01-12 03:33:27 +00:00
});
wrapper.find('input').trigger('click');
wrapper.find('input').element.value = 'a';
wrapper.find('input').trigger('input');
2019-01-03 12:47:39 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
expect($$('.ant-cascader-menu-item').length).toBe(2);
}, 0);
});
2019-01-03 12:47:39 +00:00
it('negative limit', async () => {
2019-01-12 03:33:27 +00:00
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
2019-01-03 12:47:39 +00:00
const wrapper = mount(Cascader, {
2019-01-12 03:33:27 +00:00
propsData: { options, showSearch: { filter, limit: -1 } },
2019-01-03 12:47:39 +00:00
sync: false,
attachToDocument: true,
2019-01-12 03:33:27 +00:00
});
wrapper.find('input').trigger('click');
wrapper.find('input').element.value = 'a';
wrapper.find('input').trigger('input');
2019-01-03 12:47:39 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
expect($$('.ant-cascader-menu-item').length).toBe(2);
}, 0);
2019-01-03 12:47:39 +00:00
expect(errorSpy).toBeCalledWith(
"Warning: 'limit' of showSearch in Cascader should be positive number or false.",
2019-01-12 03:33:27 +00:00
);
});
});
});