forked from alibaba-fusion/next
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtextarea-spec.js
More file actions
292 lines (249 loc) · 9.58 KB
/
textarea-spec.js
File metadata and controls
292 lines (249 loc) · 9.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import React from 'react';
import Enzyme, { mount } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import sinon from 'sinon';
import assert from 'power-assert';
import Input from '../../src/input/index';
Enzyme.configure({ adapter: new Adapter() });
describe('TextArea', () => {
describe('behavior', () => {
// 判断事件是否执行
it('should support onChange/onFocus/onBlur events', () => {
const onChange = sinon.spy();
const onFocus = sinon.spy();
const onBlur = sinon.spy();
const wrapper = mount(
<Input.TextArea
onChange={onChange}
onFocus={onFocus}
onBlur={onBlur}
/>
);
wrapper
.find('textarea')
.simulate('change', { target: { value: '20' } });
assert(onChange.calledOnce);
wrapper.find('textarea').simulate('focus');
assert(onFocus.calledOnce);
wrapper.find('textarea').simulate('blur');
assert(onBlur.calledOnce);
});
it('should support defaultValue and can be changed', done => {
let onChange = value => {
assert(value === '20');
},
wrapper = mount(
<Input.TextArea defaultValue={'123'} onChange={onChange} />
);
assert(wrapper.find('textarea').prop('value') === '123');
wrapper
.find('textarea')
.simulate('change', { target: { value: '20' } });
assert(wrapper.find('textarea').prop('value') === '20');
done();
});
it('should support value and can not be changed', done => {
let onChange = value => {
assert(value === '20');
},
wrapper = mount(
<Input.TextArea value={'123'} onChange={onChange} />
);
assert(wrapper.find('textarea').prop('value') === '123');
wrapper
.find('textarea')
.simulate('change', { target: { value: '20' } });
assert(wrapper.find('textarea').prop('value') === '123');
done();
});
it('should work with value and onChange under control', done => {
class App extends React.Component {
state = {
value: '123',
};
onChange = value => {
this.setState({
value,
});
};
render() {
return (
<Input.TextArea
value={this.state.value}
onChange={this.onChange}
/>
);
}
}
let wrapper = mount(<App />);
assert(wrapper.find('textarea').prop('value') === '123');
wrapper
.find('textarea')
.simulate('change', { target: { value: '20' } });
assert(wrapper.find('textarea').prop('value') === '20');
done();
});
it('should support dsiabled', done => {
let wrapper = mount(<Input.TextArea disabled />);
assert(wrapper.find('textarea').prop('disabled'));
done();
});
it('should support maxLength & hasLimitHint', done => {
let wrapper = mount(
<Input.TextArea
defaultValue={'abcd'}
maxLength={10}
hasLimitHint
/>
);
assert(!wrapper.find('.next-input-len').hasClass('next-error'));
wrapper
.find('textarea')
.simulate('change', { target: { value: '12345678901' } });
assert(wrapper.find('.next-input-len').hasClass('next-error'));
let wrapper2 = mount(
<Input.TextArea maxLength={10} hasLimitHint />
);
wrapper2
.find('textarea')
.simulate('change', { target: { value: 'abc\nabc\n' } });
//ie 浏览器下面认为\n是两个字符串所以展示各有不同,这里不做校验了
// expect(wrapper2.find('.next-input-len').text()).to.be.equal('8/10');
done();
});
it('should support state', () => {
let wrapper = mount(<Input.TextArea state="error" />);
assert(wrapper.find('.next-input').hasClass('next-error'));
});
it('should support onFocus & onBlur', done => {
let wrapper2 = mount(
<Input.TextArea
defaultValue={'123'}
onFocus={e => {
assert(e.target.value === '123');
}}
/>
);
wrapper2.find('textarea').simulate('focus');
let wrapper3 = mount(
<Input.TextArea
defaultValue={'123'}
onBlur={e => {
assert(e.target.value === '123');
}}
/>
);
wrapper3.find('textarea').simulate('blur');
done();
});
it('should support getInputNode', done => {
class App extends React.Component {
render() {
return (
<Input.TextArea
ref="textarea"
onFocus={e => {
assert(
this.refs.textarea
.getInstance()
.getInputNode() !== undefined
);
}}
/>
);
}
}
let wrapper = mount(<App />);
wrapper.find('textarea').simulate('focus');
done();
});
it('should support getValueLength', done => {
let getValueLength = sinon.spy();
mount(
<Input.TextArea
defaultValue="abcdef"
maxLength={10}
getValueLength={getValueLength}
/>
);
assert(getValueLength.calledOnce);
let getValueLength2 = value => {
return 1;
},
wrapper = mount(
<Input.TextArea
defaultValue="abcdef"
maxLength={10}
hasLimitHint
getValueLength={getValueLength2}
/>
);
assert(wrapper.find('.next-input-len').text() === '1/10');
let wrapper2 = mount(
<Input.TextArea
defaultValue="abcdef"
maxLength={10}
hasLimitHint
getValueLength={getValueLength2}
/>
);
assert(wrapper2.find('.next-input-len').text() === '1/10');
done();
});
it('should support autoHeight', done => {
let wrapper = mount(
<Input.TextArea defaultValue="abcdef" autoHeight />
);
// console.log(wrapper.find('textarea[data-real]').instance().clientHeight)
// let originHeight = wrapper.find('textarea[data-real]').instance().clientHeight;
wrapper
.find('textarea[data-real]')
.simulate('change', { target: { value: '1\n2\n3\n4\n5\n' } });
// assert(wrapper.find('textarea[data-real]').at(0).getElement().clientHeight > originHeight);
let wrapper2 = mount(
<Input.TextArea
defaultValue="abcdef"
autoHeight={{ minRows: 2, maxRows: 4 }}
/>
);
// console.log(wrapper2.find('textarea[data-real]').instance().clientHeight)
// let originHeight = wrapper2.find('textarea[data-real]').instance().clientHeight;
wrapper2
.find('textarea[data-real]')
.simulate('change', { target: { value: '1\n2\n3\n4\n5\n' } });
wrapper2
.find('textarea[data-real]')
.simulate('change', { target: { value: '1\n2\n3\n4' } });
//
done();
});
it('should support focus', done => {
class App extends React.Component {
render() {
return <Input.TextArea ref="textarea" />;
}
}
let wrapper = mount(<App />);
wrapper
.ref('textarea')
.getInstance()
.focus();
wrapper.update();
// assert(wrapper.find('.next-input').hasClass('next-focus'));
done();
});
});
describe('react api', () => {
it('calls componentWillReceiveProps', done => {
let wrapper = mount(
<Input.TextArea autoHeight defaultValue={19} />
);
wrapper.setProps({ value: '30' });
assert(wrapper.find('textarea[data-real]').prop('value') === '30');
// value = undefined 时候清空数据
wrapper.setProps({ value: undefined });
assert(wrapper.find('textarea[data-real]').prop('value') === '');
done();
});
});
});