Skip to content

Commit

Permalink
refactor: focusTest to ts (ant-design#24489)
Browse files Browse the repository at this point in the history
  • Loading branch information
hengkx authored May 27, 2020
1 parent ab972ac commit 89d7101
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions tests/shared/focusTest.js → tests/shared/focusTest.tsx
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand All @@ -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();
Expand All @@ -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} />
Expand All @@ -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} />
Expand Down Expand Up @@ -99,17 +99,17 @@ 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();
});

it('blur() and onBlur', async () => {
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();
});
Expand Down

0 comments on commit 89d7101

Please sign in to comment.