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

266 lines
6.6 KiB
JavaScript
Raw Normal View History

import React from 'react';
import { mount, render } from 'enzyme';
import Form from '..';
2017-01-15 14:37:22 +08:00
describe('Form', () => {
2019-02-04 22:02:46 +08:00
// Mock of `querySelector`
const originQuerySelector = HTMLElement.prototype.querySelector;
2019-02-23 16:35:04 +08:00
HTMLElement.prototype.querySelector = function querySelector(str) {
2019-02-04 22:02:46 +08:00
const match = str.match(/^\[id=('|")(.*)('|")]$/);
const id = match && match[2];
// Use origin logic
if (id) {
2019-02-04 22:55:00 +08:00
const [input] = this.getElementsByTagName('input');
if (input && input.id === id) {
2019-02-04 22:02:46 +08:00
return input;
}
}
return originQuerySelector.call(this, str);
};
afterAll(() => {
HTMLElement.prototype.querySelector = originQuerySelector;
});
it('should remove duplicated user input colon', () => {
const wrapper = mount(
<Form>
<Form.Item label="label:">input</Form.Item>
<Form.Item label="label">input</Form.Item>
2018-12-07 16:17:45 +08:00
</Form>,
);
2018-12-07 16:17:45 +08:00
expect(
wrapper
.find('.ant-form-item-label label')
.at(0)
.text(),
).not.toContain(':');
expect(
wrapper
.find('.ant-form-item-label label')
.at(1)
.text(),
).not.toContain('');
});
2019-03-08 11:25:37 +08:00
it('should disable colon when props colon Form is false', () => {
const wrapper = mount(
<Form colon={false}>
<Form.Item label="label">input</Form.Item>
2019-03-08 11:25:37 +08:00
</Form>,
);
expect(
wrapper
.find('.ant-form-item-label label')
2019-03-08 11:25:37 +08:00
.at(0)
.hasClass('ant-form-item-no-colon'),
).toBe(true);
});
it('should props colon of Form.Item override the props colon of Form.', () => {
const wrapper = mount(
<Form colon={false}>
<Form.Item label="label">input</Form.Item>
<Form.Item label="label" colon>
input
</Form.Item>
<Form.Item label="label" colon={false}>
input
</Form.Item>
</Form>,
);
expect(wrapper.render()).toMatchSnapshot();
const testLabel = mount(
<Form colon={false}>
<Form.Item label="label:" colon>
input
</Form.Item>
<Form.Item label="label" colon>
input
</Form.Item>
</Form>,
);
expect(
testLabel
.find('.ant-form-item-label label')
.at(0)
.text(),
).not.toContain(':');
expect(
testLabel
.find('.ant-form-item-label label')
.at(1)
.text(),
).not.toContain('');
});
it('should not remove duplicated user input colon when props colon is false', () => {
const wrapper = mount(
<Form>
2018-12-07 16:17:45 +08:00
<Form.Item label="label:" colon={false}>
input
</Form.Item>
<Form.Item label="label" colon={false}>
input
</Form.Item>
</Form>,
);
2018-12-07 16:17:45 +08:00
expect(
wrapper
.find('.ant-form-item-label label')
.at(0)
.text(),
).toContain(':');
expect(
wrapper
.find('.ant-form-item-label label')
.at(1)
.text(),
).toContain('');
});
it('should not remove duplicated user input colon when layout is vertical', () => {
const wrapper = mount(
2017-03-02 14:14:34 +08:00
<Form layout="vertical">
<Form.Item label="label:">input</Form.Item>
<Form.Item label="label">input</Form.Item>
2018-12-07 16:17:45 +08:00
</Form>,
);
2018-12-07 16:17:45 +08:00
expect(
wrapper
.find('.ant-form-item-label label')
.at(0)
.text(),
).toContain(':');
expect(
wrapper
.find('.ant-form-item-label label')
.at(1)
.text(),
).toContain('');
});
it('should has dom with .ant-form-item-control-wrapper', () => {
const formItemLayout = {
labelCol: { span: 6 },
wrapperCol: { span: 14 },
};
const wrapper = mount(
<Form>
<Form.Item {...formItemLayout}>input</Form.Item>
<Form.Item>input</Form.Item>
2018-12-07 16:17:45 +08:00
</Form>,
);
2017-09-20 16:26:18 +08:00
expect(wrapper.find('.ant-form-item-control-wrapper').hostNodes().length).toBe(2);
expect(wrapper.find('.ant-form-item-control-wrapper.ant-col-14').length).toBe(1);
});
// https://github.com/ant-design/ant-design/issues/7351
it('focus correct input when click label', () => {
const Form1 = Form.create()(({ form }) => (
<Form>
2018-12-07 16:17:45 +08:00
<Form.Item label="label 1">{form.getFieldDecorator('test')(<input />)}</Form.Item>
</Form>
));
const Form2 = Form.create()(({ form }) => (
<Form>
2018-12-07 16:17:45 +08:00
<Form.Item label="label 2">{form.getFieldDecorator('test2')(<input />)}</Form.Item>
</Form>
));
2018-12-07 16:17:45 +08:00
const wrapper = mount(
<div>
<Form1 />
<Form2 />
</div>,
);
wrapper
.find('Form label')
.at(0)
.simulate('click');
expect(
wrapper
.find('Form input')
.at(0)
.getDOMNode(),
).toBe(document.activeElement);
wrapper
.find('Form label')
.at(1)
.simulate('click');
expect(
wrapper
.find('Form input')
.at(1)
.getDOMNode(),
).toBe(document.activeElement);
});
// https://github.com/ant-design/ant-design/issues/7693
it('should not throw error when is not a valid id', () => {
const Form1 = Form.create()(({ form }) => (
<Form>
<Form.Item label="label 1">
{form.getFieldDecorator('member[0].name.firstname')(<input />)}
</Form.Item>
</Form>
));
const wrapper = mount(<Form1 />);
expect(() => {
2018-12-07 16:17:45 +08:00
wrapper
.find('Form label')
.at(0)
.simulate('click');
}).not.toThrow();
2018-12-07 16:17:45 +08:00
expect(
wrapper
.find('Form input')
.at(0)
.getDOMNode(),
).toBe(document.activeElement);
});
it('should `labelAlign` work in FormItem', () => {
// Works in Form.Item
expect(render(<Form.Item label="test" labelAlign="left" />)).toMatchSnapshot();
// Use Form.Item first
expect(
render(
<Form labelAlign="right">
<Form.Item label="test" labelAlign="left" />
</Form>,
),
).toMatchSnapshot();
});
meger feature to master (#16421) * use ul in list * update snapshot * update comment * feat: TreeSelect support `showSearch` in multiple mode (#15933) * update rc-tree-select * typo * update desc & snapshot * update desc & snapshot * check default showSearch * feat: table customizing variable (#15971) * feat: added table selected row color variable * fix: @table-selected-row-color default is inherit * feat: Upload support customize previewFile (#15984) * support preview file * use promise * dealy load * use canvas of render * use domHook of test * update demo * add snapshot * update types * update testcase * feat: form customizing variables (#15954) * fix: added styling form input background-color * feat: added '@form-warning-input-bg' variable * feat: added '@form-error-input-bg' variable * use li wrap with comment * feat: Support append theme less file with less-variable (#16118) * add override * add override support * update doc * feat: dropdown support set right icon * docs: update doc of dropdown component * style: format dropdown-button.md * test: update updateSnapshot * style: format dropdown-button.md * test: update updateSnapshot * test: update updateSnapshot * style: change style of dropdown-button demo * fix: fix document table order * feat: Support SkeletonAvatarProps.size accept number (#16078) (#16128) * chore:update style of demo * feat: Notification functions accept top, bottom and getContainer as arguments * drawer: add afterVisibleChange * rm onVisibleChange * update * feat: 🇭🇷 hr_HR locale (#16258) * Added Croatian locale * fixed lint error * :white_check_mark: Add test cases for hr_HR * :memo: update i18n documentation * feat: add `htmlFor` in Form.Item (#16278) * add htmlFor in Form.Item * update doc * feat: Button support `link` type (#16289) close #15892 * feat: Add Timeline.Item.position (#16148) (#16193) * fix: Timeline.pendingDot interface documentation there is a small problem (#16177) * feat: Add Timeline.Item.position (#16148) * doc: add version infomation for Timeline.Item.position * refactor: Update Tree & TreeSelect deps (#16330) * use CSSMotion * update snapshot * feat: Collapse support `expandIconPosition` (#16365) * update doc * support expandIconPosition * update snapshot * feat: Breadcrumb support DropDown (#16315) * breadcrumbs support drop down menu * update doc * add require less * fix test * fix md doc * less code * fix style warning * update snap * add children render test * feat: TreeNode support checkable * feat: add optional to support top and left slick dots (#16186) (#16225) * add optional to support top and left slick dots * update carousel snapshot * Update doc, add placement demo * update carousel placement demo snapshots * rename dots placement to position * update vertical as deprecated * rename dotsPosition to dotPosition * refine code * add warning testcase for vertical * remove unused warning * update expression * Additional test case for dotPosition * refactor: Upgrade `rc-tree-select` to support pure React motion (#16402) * upgrade `rc-tree-select` * update snapshot * 3.17.0 changelog * fix warning * fix review warning
2019-05-06 12:04:39 +08:00
describe('should `htmlFor` work', () => {
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
afterEach(() => {
errorSpy.mockReset();
});
afterAll(() => {
errorSpy.mockRestore();
});
it('should warning when use `id`', () => {
mount(<Form.Item id="bamboo" label="bamboo" />);
expect(errorSpy).toHaveBeenCalledWith(
'Warning: [antd: Form.Item] `id` is deprecated for its label `htmlFor`. Please use `htmlFor` directly.',
);
});
it('use `htmlFor`', () => {
const wrapper = mount(<Form.Item htmlFor="bamboo" label="bamboo" />);
expect(wrapper.find('label').props().htmlFor).toBe('bamboo');
});
});
});