-
-
Notifications
You must be signed in to change notification settings - Fork 185
Expand file tree
/
Copy pathvalidator.test.tsx
More file actions
84 lines (74 loc) · 2.55 KB
/
validator.test.tsx
File metadata and controls
84 lines (74 loc) · 2.55 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
import KeyCode from 'rc-util/lib/KeyCode';
import InputNumber from '../src';
import { fireEvent, render } from './util/wrapper';
describe('InputNumber.validator', () => {
it('validator on direct input', () => {
const onChange = jest.fn();
const { container } = render(
<InputNumber
defaultValue={0}
validator={(num) => {
return /^[0-9]*$/.test(num);
}}
onChange={onChange}
/>,
);
const input = container.querySelector('input');
fireEvent.focus(input);
fireEvent.change(input, { target: { value: 'a' } });
expect(input.value).toEqual('0');
fireEvent.change(input, { target: { value: '5' } });
expect(input.value).toEqual('5');
expect(onChange).toHaveBeenCalledWith(5);
fireEvent.change(input, { target: { value: '10e' } });
expect(input.value).toEqual('5');
fireEvent.change(input, { target: { value: '_' } });
expect(input.value).toEqual('5');
fireEvent.change(input, { target: { value: '10' } });
expect(input.value).toEqual('10');
expect(onChange).toHaveBeenCalledWith(10);
});
it('validator and formatter', () => {
const onChange = jest.fn();
const { container } = render(
<InputNumber
defaultValue={1}
formatter={(num) => `$ ${num} boeing 737`}
validator={(num) => {
return /^[0-9]*$/.test(num);
}}
onChange={onChange}
/>,
);
const input = container.querySelector('input');
fireEvent.focus(input);
expect(input.value).toEqual('$ 1 boeing 737');
fireEvent.change(input, { target: { value: '5' } });
expect(input.value).toEqual('$ 5 boeing 737');
fireEvent.keyDown(input, {
which: KeyCode.UP,
key: 'ArrowUp',
code: 'ArrowUp',
keyCode: KeyCode.UP,
});
expect(input.value).toEqual('$ 6 boeing 737');
expect(onChange).toHaveBeenLastCalledWith(6);
fireEvent.change(input, { target: { value: '#' } });
expect(input.value).toEqual('$ 6 boeing 737');
fireEvent.keyDown(input, {
which: KeyCode.DOWN,
key: 'ArrowDown',
code: 'ArrowDown',
keyCode: KeyCode.DOWN,
});
expect(input.value).toEqual('$ 5 boeing 737');
expect(onChange).toHaveBeenLastCalledWith(5);
fireEvent.mouseDown(container.querySelector('.rc-input-number-handler-up'), {
which: KeyCode.DOWN,
});
expect(input.value).toEqual('$ 6 boeing 737');
expect(onChange).toHaveBeenLastCalledWith(6);
fireEvent.change(input, { target: { value: 'a' } });
expect(input.value).toEqual('$ 6 boeing 737');
});
});