import React from 'react'; import { mount } from 'enzyme'; import { sleep } from '../utils'; // eslint-disable-next-line jest/no-export export default function focusTest(Component, { refFocus = false } = {}) { describe('focus and blur', () => { let focused = false; let blurred = false; const mockFocus = jest.spyOn(HTMLElement.prototype, 'focus'); const mockBlur = jest.spyOn(HTMLElement.prototype, 'blur'); beforeAll(() => { if (refFocus) { mockFocus.mockImplementation(() => { focused = true; }); mockBlur.mockImplementation(() => { blurred = true; }); } }); let container; beforeEach(() => { container = document.createElement('div'); document.body.appendChild(container); focused = false; blurred = false; }); afterAll(() => { mockFocus.mockRestore(); mockBlur.mockRestore(); }); afterEach(() => { document.body.removeChild(container); }); const getElement = wrapper => { let ele = wrapper.find('input').first(); if (ele.length === 0) { ele = wrapper.find('button').first(); } if (ele.length === 0) { ele = wrapper.find('div[tabIndex]').first(); } return ele; }; if (refFocus) { it('Ref: focus() and onFocus', () => { const onFocus = jest.fn(); const ref = React.createRef(); const wrapper = mount(