2017-11-19 01:41:40 +08:00
|
|
|
import React from 'react';
|
|
|
|
import { mount } from 'enzyme';
|
|
|
|
|
2019-11-05 00:19:02 +08:00
|
|
|
// eslint-disable-next-line jest/no-export
|
2017-11-19 01:41:40 +08:00
|
|
|
export default function focusTest(Component) {
|
|
|
|
describe('focus and blur', () => {
|
|
|
|
beforeAll(() => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
let container;
|
|
|
|
beforeEach(() => {
|
|
|
|
container = document.createElement('div');
|
|
|
|
document.body.appendChild(container);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
document.body.removeChild(container);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('focus() and onFocus', () => {
|
|
|
|
const handleFocus = jest.fn();
|
|
|
|
const wrapper = mount(<Component onFocus={handleFocus} />, { attachTo: container });
|
|
|
|
wrapper.instance().focus();
|
|
|
|
jest.runAllTimers();
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(handleFocus).toHaveBeenCalled();
|
2017-11-19 01:41:40 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('blur() and onBlur', () => {
|
|
|
|
const handleBlur = jest.fn();
|
|
|
|
const wrapper = mount(<Component onBlur={handleBlur} />, { attachTo: container });
|
|
|
|
wrapper.instance().focus();
|
|
|
|
jest.runAllTimers();
|
|
|
|
wrapper.instance().blur();
|
|
|
|
jest.runAllTimers();
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(handleBlur).toHaveBeenCalled();
|
2017-11-19 01:41:40 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('autoFocus', () => {
|
|
|
|
const handleFocus = jest.fn();
|
|
|
|
mount(<Component autoFocus onFocus={handleFocus} />, { attachTo: container });
|
|
|
|
jest.runAllTimers();
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(handleFocus).toHaveBeenCalled();
|
2017-11-19 01:41:40 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|