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(() => {
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.useFakeTimers();
|
2019-03-26 14:47:50 +08:00
|
|
|
});
|
|
|
|
|
2022-09-05 19:41:32 +08:00
|
|
|
let container: HTMLDivElement;
|
2019-03-26 14:47:50 +08:00
|
|
|
beforeEach(() => {
|
|
|
|
container = document.createElement('div');
|
|
|
|
document.body.appendChild(container);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.useRealTimers();
|
2019-03-26 14:47:50 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
document.body.removeChild(container);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('focus() and onFocus', () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
const handleFocus = jest.fn();
|
2022-09-05 19:41:32 +08:00
|
|
|
const { container: wrapper } = render(<AutoComplete onFocus={handleFocus} />, { container });
|
|
|
|
wrapper.querySelector('input')?.focus();
|
2022-06-08 09:57:09 +08:00
|
|
|
act(() => {
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.runAllTimers();
|
2022-06-08 09:57:09 +08:00
|
|
|
});
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(handleFocus).toHaveBeenCalled();
|
2019-03-26 14:47:50 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('blur() and onBlur', () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
const handleBlur = jest.fn();
|
2022-09-05 19:41:32 +08:00
|
|
|
const { container: wrapper } = render(<AutoComplete onBlur={handleBlur} />, { container });
|
|
|
|
wrapper.querySelector('input')?.focus();
|
2022-06-08 09:57:09 +08:00
|
|
|
act(() => {
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.runAllTimers();
|
2022-06-08 09:57:09 +08:00
|
|
|
});
|
2022-09-05 19:41:32 +08:00
|
|
|
wrapper.querySelector('input')?.blur();
|
2022-06-08 09:57:09 +08:00
|
|
|
act(() => {
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.runAllTimers();
|
2022-06-08 09:57:09 +08:00
|
|
|
});
|
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', () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
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();
|
|
|
|
});
|
|
|
|
|
2023-02-23 21:56:43 +08:00
|
|
|
it('child.ref instance should support be focused and blurred', () => {
|
2022-09-05 19:41:32 +08:00
|
|
|
const inputRef = React.createRef<HTMLInputElement>();
|
2022-06-08 09:57:09 +08:00
|
|
|
render(
|
2019-08-09 14:31:36 +08:00
|
|
|
<AutoComplete dataSource={[]}>
|
2022-09-05 19:41:32 +08:00
|
|
|
<input ref={inputRef} />
|
2019-08-09 14:31:36 +08:00
|
|
|
</AutoComplete>,
|
|
|
|
);
|
2022-09-05 19:41:32 +08:00
|
|
|
expect(typeof inputRef.current?.focus).toBe('function');
|
|
|
|
expect(typeof inputRef.current?.blur).toBe('function');
|
2019-08-09 14:31:36 +08:00
|
|
|
});
|
2019-03-26 14:47:50 +08:00
|
|
|
});
|