2019-03-26 14:47:50 +08:00
|
|
|
import React from 'react';
|
2022-06-08 09:57:09 +08:00
|
|
|
import { act } from 'react-dom/test-utils';
|
2019-03-26 14:47:50 +08:00
|
|
|
import AutoComplete from '..';
|
2022-06-08 09:57:09 +08:00
|
|
|
import { render } from '../../../tests/utils';
|
2019-03-26 14:47:50 +08:00
|
|
|
|
|
|
|
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();
|
2022-06-08 09:57:09 +08:00
|
|
|
const { container: wrapper } = render(<AutoComplete onFocus={handleFocus} />, {
|
|
|
|
attachTo: container,
|
|
|
|
});
|
|
|
|
wrapper.querySelector('input').focus();
|
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(handleFocus).toHaveBeenCalled();
|
2019-03-26 14:47:50 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('blur() and onBlur', () => {
|
|
|
|
const handleBlur = jest.fn();
|
2022-06-08 09:57:09 +08:00
|
|
|
const { container: wrapper } = render(<AutoComplete onBlur={handleBlur} />, {
|
|
|
|
attachTo: container,
|
|
|
|
});
|
|
|
|
wrapper.querySelector('input').focus();
|
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
|
|
|
wrapper.querySelector('input').blur();
|
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(handleBlur).toHaveBeenCalled();
|
2019-03-26 14:47:50 +08:00
|
|
|
});
|
2019-08-09 14:31:36 +08:00
|
|
|
|
|
|
|
it('child.ref should work', () => {
|
|
|
|
const mockRef = jest.fn();
|
2022-06-08 09:57:09 +08:00
|
|
|
render(
|
2019-08-09 14:31:36 +08:00
|
|
|
<AutoComplete dataSource={[]}>
|
|
|
|
<input ref={mockRef} />
|
|
|
|
</AutoComplete>,
|
|
|
|
);
|
|
|
|
expect(mockRef).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('child.ref instance should support be focused and blured', () => {
|
|
|
|
let inputRef;
|
2022-06-08 09:57:09 +08:00
|
|
|
render(
|
2019-08-09 14:31:36 +08:00
|
|
|
<AutoComplete dataSource={[]}>
|
2019-08-12 13:34:23 +08:00
|
|
|
<input
|
|
|
|
ref={node => {
|
|
|
|
inputRef = node;
|
|
|
|
}}
|
|
|
|
/>
|
2019-08-09 14:31:36 +08:00
|
|
|
</AutoComplete>,
|
|
|
|
);
|
|
|
|
expect(typeof inputRef.focus).toBe('function');
|
|
|
|
expect(typeof inputRef.blur).toBe('function');
|
|
|
|
});
|
2019-03-26 14:47:50 +08:00
|
|
|
});
|