Skip to content

Commit 96f7155

Browse files
committed
test: add test case
1 parent 1294284 commit 96f7155

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

tests/focus.test.tsx

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { fireEvent, render } from '@testing-library/react';
2+
import InputNumber, { InputNumberRef } from 'rc-input-number';
3+
import { spyElementPrototypes } from 'rc-util/lib/test/domHook';
4+
import React from 'react';
5+
6+
const getInputRef = () => {
7+
const ref = React.createRef<InputNumberRef>();
8+
render(<InputNumber ref={ref} defaultValue={12345} />);
9+
return ref;
10+
};
11+
12+
describe('InputNumber.Focus', () => {
13+
let inputSpy: ReturnType<typeof spyElementPrototypes>;
14+
let focus: ReturnType<typeof jest.fn>;
15+
let setSelectionRange: ReturnType<typeof jest.fn>;
16+
17+
beforeEach(() => {
18+
focus = jest.fn();
19+
setSelectionRange = jest.fn();
20+
inputSpy = spyElementPrototypes(HTMLInputElement, {
21+
focus,
22+
setSelectionRange,
23+
});
24+
});
25+
26+
afterEach(() => {
27+
inputSpy.mockRestore();
28+
});
29+
30+
it('start', () => {
31+
const input = getInputRef();
32+
input.current?.focus({ cursor: 'start' });
33+
34+
expect(focus).toHaveBeenCalled();
35+
expect(setSelectionRange).toHaveBeenCalledWith(expect.anything(), 0, 0);
36+
});
37+
38+
it('end', () => {
39+
const input = getInputRef();
40+
input.current?.focus({ cursor: 'end' });
41+
42+
expect(focus).toHaveBeenCalled();
43+
expect(setSelectionRange).toHaveBeenCalledWith(expect.anything(), 5, 5);
44+
});
45+
46+
it('all', () => {
47+
const input = getInputRef();
48+
input.current?.focus({ cursor: 'all' });
49+
50+
expect(focus).toHaveBeenCalled();
51+
expect(setSelectionRange).toHaveBeenCalledWith(expect.anything(), 0, 5);
52+
});
53+
54+
it('disabled should reset focus', () => {
55+
const { container, rerender } = render(<InputNumber prefixCls="rc-input-number" />);
56+
const input = container.querySelector('input')!;
57+
58+
fireEvent.focus(input);
59+
expect(container.querySelector('.rc-input-number-focused')).toBeTruthy();
60+
61+
rerender(<InputNumber prefixCls="rc-input-number" disabled />);
62+
fireEvent.blur(input);
63+
64+
expect(container.querySelector('.rc-input-number-focused')).toBeFalsy();
65+
});
66+
});

tests/input.test.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,16 @@ describe('InputNumber.Input', () => {
224224
expect(onChange).not.toHaveBeenCalled();
225225
});
226226

227+
it('input should work', () => {
228+
const ref = React.createRef<InputNumberRef>();
229+
const { container } = render(<InputNumber ref={ref} />);
230+
const inputEl = container.querySelector('input')!;
231+
const rootEl = container.querySelector('.rc-input-number')!;
232+
233+
expect(ref.current?.input).toBe(inputEl);
234+
expect(ref.current?.nativeElement).toBe(rootEl);
235+
});
236+
227237
describe('nativeElement', () => {
228238
it('basic', () => {
229239
const ref = React.createRef<InputNumberRef>();

0 commit comments

Comments
 (0)