2017-11-19 01:41:40 +08:00
|
|
|
import React from 'react';
|
2023-01-29 12:30:46 +08:00
|
|
|
import { fireEvent, render, sleep } from '../utils';
|
2017-11-19 01:41:40 +08:00
|
|
|
|
2023-01-29 12:30:46 +08:00
|
|
|
const focusTest = (
|
2022-03-01 14:48:41 +08:00
|
|
|
Component: React.ComponentType<any>,
|
2023-01-29 12:30:46 +08:00
|
|
|
{ refFocus = false, blurDelay = 0 } = {},
|
|
|
|
) => {
|
2017-11-19 01:41:40 +08:00
|
|
|
describe('focus and blur', () => {
|
2019-12-11 23:32:19 +08:00
|
|
|
let focused = false;
|
|
|
|
let blurred = false;
|
2023-06-07 21:59:21 +08:00
|
|
|
const mockFocus = jest.spyOn(HTMLElement.prototype, 'focus');
|
|
|
|
const mockBlur = jest.spyOn(HTMLElement.prototype, 'blur');
|
2019-12-11 23:32:19 +08:00
|
|
|
|
2017-11-19 01:41:40 +08:00
|
|
|
beforeAll(() => {
|
2019-12-11 23:32:19 +08:00
|
|
|
if (refFocus) {
|
2020-04-22 11:59:56 +08:00
|
|
|
mockFocus.mockImplementation(() => {
|
|
|
|
focused = true;
|
|
|
|
});
|
|
|
|
mockBlur.mockImplementation(() => {
|
|
|
|
blurred = true;
|
2019-12-11 23:32:19 +08:00
|
|
|
});
|
|
|
|
}
|
2017-11-19 01:41:40 +08:00
|
|
|
});
|
|
|
|
|
2023-01-29 12:30:46 +08:00
|
|
|
let containerHtml: HTMLDivElement;
|
2017-11-19 01:41:40 +08:00
|
|
|
beforeEach(() => {
|
2022-08-30 10:09:37 +08:00
|
|
|
containerHtml = document.createElement('div');
|
|
|
|
document.body.appendChild(containerHtml);
|
2019-12-11 23:32:19 +08:00
|
|
|
focused = false;
|
|
|
|
blurred = false;
|
2017-11-19 01:41:40 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
2020-04-22 11:59:56 +08:00
|
|
|
mockFocus.mockRestore();
|
|
|
|
mockBlur.mockRestore();
|
2017-11-19 01:41:40 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
2022-08-30 10:09:37 +08:00
|
|
|
document.body.removeChild(containerHtml);
|
2017-11-19 01:41:40 +08:00
|
|
|
});
|
|
|
|
|
2023-01-29 12:30:46 +08:00
|
|
|
const getElement = (container: HTMLElement) =>
|
2022-08-30 10:09:37 +08:00
|
|
|
container.querySelector('input') ||
|
|
|
|
container.querySelector('button') ||
|
|
|
|
container.querySelector('textarea') ||
|
|
|
|
container.querySelector('div[tabIndex]');
|
2020-04-22 15:58:57 +08:00
|
|
|
|
2019-12-11 23:32:19 +08:00
|
|
|
if (refFocus) {
|
|
|
|
it('Ref: focus() and onFocus', () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
const onFocus = jest.fn();
|
2020-05-27 12:06:50 +08:00
|
|
|
const ref = React.createRef<any>();
|
2022-08-30 10:09:37 +08:00
|
|
|
const { container } = render(
|
2019-12-11 23:32:19 +08:00
|
|
|
<div>
|
|
|
|
<Component onFocus={onFocus} ref={ref} />
|
|
|
|
</div>,
|
|
|
|
);
|
|
|
|
ref.current.focus();
|
|
|
|
expect(focused).toBeTruthy();
|
2017-11-19 01:41:40 +08:00
|
|
|
|
2023-01-29 12:30:46 +08:00
|
|
|
fireEvent.focus(getElement(container)!);
|
2019-12-11 23:32:19 +08:00
|
|
|
expect(onFocus).toHaveBeenCalled();
|
|
|
|
});
|
2017-11-19 01:41:40 +08:00
|
|
|
|
2020-05-26 22:29:27 +08:00
|
|
|
it('Ref: blur() and onBlur', async () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.useRealTimers();
|
|
|
|
const onBlur = jest.fn();
|
2020-05-27 12:06:50 +08:00
|
|
|
const ref = React.createRef<any>();
|
2022-08-30 10:09:37 +08:00
|
|
|
const { container } = render(
|
2019-12-11 23:32:19 +08:00
|
|
|
<div>
|
|
|
|
<Component onBlur={onBlur} ref={ref} />
|
|
|
|
</div>,
|
|
|
|
);
|
2020-05-26 22:29:27 +08:00
|
|
|
|
2019-12-11 23:32:19 +08:00
|
|
|
ref.current.blur();
|
|
|
|
expect(blurred).toBeTruthy();
|
|
|
|
|
2023-01-29 12:30:46 +08:00
|
|
|
fireEvent.blur(getElement(container)!);
|
2022-11-12 16:32:41 +08:00
|
|
|
await sleep(blurDelay);
|
2019-12-11 23:32:19 +08:00
|
|
|
expect(onBlur).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Ref: autoFocus', () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
const onFocus = jest.fn();
|
2022-08-30 10:09:37 +08:00
|
|
|
const { container } = render(<Component autoFocus onFocus={onFocus} />);
|
2019-12-11 23:32:19 +08:00
|
|
|
|
|
|
|
expect(focused).toBeTruthy();
|
|
|
|
|
2023-01-29 12:30:46 +08:00
|
|
|
fireEvent.focus(getElement(container)!);
|
2019-12-11 23:32:19 +08:00
|
|
|
expect(onFocus).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
it('focus() and onFocus', () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
const handleFocus = jest.fn();
|
2022-08-30 10:09:37 +08:00
|
|
|
const { container } = render(<Component onFocus={handleFocus} />);
|
2023-01-29 12:30:46 +08:00
|
|
|
fireEvent.focus(getElement(container)!);
|
2019-12-11 23:32:19 +08:00
|
|
|
expect(handleFocus).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2020-02-13 17:32:07 +08:00
|
|
|
it('blur() and onBlur', async () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.useRealTimers();
|
|
|
|
const handleBlur = jest.fn();
|
2022-08-30 10:09:37 +08:00
|
|
|
const { container } = render(<Component onBlur={handleBlur} />);
|
2023-01-29 12:30:46 +08:00
|
|
|
fireEvent.focus(getElement(container)!);
|
2020-02-13 17:32:07 +08:00
|
|
|
await sleep(0);
|
2023-01-29 12:30:46 +08:00
|
|
|
fireEvent.blur(getElement(container)!);
|
2020-02-13 17:32:07 +08:00
|
|
|
await sleep(0);
|
2019-12-11 23:32:19 +08:00
|
|
|
expect(handleBlur).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('autoFocus', () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
const handleFocus = jest.fn();
|
2022-08-30 10:09:37 +08:00
|
|
|
render(<Component autoFocus onFocus={handleFocus} />);
|
2019-12-11 23:32:19 +08:00
|
|
|
expect(handleFocus).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
}
|
2017-11-19 01:41:40 +08:00
|
|
|
});
|
2023-01-29 12:30:46 +08:00
|
|
|
};
|
|
|
|
|
2023-06-07 21:59:21 +08:00
|
|
|
// eslint-disable-next-line jest/no-export
|
2023-01-29 12:30:46 +08:00
|
|
|
export default focusTest;
|