ant-design-vue/components/form/__tests__/label.test.js

224 lines
5.5 KiB
JavaScript
Raw Normal View History

2019-01-12 03:33:27 +00:00
import { mount } from '@vue/test-utils';
import Form from '..';
import { asyncExpect } from '@/tests/utils';
2018-06-28 13:41:02 +00:00
describe('Form', () => {
2019-09-11 10:11:48 +00:00
// Mock of `querySelector`
const originQuerySelector = HTMLElement.prototype.querySelector;
HTMLElement.prototype.querySelector = function querySelector(str) {
const match = str.match(/^\[id=('|")(.*)('|")]$/);
const id = match && match[2];
// Use origin logic
if (id) {
const [input] = this.getElementsByTagName('input');
if (input && input.id === id) {
return input;
}
}
return originQuerySelector.call(this, str);
};
afterAll(() => {
HTMLElement.prototype.querySelector = originQuerySelector;
});
2018-06-28 13:41:02 +00:00
it('should remove duplicated user input colon', () => {
const wrapper = mount({
2019-01-12 03:33:27 +00:00
render() {
2018-06-28 13:41:02 +00:00
return (
<Form>
2019-01-12 03:33:27 +00:00
<Form.Item label="label:">input</Form.Item>
<Form.Item label="label">input</Form.Item>
2018-06-28 13:41:02 +00:00
</Form>
2019-01-12 03:33:27 +00:00
);
2018-06-28 13:41:02 +00:00
},
2019-01-12 03:33:27 +00:00
});
expect(
wrapper
.findAll('.ant-form-item-label label')
.at(0)
.text(),
).not.toContain(':');
expect(
wrapper
.findAll('.ant-form-item-label label')
.at(1)
.text(),
).not.toContain('');
});
2018-06-28 13:41:02 +00:00
it('should not remove duplicated user input colon when props colon is false', () => {
const wrapper = mount({
2019-01-12 03:33:27 +00:00
render() {
2018-06-28 13:41:02 +00:00
return (
<Form>
2019-01-12 03:33:27 +00:00
<Form.Item label="label:" colon={false}>
input
</Form.Item>
<Form.Item label="label" colon={false}>
input
</Form.Item>
2018-06-28 13:41:02 +00:00
</Form>
2019-01-12 03:33:27 +00:00
);
2018-06-28 13:41:02 +00:00
},
2019-01-12 03:33:27 +00:00
});
expect(
wrapper
.findAll('.ant-form-item-label label')
.at(0)
.text(),
).toContain(':');
expect(
wrapper
.findAll('.ant-form-item-label label')
.at(1)
.text(),
).toContain('');
});
2018-06-28 13:41:02 +00:00
it('should not remove duplicated user input colon when layout is vertical', () => {
const wrapper = mount({
2019-01-12 03:33:27 +00:00
render() {
2018-06-28 13:41:02 +00:00
return (
2019-01-12 03:33:27 +00:00
<Form layout="vertical">
<Form.Item label="label:">input</Form.Item>
<Form.Item label="label">input</Form.Item>
2018-06-28 13:41:02 +00:00
</Form>
2019-01-12 03:33:27 +00:00
);
2018-06-28 13:41:02 +00:00
},
2019-01-12 03:33:27 +00:00
});
expect(
wrapper
.findAll('.ant-form-item-label label')
.at(0)
.text(),
).toContain(':');
expect(
wrapper
.findAll('.ant-form-item-label label')
.at(1)
.text(),
).toContain('');
});
2018-06-28 13:41:02 +00:00
it('should has dom with .ant-form-item-control-wrapper', () => {
const formItemLayout = {
labelCol: { span: 6 },
wrapperCol: { span: 14 },
2019-01-12 03:33:27 +00:00
};
2018-06-28 13:41:02 +00:00
const wrapper = mount({
2019-01-12 03:33:27 +00:00
render() {
2018-06-28 13:41:02 +00:00
return (
<Form>
2019-01-12 03:33:27 +00:00
<Form.Item {...{ props: { ...formItemLayout } }}>input</Form.Item>
2018-06-28 13:41:02 +00:00
<Form.Item>input</Form.Item>
</Form>
2019-01-12 03:33:27 +00:00
);
2018-06-28 13:41:02 +00:00
},
2019-01-12 03:33:27 +00:00
});
expect(wrapper.findAll('.ant-form-item-control-wrapper').length).toBe(2);
expect(wrapper.findAll('.ant-form-item-control-wrapper.ant-col-14').length).toBe(1);
});
2018-06-28 13:41:02 +00:00
// https://github.com/ant-design/ant-design/issues/7351
it('focus correct input when click label', async () => {
const Form1 = Form.create()({
2019-01-12 03:33:27 +00:00
render() {
2018-06-28 13:41:02 +00:00
return (
<Form>
2019-01-12 03:33:27 +00:00
<Form.Item label="label 1">{this.form.getFieldDecorator('test')(<input />)}</Form.Item>
2018-06-28 13:41:02 +00:00
</Form>
2019-01-12 03:33:27 +00:00
);
2018-06-28 13:41:02 +00:00
},
2019-01-12 03:33:27 +00:00
});
2018-06-28 13:41:02 +00:00
const Form2 = Form.create()({
2019-01-12 03:33:27 +00:00
render() {
2018-06-28 13:41:02 +00:00
return (
<Form>
2019-01-12 03:33:27 +00:00
<Form.Item label="label 2">{this.form.getFieldDecorator('test2')(<input />)}</Form.Item>
2018-06-28 13:41:02 +00:00
</Form>
2019-01-12 03:33:27 +00:00
);
2018-06-28 13:41:02 +00:00
},
2019-01-12 03:33:27 +00:00
});
2018-06-28 13:41:02 +00:00
2019-01-12 03:33:27 +00:00
const wrapper = mount(
{
render() {
return (
<div>
<Form1 />
<Form2 />
</div>
);
},
2018-06-28 13:41:02 +00:00
},
2019-01-12 03:33:27 +00:00
{ sync: false },
);
2018-06-28 13:41:02 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
wrapper
.findAll({ name: 'AForm' })
.at(0)
.findAll('label')
.at(0)
.trigger('click');
});
2018-06-28 13:41:02 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
expect(
wrapper
.findAll({ name: 'AForm' })
.at(0)
.findAll('input')
.at(0).element,
).toBe(document.activeElement);
});
2018-06-28 13:41:02 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
wrapper
.findAll({ name: 'AForm' })
.at(1)
.findAll('label')
.at(0)
.trigger('click');
});
2018-06-28 13:41:02 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
expect(
wrapper
.findAll({ name: 'AForm' })
.at(1)
.findAll('input')
.at(0).element,
).toBe(document.activeElement);
});
});
2018-06-28 13:41:02 +00:00
// https://github.com/ant-design/ant-design/issues/7693
it('should not throw error when is not a valid id', async () => {
const Form1 = Form.create()({
2019-01-12 03:33:27 +00:00
render() {
2018-06-28 13:41:02 +00:00
return (
<Form>
2019-01-12 03:33:27 +00:00
<Form.Item label="label 1">
2018-06-28 13:41:02 +00:00
{this.form.getFieldDecorator('member[0].name.firstname')(<input />)}
</Form.Item>
</Form>
2019-01-12 03:33:27 +00:00
);
2018-06-28 13:41:02 +00:00
},
2019-01-12 03:33:27 +00:00
});
2018-06-28 13:41:02 +00:00
2019-01-12 03:33:27 +00:00
const wrapper = mount(Form1, { sync: false, attachToDocument: true });
2018-06-28 13:41:02 +00:00
await asyncExpect(() => {
expect(() => {
2019-01-12 03:33:27 +00:00
wrapper
.find({ name: 'AForm' })
.findAll('label')
.at(0)
.trigger('click');
}).not.toThrow();
});
2018-06-28 13:41:02 +00:00
// 不合法id时document.activeElement没有正确更新
// await asyncExpect(() => {
// expect(wrapper.find({ name: 'AForm' }).findAll('input').at(0).element).toBe(document.activeElement)
// }, 0)
2019-01-12 03:33:27 +00:00
});
});