2020-12-29 22:42:04 +08:00
|
|
|
import { spyElementPrototypes } from 'rc-util/lib/test/domHook';
|
2022-06-22 14:57:09 +08:00
|
|
|
import React from 'react';
|
2020-12-29 22:42:04 +08:00
|
|
|
import Input from '..';
|
2022-06-22 14:57:09 +08:00
|
|
|
import { fireEvent, render } from '../../../tests/utils';
|
|
|
|
import type { InputRef } from '../Input';
|
2022-11-11 18:15:02 +08:00
|
|
|
import type { TextAreaRef } from '../TextArea';
|
2020-12-29 22:42:04 +08:00
|
|
|
|
|
|
|
const { TextArea } = Input;
|
|
|
|
|
|
|
|
describe('Input.Focus', () => {
|
|
|
|
let inputSpy: ReturnType<typeof spyElementPrototypes>;
|
|
|
|
let textareaSpy: ReturnType<typeof spyElementPrototypes>;
|
2023-06-07 21:59:21 +08:00
|
|
|
let focus: ReturnType<typeof jest.fn>;
|
|
|
|
let setSelectionRange: ReturnType<typeof jest.fn>;
|
2020-12-29 22:42:04 +08:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2023-06-07 21:59:21 +08:00
|
|
|
focus = jest.fn();
|
|
|
|
setSelectionRange = jest.fn();
|
2020-12-29 22:42:04 +08:00
|
|
|
inputSpy = spyElementPrototypes(HTMLInputElement, {
|
|
|
|
focus,
|
|
|
|
setSelectionRange,
|
|
|
|
});
|
|
|
|
textareaSpy = spyElementPrototypes(HTMLTextAreaElement, {
|
|
|
|
focus,
|
|
|
|
setSelectionRange,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
inputSpy.mockRestore();
|
|
|
|
textareaSpy.mockRestore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('start', () => {
|
2022-03-01 14:17:48 +08:00
|
|
|
const ref = React.createRef<InputRef>();
|
2022-06-02 16:28:22 +08:00
|
|
|
render(<TextArea ref={ref} defaultValue="light" />);
|
2020-12-29 22:42:04 +08:00
|
|
|
ref.current!.focus({ cursor: 'start' });
|
|
|
|
|
|
|
|
expect(focus).toHaveBeenCalled();
|
|
|
|
expect(setSelectionRange).toHaveBeenCalledWith(expect.anything(), 0, 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('end', () => {
|
2022-03-01 14:17:48 +08:00
|
|
|
const ref = React.createRef<InputRef>();
|
2022-06-02 16:28:22 +08:00
|
|
|
render(<TextArea ref={ref} defaultValue="light" />);
|
2020-12-29 22:42:04 +08:00
|
|
|
ref.current!.focus({ cursor: 'end' });
|
|
|
|
|
|
|
|
expect(focus).toHaveBeenCalled();
|
|
|
|
expect(setSelectionRange).toHaveBeenCalledWith(expect.anything(), 5, 5);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('all', () => {
|
2022-11-11 18:15:02 +08:00
|
|
|
const ref = React.createRef<TextAreaRef>();
|
2022-06-02 16:28:22 +08:00
|
|
|
render(<TextArea ref={ref} defaultValue="light" />);
|
2020-12-29 22:42:04 +08:00
|
|
|
ref.current!.focus({ cursor: 'all' });
|
|
|
|
|
|
|
|
expect(focus).toHaveBeenCalled();
|
|
|
|
expect(setSelectionRange).toHaveBeenCalledWith(expect.anything(), 0, 5);
|
|
|
|
});
|
2021-11-04 16:49:57 +08:00
|
|
|
|
|
|
|
it('disabled should reset focus', () => {
|
2022-04-18 21:02:11 +08:00
|
|
|
const { rerender, container } = render(<Input allowClear />);
|
|
|
|
fireEvent.focus(container.querySelector('input')!);
|
|
|
|
expect(container.querySelector('.ant-input-affix-wrapper-focused')).toBeTruthy();
|
2021-11-04 16:49:57 +08:00
|
|
|
|
2022-04-18 21:02:11 +08:00
|
|
|
rerender(<Input allowClear disabled />);
|
|
|
|
expect(container.querySelector('.ant-input-affix-wrapper-focused')).toBeFalsy();
|
2021-11-04 16:49:57 +08:00
|
|
|
});
|
2020-12-29 22:42:04 +08:00
|
|
|
});
|