refactor: focusTest to ts (#24489)

This commit is contained in:
Tom Xu 2020-05-27 12:06:50 +08:00 committed by GitHub
parent ab972ac1eb
commit 89d7101e6e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,9 +1,9 @@
import React from 'react';
import { mount } from 'enzyme';
import { mount, ReactWrapper } from 'enzyme';
import { sleep } from '../utils';
// eslint-disable-next-line jest/no-export
export default function focusTest(Component, { refFocus = false } = {}) {
export default function focusTest(Component: React.ComponentType<any>, { refFocus = false } = {}) {
describe('focus and blur', () => {
let focused = false;
let blurred = false;
@ -21,7 +21,7 @@ export default function focusTest(Component, { refFocus = false } = {}) {
}
});
let container;
let container: HTMLElement;
beforeEach(() => {
container = document.createElement('div');
document.body.appendChild(container);
@ -38,7 +38,7 @@ export default function focusTest(Component, { refFocus = false } = {}) {
document.body.removeChild(container);
});
const getElement = wrapper => {
const getElement = (wrapper: ReactWrapper) => {
let ele = wrapper.find('input').first();
if (ele.length === 0) {
ele = wrapper.find('button').first();
@ -55,7 +55,7 @@ export default function focusTest(Component, { refFocus = false } = {}) {
if (refFocus) {
it('Ref: focus() and onFocus', () => {
const onFocus = jest.fn();
const ref = React.createRef();
const ref = React.createRef<any>();
const wrapper = mount(
<div>
<Component onFocus={onFocus} ref={ref} />
@ -71,7 +71,7 @@ export default function focusTest(Component, { refFocus = false } = {}) {
it('Ref: blur() and onBlur', async () => {
jest.useRealTimers();
const onBlur = jest.fn();
const ref = React.createRef();
const ref = React.createRef<any>();
const wrapper = mount(
<div>
<Component onBlur={onBlur} ref={ref} />
@ -99,7 +99,7 @@ export default function focusTest(Component, { refFocus = false } = {}) {
it('focus() and onFocus', () => {
const handleFocus = jest.fn();
const wrapper = mount(<Component onFocus={handleFocus} />, { attachTo: container });
wrapper.instance().focus();
(wrapper.instance() as any).focus();
expect(handleFocus).toHaveBeenCalled();
});
@ -107,9 +107,9 @@ export default function focusTest(Component, { refFocus = false } = {}) {
jest.useRealTimers();
const handleBlur = jest.fn();
const wrapper = mount(<Component onBlur={handleBlur} />, { attachTo: container });
wrapper.instance().focus();
(wrapper.instance() as any).focus();
await sleep(0);
wrapper.instance().blur();
(wrapper.instance() as any).blur();
await sleep(0);
expect(handleBlur).toHaveBeenCalled();
});