mirror of
https://github.com/ant-design/ant-design.git
synced 2025-06-11 19:42:54 +08:00
refactor: focusTest to ts (#24489)
This commit is contained in:
parent
ab972ac1eb
commit
89d7101e6e
@ -1,9 +1,9 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { mount } from 'enzyme';
|
import { mount, ReactWrapper } from 'enzyme';
|
||||||
import { sleep } from '../utils';
|
import { sleep } from '../utils';
|
||||||
|
|
||||||
// eslint-disable-next-line jest/no-export
|
// 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', () => {
|
describe('focus and blur', () => {
|
||||||
let focused = false;
|
let focused = false;
|
||||||
let blurred = false;
|
let blurred = false;
|
||||||
@ -21,7 +21,7 @@ export default function focusTest(Component, { refFocus = false } = {}) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
let container;
|
let container: HTMLElement;
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
container = document.createElement('div');
|
container = document.createElement('div');
|
||||||
document.body.appendChild(container);
|
document.body.appendChild(container);
|
||||||
@ -38,7 +38,7 @@ export default function focusTest(Component, { refFocus = false } = {}) {
|
|||||||
document.body.removeChild(container);
|
document.body.removeChild(container);
|
||||||
});
|
});
|
||||||
|
|
||||||
const getElement = wrapper => {
|
const getElement = (wrapper: ReactWrapper) => {
|
||||||
let ele = wrapper.find('input').first();
|
let ele = wrapper.find('input').first();
|
||||||
if (ele.length === 0) {
|
if (ele.length === 0) {
|
||||||
ele = wrapper.find('button').first();
|
ele = wrapper.find('button').first();
|
||||||
@ -55,7 +55,7 @@ export default function focusTest(Component, { refFocus = false } = {}) {
|
|||||||
if (refFocus) {
|
if (refFocus) {
|
||||||
it('Ref: focus() and onFocus', () => {
|
it('Ref: focus() and onFocus', () => {
|
||||||
const onFocus = jest.fn();
|
const onFocus = jest.fn();
|
||||||
const ref = React.createRef();
|
const ref = React.createRef<any>();
|
||||||
const wrapper = mount(
|
const wrapper = mount(
|
||||||
<div>
|
<div>
|
||||||
<Component onFocus={onFocus} ref={ref} />
|
<Component onFocus={onFocus} ref={ref} />
|
||||||
@ -71,7 +71,7 @@ export default function focusTest(Component, { refFocus = false } = {}) {
|
|||||||
it('Ref: blur() and onBlur', async () => {
|
it('Ref: blur() and onBlur', async () => {
|
||||||
jest.useRealTimers();
|
jest.useRealTimers();
|
||||||
const onBlur = jest.fn();
|
const onBlur = jest.fn();
|
||||||
const ref = React.createRef();
|
const ref = React.createRef<any>();
|
||||||
const wrapper = mount(
|
const wrapper = mount(
|
||||||
<div>
|
<div>
|
||||||
<Component onBlur={onBlur} ref={ref} />
|
<Component onBlur={onBlur} ref={ref} />
|
||||||
@ -99,7 +99,7 @@ export default function focusTest(Component, { refFocus = false } = {}) {
|
|||||||
it('focus() and onFocus', () => {
|
it('focus() and onFocus', () => {
|
||||||
const handleFocus = jest.fn();
|
const handleFocus = jest.fn();
|
||||||
const wrapper = mount(<Component onFocus={handleFocus} />, { attachTo: container });
|
const wrapper = mount(<Component onFocus={handleFocus} />, { attachTo: container });
|
||||||
wrapper.instance().focus();
|
(wrapper.instance() as any).focus();
|
||||||
expect(handleFocus).toHaveBeenCalled();
|
expect(handleFocus).toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -107,9 +107,9 @@ export default function focusTest(Component, { refFocus = false } = {}) {
|
|||||||
jest.useRealTimers();
|
jest.useRealTimers();
|
||||||
const handleBlur = jest.fn();
|
const handleBlur = jest.fn();
|
||||||
const wrapper = mount(<Component onBlur={handleBlur} />, { attachTo: container });
|
const wrapper = mount(<Component onBlur={handleBlur} />, { attachTo: container });
|
||||||
wrapper.instance().focus();
|
(wrapper.instance() as any).focus();
|
||||||
await sleep(0);
|
await sleep(0);
|
||||||
wrapper.instance().blur();
|
(wrapper.instance() as any).blur();
|
||||||
await sleep(0);
|
await sleep(0);
|
||||||
expect(handleBlur).toHaveBeenCalled();
|
expect(handleBlur).toHaveBeenCalled();
|
||||||
});
|
});
|
Loading…
Reference in New Issue
Block a user