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

122 lines
3.5 KiB
JavaScript
Raw Normal View History

2019-01-12 11:33:27 +08:00
import { mount } from '@vue/test-utils';
import { asyncExpect } from '@/tests/utils';
import Input from '..';
import Form from '../../form';
import focusTest from '../../../tests/shared/focusTest';
2018-06-10 10:26:24 +08:00
2019-01-12 11:33:27 +08:00
const { TextArea } = Input;
2018-06-10 10:26:24 +08:00
describe('Input', () => {
2019-01-12 11:33:27 +08:00
focusTest(Input);
2018-06-10 10:26:24 +08:00
it('should support maxLength', async () => {
2019-01-12 11:33:27 +08:00
const wrapper = mount(Input, { attrs: { maxLength: 3 }, sync: false });
2018-06-10 10:26:24 +08:00
await asyncExpect(() => {
2019-01-12 11:33:27 +08:00
expect(wrapper.html()).toMatchSnapshot();
}, 0);
});
2018-12-09 11:43:27 +08:00
it('select()', () => {
2019-01-12 11:33:27 +08:00
const wrapper = mount(Input);
wrapper.vm.select();
});
2019-10-17 18:52:10 +08:00
it('should not support allowClear when it is disabled', () => {
const wrapper = mount(Input, {
propsData: { allowClear: true, defaultValue: '111', disabled: true },
sync: false,
});
expect(wrapper.findAll('.ant-input-clear-icon').length).toBe(0);
});
2019-01-12 11:33:27 +08:00
});
2018-06-10 10:26:24 +08:00
2019-01-12 11:33:27 +08:00
focusTest(TextArea);
2018-06-10 10:26:24 +08:00
describe('TextArea', () => {
it('should auto calculate height according to content length', async () => {
2019-01-12 11:33:27 +08:00
const wrapper = mount(TextArea, {
propsData: { value: '', readOnly: true, autosize: true },
sync: false,
});
2018-06-10 10:26:24 +08:00
2019-01-12 11:33:27 +08:00
const mockFunc = jest.spyOn(wrapper.vm, 'resizeTextarea');
2018-06-10 10:26:24 +08:00
await asyncExpect(() => {
2019-01-12 11:33:27 +08:00
wrapper.setProps({ value: '1111\n2222\n3333' });
});
2018-06-10 10:26:24 +08:00
await asyncExpect(() => {
2019-01-12 11:33:27 +08:00
expect(mockFunc).toHaveBeenCalledTimes(1);
});
2018-06-10 10:26:24 +08:00
await asyncExpect(() => {
2019-01-12 11:33:27 +08:00
wrapper.setProps({ value: '1111' });
});
2018-06-10 10:26:24 +08:00
await asyncExpect(() => {
2019-01-12 11:33:27 +08:00
expect(mockFunc).toHaveBeenCalledTimes(2);
2019-07-04 18:21:43 +08:00
}, 100);
2019-01-12 11:33:27 +08:00
});
2018-06-10 10:26:24 +08:00
it('should support disabled', async () => {
2019-01-12 11:33:27 +08:00
const wrapper = mount(TextArea, { propsData: { disabled: true }, sync: false });
2018-06-10 10:26:24 +08:00
await asyncExpect(() => {
2019-01-12 11:33:27 +08:00
expect(wrapper.html()).toMatchSnapshot();
});
});
2018-06-10 10:26:24 +08:00
it('should support maxLength', async () => {
2019-01-12 11:33:27 +08:00
const wrapper = mount(TextArea, { attrs: { maxLength: 10 }, sync: false });
2018-06-10 10:26:24 +08:00
await asyncExpect(() => {
2019-01-12 11:33:27 +08:00
expect(wrapper.html()).toMatchSnapshot();
});
});
});
2018-06-10 10:26:24 +08:00
describe('As Form Control', () => {
it('should be reset when wrapped in form.getFieldDecorator without initialValue', async () => {
const Demo = {
methods: {
2019-01-12 11:33:27 +08:00
reset() {
this.form.resetFields();
2018-06-10 10:26:24 +08:00
},
},
2019-01-12 11:33:27 +08:00
render() {
const { getFieldDecorator } = this.form;
2018-06-10 10:26:24 +08:00
return (
<Form>
2019-01-12 11:33:27 +08:00
<Form.Item>{getFieldDecorator('input')(<Input />)}</Form.Item>
<Form.Item>{getFieldDecorator('textarea')(<Input.TextArea />)}</Form.Item>
<button type="button" onClick={this.reset}>
reset
</button>
2018-06-10 10:26:24 +08:00
</Form>
2019-01-12 11:33:27 +08:00
);
2018-06-10 10:26:24 +08:00
},
2019-01-12 11:33:27 +08:00
};
const DemoForm = Form.create()(Demo);
const wrapper = mount(DemoForm, { sync: false });
2018-06-10 10:26:24 +08:00
await asyncExpect(() => {
2019-01-12 11:33:27 +08:00
wrapper.find('input').element.value = '111';
wrapper.find('input').trigger('input');
wrapper.find('textarea').element.value = '222';
wrapper.find('textarea').trigger('input');
});
2018-06-10 10:26:24 +08:00
await asyncExpect(() => {
2019-01-12 11:33:27 +08:00
expect(wrapper.find('input').element.value).toBe('111');
expect(wrapper.find('textarea').element.value).toBe('222');
wrapper.find('button').trigger('click');
});
2018-06-10 10:26:24 +08:00
await asyncExpect(() => {
2019-01-12 11:33:27 +08:00
expect(wrapper.find('input').element.value).toBe('');
expect(wrapper.find('textarea').element.value).toBe('');
});
});
});
2018-06-10 10:26:24 +08:00
describe('Input.Search', () => {
it('should support suffix', async () => {
2019-01-12 11:33:27 +08:00
const wrapper = mount(Input.Search, { propsData: { suffix: 'suffix' }, sync: false });
2018-06-10 10:26:24 +08:00
await asyncExpect(() => {
2019-01-12 11:33:27 +08:00
expect(wrapper.html()).toMatchSnapshot();
});
});
});