ant-design/components/auto-complete/__tests__/focus.test.js

67 lines
1.7 KiB
JavaScript
Raw Normal View History

import React from 'react';
import { mount } from 'enzyme';
import AutoComplete from '..';
describe('AutoComplete children could be focus', () => {
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(<AutoComplete onFocus={handleFocus} />, { attachTo: container });
2020-10-21 10:35:06 +08:00
wrapper.find('input').instance().focus();
jest.runAllTimers();
expect(handleFocus).toHaveBeenCalled();
});
it('blur() and onBlur', () => {
const handleBlur = jest.fn();
const wrapper = mount(<AutoComplete onBlur={handleBlur} />, { attachTo: container });
2020-10-21 10:35:06 +08:00
wrapper.find('input').instance().focus();
jest.runAllTimers();
2020-10-21 10:35:06 +08:00
wrapper.find('input').instance().blur();
jest.runAllTimers();
expect(handleBlur).toHaveBeenCalled();
});
it('child.ref should work', () => {
const mockRef = jest.fn();
mount(
<AutoComplete dataSource={[]}>
<input ref={mockRef} />
</AutoComplete>,
);
expect(mockRef).toHaveBeenCalled();
});
it('child.ref instance should support be focused and blured', () => {
let inputRef;
mount(
<AutoComplete dataSource={[]}>
2019-08-12 13:34:23 +08:00
<input
ref={node => {
inputRef = node;
}}
/>
</AutoComplete>,
);
expect(typeof inputRef.focus).toBe('function');
expect(typeof inputRef.blur).toBe('function');
});
});