Skip to content

Commit f3b370a

Browse files
committed
test: add input test
1 parent abd5ff9 commit f3b370a

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Input should support maxLength 1`] = `<input type="text" maxlength="3" class="ant-input">`;
4+
5+
exports[`Input.Search should support suffix 1`] = `<span class="ant-input-search ant-input-affix-wrapper ant-input-search"><input type="text" class="ant-input ant-input-search"><span class="ant-input-suffix">suffix<i class="anticon anticon-search ant-input-search-icon"></i></span></span>`;
6+
7+
exports[`TextArea should support disabled 1`] = `<textarea disabled="disabled" class="ant-input ant-input-disabled"></textarea>`;
8+
9+
exports[`TextArea should support maxLength 1`] = `<textarea maxlength="10" class="ant-input"></textarea>`;
+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import { mount } from '@vue/test-utils'
2+
import { asyncExpect } from '@/tests/utils'
3+
import Input from '..'
4+
import Form from '../../form'
5+
import focusTest from '../../../tests/shared/focusTest'
6+
7+
const { TextArea } = Input
8+
9+
describe('Input', () => {
10+
focusTest(Input)
11+
12+
it('should support maxLength', async () => {
13+
const wrapper = mount(Input, { attrs: { maxLength: 3 }, sync: false })
14+
await asyncExpect(() => {
15+
expect(wrapper.html()).toMatchSnapshot()
16+
}, 0)
17+
})
18+
})
19+
20+
focusTest(TextArea)
21+
22+
describe('TextArea', () => {
23+
it('should auto calculate height according to content length', async () => {
24+
const wrapper = mount(TextArea, { propsData: { value: '', readOnly: true, autosize: true }, sync: false })
25+
26+
const mockFunc = jest.spyOn(wrapper.vm, 'resizeTextarea')
27+
await asyncExpect(() => {
28+
wrapper.setProps({ value: '1111\n2222\n3333' })
29+
})
30+
await asyncExpect(() => {
31+
expect(mockFunc).toHaveBeenCalledTimes(1)
32+
})
33+
await asyncExpect(() => {
34+
wrapper.setProps({ value: '1111' })
35+
})
36+
37+
await asyncExpect(() => {
38+
expect(mockFunc).toHaveBeenCalledTimes(2)
39+
}, 0)
40+
})
41+
42+
it('should support disabled', async () => {
43+
const wrapper = mount(TextArea, { propsData: { disabled: true }, sync: false })
44+
await asyncExpect(() => {
45+
expect(wrapper.html()).toMatchSnapshot()
46+
})
47+
})
48+
49+
it('should support maxLength', async () => {
50+
const wrapper = mount(TextArea, { attrs: { maxLength: 10 }, sync: false })
51+
await asyncExpect(() => {
52+
expect(wrapper.html()).toMatchSnapshot()
53+
})
54+
})
55+
})
56+
57+
describe('As Form Control', () => {
58+
it('should be reset when wrapped in form.getFieldDecorator without initialValue', async () => {
59+
const Demo = {
60+
methods: {
61+
reset () {
62+
this.form.resetFields()
63+
},
64+
},
65+
66+
render () {
67+
const { getFieldDecorator } = this.form
68+
return (
69+
<Form>
70+
<Form.Item>
71+
{getFieldDecorator('input')(<Input />)}
72+
</Form.Item>
73+
<Form.Item>
74+
{getFieldDecorator('textarea')(<Input.TextArea />)}
75+
</Form.Item>
76+
<button onClick={this.reset}>reset</button>
77+
</Form>
78+
)
79+
},
80+
}
81+
const DemoForm = Form.create()(Demo)
82+
const wrapper = mount(DemoForm, { sync: false })
83+
await asyncExpect(() => {
84+
wrapper.find('input').element.value = '111'
85+
wrapper.find('input').trigger('change')
86+
wrapper.find('textarea').element.value = '222'
87+
wrapper.find('textarea').trigger('change')
88+
})
89+
await asyncExpect(() => {
90+
expect(wrapper.find('input').element.value).toBe('111')
91+
expect(wrapper.find('textarea').element.value).toBe('222')
92+
wrapper.find('button').trigger('click')
93+
})
94+
await asyncExpect(() => {
95+
expect(wrapper.find('input').element.value).toBe('')
96+
expect(wrapper.find('textarea').element.value).toBe('')
97+
})
98+
})
99+
})
100+
101+
describe('Input.Search', () => {
102+
it('should support suffix', async () => {
103+
const wrapper = mount(Input.Search, { propsData: { suffix: 'suffix' }, sync: false })
104+
await asyncExpect(() => {
105+
expect(wrapper.html()).toMatchSnapshot()
106+
})
107+
})
108+
})

0 commit comments

Comments
 (0)