2017-11-19 01:41:40 +08:00
|
|
|
import React from 'react';
|
|
|
|
import { mount } from 'enzyme';
|
2020-02-13 17:32:07 +08:00
|
|
|
import { sleep } 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
|
2020-04-22 15:58:57 +08:00
|
|
|
export default function focusTest(Component, { refFocus = false } = {}) {
|
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
|
|
|
});
|
|
|
|
|
|
|
|
let container;
|
|
|
|
beforeEach(() => {
|
|
|
|
container = document.createElement('div');
|
|
|
|
document.body.appendChild(container);
|
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(() => {
|
|
|
|
document.body.removeChild(container);
|
|
|
|
});
|
|
|
|
|
2020-04-22 15:58:57 +08:00
|
|
|
const getElement = wrapper => {
|
|
|
|
let ele = wrapper.find('input').first();
|
2020-04-26 22:33:36 +08:00
|
|
|
if (ele.length === 0) {
|
|
|
|
ele = wrapper.find('button').first();
|
|
|
|
}
|
2020-05-26 22:29:27 +08:00
|
|
|
if (ele.length === 0) {
|
|
|
|
ele = wrapper.find('textarea').first();
|
|
|
|
}
|
2020-04-22 15:58:57 +08:00
|
|
|
if (ele.length === 0) {
|
|
|
|
ele = wrapper.find('div[tabIndex]').first();
|
|
|
|
}
|
|
|
|
return ele;
|
|
|
|
};
|
|
|
|
|
2019-12-11 23:32:19 +08:00
|
|
|
if (refFocus) {
|
|
|
|
it('Ref: focus() and onFocus', () => {
|
|
|
|
const onFocus = jest.fn();
|
|
|
|
const ref = React.createRef();
|
|
|
|
const wrapper = mount(
|
|
|
|
<div>
|
|
|
|
<Component onFocus={onFocus} ref={ref} />
|
|
|
|
</div>,
|
|
|
|
);
|
|
|
|
ref.current.focus();
|
|
|
|
expect(focused).toBeTruthy();
|
2017-11-19 01:41:40 +08:00
|
|
|
|
2020-04-22 15:58:57 +08:00
|
|
|
getElement(wrapper).simulate('focus');
|
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();
|
|
|
|
const ref = React.createRef();
|
|
|
|
const wrapper = mount(
|
|
|
|
<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();
|
|
|
|
|
2020-04-22 15:58:57 +08:00
|
|
|
getElement(wrapper).simulate('blur');
|
2020-05-26 22:29:27 +08:00
|
|
|
await sleep(0);
|
2019-12-11 23:32:19 +08:00
|
|
|
expect(onBlur).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Ref: autoFocus', () => {
|
|
|
|
const onFocus = jest.fn();
|
|
|
|
const wrapper = mount(<Component autoFocus onFocus={onFocus} />);
|
|
|
|
|
|
|
|
expect(focused).toBeTruthy();
|
|
|
|
|
2020-04-22 15:58:57 +08:00
|
|
|
getElement(wrapper).simulate('focus');
|
2019-12-11 23:32:19 +08:00
|
|
|
expect(onFocus).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
it('focus() and onFocus', () => {
|
|
|
|
const handleFocus = jest.fn();
|
|
|
|
const wrapper = mount(<Component onFocus={handleFocus} />, { attachTo: container });
|
|
|
|
wrapper.instance().focus();
|
|
|
|
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();
|
|
|
|
const wrapper = mount(<Component onBlur={handleBlur} />, { attachTo: container });
|
|
|
|
wrapper.instance().focus();
|
2020-02-13 17:32:07 +08:00
|
|
|
await sleep(0);
|
2019-12-11 23:32:19 +08:00
|
|
|
wrapper.instance().blur();
|
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();
|
|
|
|
mount(<Component autoFocus onFocus={handleFocus} />, { attachTo: container });
|
|
|
|
expect(handleFocus).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
}
|
2017-11-19 01:41:40 +08:00
|
|
|
});
|
|
|
|
}
|