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

114 lines
3.3 KiB
JavaScript
Raw Normal View History

2019-01-12 03:33:27 +00: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 02:26:24 +00:00
2019-01-12 03:33:27 +00:00
const { TextArea } = Input;
2018-06-10 02:26:24 +00:00
describe('Input', () => {
2019-01-12 03:33:27 +00:00
focusTest(Input);
2018-06-10 02:26:24 +00:00
it('should support maxLength', async () => {
2019-01-12 03:33:27 +00:00
const wrapper = mount(Input, { attrs: { maxLength: 3 }, sync: false });
2018-06-10 02:26:24 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
expect(wrapper.html()).toMatchSnapshot();
}, 0);
});
2018-12-09 03:43:27 +00:00
it('select()', () => {
2019-01-12 03:33:27 +00:00
const wrapper = mount(Input);
wrapper.vm.select();
});
});
2018-06-10 02:26:24 +00:00
2019-01-12 03:33:27 +00:00
focusTest(TextArea);
2018-06-10 02:26:24 +00:00
describe('TextArea', () => {
it('should auto calculate height according to content length', async () => {
2019-01-12 03:33:27 +00:00
const wrapper = mount(TextArea, {
propsData: { value: '', readOnly: true, autosize: true },
sync: false,
});
2018-06-10 02:26:24 +00:00
2019-01-12 03:33:27 +00:00
const mockFunc = jest.spyOn(wrapper.vm, 'resizeTextarea');
2018-06-10 02:26:24 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
wrapper.setProps({ value: '1111\n2222\n3333' });
});
2018-06-10 02:26:24 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
expect(mockFunc).toHaveBeenCalledTimes(1);
});
2018-06-10 02:26:24 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
wrapper.setProps({ value: '1111' });
});
2018-06-10 02:26:24 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
expect(mockFunc).toHaveBeenCalledTimes(2);
2019-07-04 10:21:43 +00:00
}, 100);
2019-01-12 03:33:27 +00:00
});
2018-06-10 02:26:24 +00:00
it('should support disabled', async () => {
2019-01-12 03:33:27 +00:00
const wrapper = mount(TextArea, { propsData: { disabled: true }, sync: false });
2018-06-10 02:26:24 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
expect(wrapper.html()).toMatchSnapshot();
});
});
2018-06-10 02:26:24 +00:00
it('should support maxLength', async () => {
2019-01-12 03:33:27 +00:00
const wrapper = mount(TextArea, { attrs: { maxLength: 10 }, sync: false });
2018-06-10 02:26:24 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
expect(wrapper.html()).toMatchSnapshot();
});
});
});
2018-06-10 02:26:24 +00:00
describe('As Form Control', () => {
it('should be reset when wrapped in form.getFieldDecorator without initialValue', async () => {
const Demo = {
methods: {
2019-01-12 03:33:27 +00:00
reset() {
this.form.resetFields();
2018-06-10 02:26:24 +00:00
},
},
2019-01-12 03:33:27 +00:00
render() {
const { getFieldDecorator } = this.form;
2018-06-10 02:26:24 +00:00
return (
<Form>
2019-01-12 03:33:27 +00: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 02:26:24 +00:00
</Form>
2019-01-12 03:33:27 +00:00
);
2018-06-10 02:26:24 +00:00
},
2019-01-12 03:33:27 +00:00
};
const DemoForm = Form.create()(Demo);
const wrapper = mount(DemoForm, { sync: false });
2018-06-10 02:26:24 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00: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 02:26:24 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00: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 02:26:24 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
expect(wrapper.find('input').element.value).toBe('');
expect(wrapper.find('textarea').element.value).toBe('');
});
});
});
2018-06-10 02:26:24 +00:00
describe('Input.Search', () => {
it('should support suffix', async () => {
2019-01-12 03:33:27 +00:00
const wrapper = mount(Input.Search, { propsData: { suffix: 'suffix' }, sync: false });
2018-06-10 02:26:24 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
expect(wrapper.html()).toMatchSnapshot();
});
});
});