-
-
Notifications
You must be signed in to change notification settings - Fork 186
Expand file tree
/
Copy pathgithub.test.tsx
More file actions
506 lines (416 loc) · 14 KB
/
Copy pathgithub.test.tsx
File metadata and controls
506 lines (416 loc) · 14 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
import React from 'react';
import { act } from 'react-dom/test-utils';
import { mount } from './util/wrapper';
import InputNumber from '../src';
import KeyCode from 'rc-util/lib/KeyCode';
// Github issues
describe('InputNumber.Github', () => {
beforeEach(() => {
jest.useFakeTimers();
});
afterEach(() => {
jest.clearAllTimers();
jest.useRealTimers();
});
// https://github.com/react-component/input-number/issues/32
it('issue 32', () => {
const wrapper = mount(<InputNumber step={0.1} />);
wrapper.focusInput();
wrapper.changeValue('2');
expect(wrapper.getInputValue()).toEqual('2');
wrapper.blurInput();
expect(wrapper.getInputValue()).toEqual('2.0');
});
// https://github.com/react-component/input-number/issues/197
it('issue 197', () => {
const Demo = () => {
const [value, setValue] = React.useState<string | number>(NaN);
return (
<InputNumber
step={1}
value={value}
onChange={(newValue) => {
setValue(newValue);
}}
/>
);
};
const wrapper = mount(<Demo />);
wrapper.focusInput();
wrapper.changeValue('foo');
});
// https://github.com/react-component/input-number/issues/222
it('issue 222', () => {
const Demo = () => {
const [value, setValue] = React.useState<string | number>(1);
return (
<InputNumber
step={1}
max={NaN}
value={value}
onChange={(newValue) => {
setValue(newValue);
}}
/>
);
};
const wrapper = mount(<Demo />);
wrapper.focusInput();
wrapper.changeValue('foo');
wrapper.find('.rc-input-number-handler-up').simulate('mouseDown');
expect(wrapper.getInputValue()).toEqual('2');
});
// https://github.com/react-component/input-number/issues/35
it('issue 35', () => {
let num: string | number;
const wrapper = mount(
<InputNumber
step={0.01}
defaultValue={2}
onChange={(value) => {
num = value;
}}
/>,
);
for (let i = 1; i <= 400; i += 1) {
wrapper.find('input').simulate('keyDown', { which: KeyCode.DOWN });
// no number like 1.5499999999999999
expect((num.toString().split('.')[1] || '').length < 3).toBeTruthy();
const expectedValue = Number(((200 - i) / 100).toFixed(2));
expect(wrapper.getInputValue()).toEqual(String(expectedValue.toFixed(2)));
expect(num).toEqual(expectedValue);
}
for (let i = 1; i <= 300; i += 1) {
wrapper.find('input').simulate('keyDown', { which: KeyCode.UP });
// no number like 1.5499999999999999
expect((num.toString().split('.')[1] || '').length < 3).toBeTruthy();
const expectedValue = Number(((i - 200) / 100).toFixed(2));
expect(wrapper.getInputValue()).toEqual(String(expectedValue.toFixed(2)));
expect(num).toEqual(expectedValue);
}
});
// https://github.com/ant-design/ant-design/issues/4229
it('long press not trigger onChange in uncontrolled component', () => {
const onChange = jest.fn();
const wrapper = mount(<InputNumber defaultValue={0} onChange={onChange} />);
wrapper.find('.rc-input-number-handler-up').simulate('mouseDown');
act(() => {
jest.advanceTimersByTime(500);
});
expect(onChange).toHaveBeenCalledWith(1);
act(() => {
jest.advanceTimersByTime(200);
});
expect(onChange).toHaveBeenCalledWith(2);
});
// https://github.com/ant-design/ant-design/issues/4757
it('should allow to input text like "1."', () => {
const Demo = () => {
const [value, setValue] = React.useState<string | number>(1.1);
return (
<InputNumber
value={value}
onChange={(newValue) => {
setValue(newValue);
}}
/>
);
};
const wrapper = mount(<Demo />);
wrapper.focusInput();
wrapper.changeValue('1.');
expect(wrapper.getInputValue()).toEqual('1.');
wrapper.blurInput();
expect(wrapper.getInputValue()).toEqual('1');
});
// https://github.com/ant-design/ant-design/issues/5012
// https://github.com/react-component/input-number/issues/64
it('controller InputNumber should be able to input number like 1.00* and 1.10*', () => {
let num;
const Demo = () => {
const [value, setValue] = React.useState<string | number>(2);
return (
<InputNumber
value={value}
onChange={(newValue) => {
num = newValue;
setValue(newValue);
}}
/>
);
};
const wrapper = mount(<Demo />);
wrapper.focusInput();
wrapper.changeValue('6.0');
expect(wrapper.getInputValue()).toEqual('6.0');
expect(num).toEqual(6);
wrapper.blurInput();
expect(wrapper.getInputValue()).toEqual('6');
expect(num).toEqual(6);
wrapper.focusInput();
wrapper.changeValue('6.10');
expect(wrapper.getInputValue()).toEqual('6.10');
expect(num).toEqual(6.1);
wrapper.blurInput();
expect(wrapper.getInputValue()).toEqual('6.1');
expect(num).toEqual(6.1);
});
it('onChange should not be called when input is not changed', () => {
const onChange = jest.fn();
const onInput = jest.fn();
const wrapper = mount(<InputNumber onChange={onChange} onInput={onInput} />);
wrapper.focusInput();
wrapper.changeValue('1');
expect(onChange).toHaveBeenCalledTimes(1);
expect(onChange).toHaveBeenCalledWith(1);
expect(onInput).toHaveBeenCalledTimes(1);
expect(onInput).toHaveBeenCalledWith('1');
wrapper.blurInput();
expect(onChange).toHaveBeenCalledTimes(1);
expect(onInput).toHaveBeenCalledTimes(1);
wrapper.focusInput();
wrapper.changeValue('');
expect(onChange).toHaveBeenCalledTimes(2);
expect(onInput).toHaveBeenCalledTimes(2);
expect(onInput).toHaveBeenCalledWith('');
wrapper.blurInput();
expect(onChange).toHaveBeenCalledTimes(2);
expect(onInput).toHaveBeenCalledTimes(2);
expect(onChange).toHaveBeenLastCalledWith(null);
wrapper.focusInput();
wrapper.blurInput();
expect(onChange).toHaveBeenCalledTimes(2);
expect(onInput).toHaveBeenCalledTimes(2);
});
// https://github.com/ant-design/ant-design/issues/5235
it('input long number', () => {
const wrapper = mount(<InputNumber />);
wrapper.focusInput();
wrapper.changeValue('111111111111111111111');
expect(wrapper.getInputValue()).toEqual('111111111111111111111');
wrapper.changeValue('11111111111111111111111111111');
expect(wrapper.getInputValue()).toEqual('11111111111111111111111111111');
});
// https://github.com/ant-design/ant-design/issues/7363
it('uncontrolled input should trigger onChange always when blur it', () => {
const onChange = jest.fn();
const onInput = jest.fn();
const wrapper = mount(<InputNumber min={1} max={10} onChange={onChange} onInput={onInput} />);
wrapper.focusInput();
wrapper.changeValue('123');
expect(onChange).toHaveBeenCalledTimes(0);
expect(onInput).toHaveBeenCalledTimes(1);
expect(onInput).toHaveBeenCalledWith('123');
wrapper.blurInput();
expect(onChange).toHaveBeenCalledTimes(1);
expect(onChange).toHaveBeenCalledWith(10);
expect(onInput).toHaveBeenCalledTimes(1);
// repeat it, it should works in same way
wrapper.focusInput();
wrapper.changeValue('123');
expect(onChange).toHaveBeenCalledTimes(1);
expect(onInput).toHaveBeenCalledTimes(2);
expect(onInput).toHaveBeenCalledWith('123');
wrapper.blurInput();
expect(onChange).toHaveBeenCalledTimes(1);
expect(onInput).toHaveBeenCalledTimes(2);
});
// https://github.com/ant-design/ant-design/issues/30465
it('not block user input with min & max', () => {
const onChange = jest.fn();
const wrapper = mount(<InputNumber min={1900} onChange={onChange} />);
wrapper.focusInput();
wrapper.changeValue('2');
expect(onChange).not.toHaveBeenCalled();
wrapper.changeValue('20');
expect(onChange).not.toHaveBeenCalled();
wrapper.changeValue('200');
expect(onChange).not.toHaveBeenCalled();
wrapper.changeValue('2000');
expect(onChange).toHaveBeenCalledWith(2000);
onChange.mockRestore();
wrapper.changeValue('1');
expect(onChange).not.toHaveBeenCalled();
wrapper.blurInput();
expect(onChange).toHaveBeenCalledWith(1900);
});
// https://github.com/ant-design/ant-design/issues/7867
it('focus should not cut precision of input value', () => {
const Demo = () => {
const [value, setValue] = React.useState<string | number>(2);
return (
<InputNumber
value={value}
step={0.1}
onBlur={() => {
setValue(2);
}}
/>
);
};
const wrapper = mount(<Demo />);
wrapper.focusInput();
wrapper.blurInput();
expect(wrapper.getInputValue()).toEqual('2.0');
wrapper.focusInput();
expect(wrapper.getInputValue()).toEqual('2.0');
});
// https://github.com/ant-design/ant-design/issues/7940
it('should not format during input', () => {
const Demo = () => {
const [value, setValue] = React.useState<string | number>('');
return (
<InputNumber
value={value}
step={0.1}
onChange={(newValue) => {
setValue(newValue);
}}
/>
);
};
const wrapper = mount(<Demo />);
wrapper.focusInput();
wrapper.changeValue('1');
expect(wrapper.getInputValue()).toEqual('1');
});
// https://github.com/ant-design/ant-design/issues/8196
it('Allow input 。', () => {
const onChange = jest.fn();
const wrapper = mount(<InputNumber min={1} max={10} onChange={onChange} />);
wrapper.changeValue('8。1');
act(() => {
jest.runAllTimers();
wrapper.update();
});
wrapper.update();
expect(wrapper.getInputValue()).toEqual('8.1');
expect(onChange).toHaveBeenCalledWith(8.1);
});
// https://github.com/ant-design/ant-design/issues/25614
it("focus value should be '' when clear the input", () => {
let targetValue: string;
const wrapper = mount(
<InputNumber
min={1}
max={10}
onBlur={(e) => {
targetValue = e.target.value;
}}
value={1}
/>,
);
wrapper.focusInput();
wrapper.changeValue('');
wrapper.blurInput();
expect(targetValue).toEqual('');
});
it('should set input value as formatted when blur', () => {
let valueOnBlur: string;
const wrapper = mount(
<InputNumber
onBlur={(e) => {
valueOnBlur = e.target.value;
}}
formatter={(value) => `${Number(value) * 100}%`}
value={1}
/>,
);
wrapper.blurInput();
expect(wrapper.getInputValue()).toEqual('100%');
expect(valueOnBlur).toEqual('100%');
});
// https://github.com/ant-design/ant-design/issues/11574
// Origin: should trigger onChange when max or min change
it('warning UI when max or min change', () => {
const onChange = jest.fn();
const wrapper = mount(<InputNumber min={0} max={20} value={10} onChange={onChange} />);
expect(wrapper.exists('.rc-input-number-out-of-range')).toBeFalsy();
wrapper.setProps({ min: 11 });
wrapper.update();
expect(wrapper.getInputValue()).toEqual('10');
expect(wrapper.exists('.rc-input-number-out-of-range')).toBeTruthy();
expect(onChange).toHaveBeenCalledTimes(0);
wrapper.setProps({ value: 15 });
wrapper.setProps({ max: 14 });
wrapper.update();
expect(wrapper.getInputValue()).toEqual('15');
expect(wrapper.exists('.rc-input-number-out-of-range')).toBeTruthy();
expect(onChange).toHaveBeenCalledTimes(0);
});
// https://github.com/react-component/input-number/issues/120
it('should not reset value when parent re-render with the same `value` prop', () => {
const Demo = () => {
const [, forceUpdate] = React.useState({});
return (
<InputNumber
value={40}
onChange={() => {
forceUpdate({});
}}
/>
);
};
const wrapper = mount(<Demo />);
wrapper.focusInput();
wrapper.changeValue('401');
// Demo re-render and the `value` prop is still 40, but the user input should be retained
expect(wrapper.getInputValue()).toEqual('401');
});
// https://github.com/ant-design/ant-design/issues/16710
it('should use correct precision when change it to 0', () => {
const Demo = () => {
const [precision, setPrecision] = React.useState(2);
return (
<div>
<InputNumber
onChange={(newPrecision: number) => {
setPrecision(newPrecision);
}}
/>
<InputNumber precision={precision} defaultValue={1.23} />
</div>
);
};
const wrapper = mount(<Demo />);
wrapper
.find('input')
.last()
.simulate('keyDown')
.simulate('change', { target: { value: '1.23' } });
wrapper
.find('input')
.first()
.simulate('keyDown')
.simulate('change', { target: { value: '0' } });
expect(wrapper.find('input').last().props().value).toEqual('1');
});
// https://github.com/ant-design/ant-design/issues/30478
it('-0 should input able', () => {
const wrapper = mount(<InputNumber />);
wrapper.changeValue('-');
wrapper.changeValue('-0');
expect(wrapper.getInputValue()).toEqual('-0');
});
// https://github.com/ant-design/ant-design/issues/32274
it('global modify when typing', () => {
const Demo = ({ value }: { value?: number }) => {
const [val, setVal] = React.useState<string | number>(7);
React.useEffect(() => {
if (value) {
setVal(value);
}
}, [value]);
return <InputNumber value={val} onChange={setVal} />;
};
const wrapper = mount(<Demo />);
// Click
wrapper.find('.rc-input-number-handler-up').simulate('mouseDown');
expect(wrapper.find('input').prop('value')).toEqual('8');
// Keyboard change
wrapper.find('input').simulate('keyDown');
wrapper.setProps({ value: 3 });
wrapper.update();
expect(wrapper.find('input').prop('value')).toEqual('3');
});
});