2017-11-19 01:41:40 +08:00
|
|
|
import React from 'react';
|
2022-08-30 10:09:37 +08:00
|
|
|
import { sleep, render, fireEvent } from '../utils';
|
2017-11-19 01:41:40 +08:00
|
|
|
|
2019-11-05 00:19:02 +08:00
|
|
|
// eslint-disable-next-line jest/no-export
|
2022-03-01 14:48:41 +08:00
|
|
|
export default function focusTest(
|
|
|
|
Component: React.ComponentType<any>,
|
2022-11-12 16:32:41 +08:00
|
|
|
{ refFocus = false, testLib = false, blurDelay = 0 } = {},
|
2022-03-01 14:48:41 +08:00
|
|
|
) {
|
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;
|
2020-04-22 11:59:56 +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
|
|
|
});
|
|
|
|
|
2022-03-01 14:48:41 +08:00
|
|
|
// ==================== React Test Lib ====================
|
|
|
|
if (testLib) {
|
|
|
|
it('Test: focus() and onFocus', () => {
|
|
|
|
const handleFocus = jest.fn();
|
|
|
|
const ref = React.createRef<any>();
|
|
|
|
const { unmount } = render(<Component onFocus={handleFocus} ref={ref} />);
|
|
|
|
ref.current.focus();
|
|
|
|
expect(handleFocus).toHaveBeenCalled();
|
|
|
|
|
|
|
|
unmount();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Test: blur() and onBlur', async () => {
|
|
|
|
const handleBlur = jest.fn();
|
|
|
|
const ref = React.createRef<any>();
|
|
|
|
const { unmount } = render(<Component ref={ref} onBlur={handleBlur} />);
|
|
|
|
ref.current.focus();
|
|
|
|
ref.current.blur();
|
|
|
|
expect(handleBlur).toHaveBeenCalled();
|
|
|
|
|
|
|
|
unmount();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Test: autoFocus', () => {
|
|
|
|
const handleFocus = jest.fn();
|
|
|
|
const { unmount } = render(<Component autoFocus onFocus={handleFocus} />);
|
|
|
|
expect(handleFocus).toHaveBeenCalled();
|
|
|
|
|
|
|
|
unmount();
|
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-30 10:09:37 +08:00
|
|
|
let containerHtml: HTMLElement;
|
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
|
|
|
});
|
|
|
|
|
2022-08-30 10:09:37 +08:00
|
|
|
const getElement = (container: { querySelector: Function }) =>
|
|
|
|
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', () => {
|
|
|
|
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
|
|
|
|
2022-08-30 10:09:37 +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 () => {
|
|
|
|
jest.useRealTimers();
|
2019-12-11 23:32:19 +08:00
|
|
|
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();
|
|
|
|
|
2022-08-30 10:09:37 +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', () => {
|
|
|
|
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();
|
|
|
|
|
2022-08-30 10:09:37 +08:00
|
|
|
fireEvent.focus(getElement(container));
|
2019-12-11 23:32:19 +08:00
|
|
|
expect(onFocus).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
it('focus() and onFocus', () => {
|
|
|
|
const handleFocus = jest.fn();
|
2022-08-30 10:09:37 +08:00
|
|
|
const { container } = render(<Component onFocus={handleFocus} />);
|
|
|
|
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 () => {
|
|
|
|
jest.useRealTimers();
|
2019-12-11 23:32:19 +08:00
|
|
|
const handleBlur = jest.fn();
|
2022-08-30 10:09:37 +08:00
|
|
|
const { container } = render(<Component onBlur={handleBlur} />);
|
|
|
|
fireEvent.focus(getElement(container));
|
2020-02-13 17:32:07 +08:00
|
|
|
await sleep(0);
|
2022-08-30 10:09:37 +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', () => {
|
|
|
|
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
|
|
|
});
|
|
|
|
}
|