-
-
Notifications
You must be signed in to change notification settings - Fork 489
Expand file tree
/
Copy pathCustom.test.tsx
More file actions
83 lines (68 loc) · 2.41 KB
/
Custom.test.tsx
File metadata and controls
83 lines (68 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import * as React from 'react';
import Select from '../src';
import { injectRunAllTimers, waitFakeTimer } from './utils/common';
import { fireEvent, render } from '@testing-library/react';
describe('Select.Custom', () => {
injectRunAllTimers(jest);
beforeEach(() => {
jest.useFakeTimers();
});
afterEach(() => {
jest.useRealTimers();
});
it('getRawInputElement', async () => {
const onPopupVisibleChange = jest.fn();
const { container } = render(
<Select
getRawInputElement={() => <span className="custom" />}
onPopupVisibleChange={onPopupVisibleChange}
/>,
);
fireEvent.click(container.querySelector('.custom'));
await waitFakeTimer();
expect(onPopupVisibleChange).toHaveBeenCalledWith(true);
});
it('should not override raw input element event handlers', () => {
const onFocus = jest.fn();
const onBlur = jest.fn();
const { getByPlaceholderText } = render(
<Select
showSearch
options={[{ value: 'a', label: 'A' }]}
getRawInputElement={() => (
<input placeholder="focus me" onFocus={onFocus} onBlur={onBlur} />
)}
/>,
);
fireEvent.focus(getByPlaceholderText('focus me'));
fireEvent.blur(getByPlaceholderText('focus me'));
expect(onFocus).toHaveBeenCalled();
expect(onBlur).toHaveBeenCalled();
});
it('should handle nested nativeElement structure correctly', () => {
// Mock component that returns nativeElement structure (similar to antd Input)
const CustomInputWithNativeElement = React.forwardRef<
{ nativeElement: HTMLInputElement },
React.InputHTMLAttributes<HTMLInputElement>
>((props, ref) => {
const inputRef = React.useRef<HTMLInputElement>(null);
React.useImperativeHandle(ref, () => ({
nativeElement: inputRef.current!,
focus: () => inputRef.current?.focus(),
blur: () => inputRef.current?.blur(),
}));
return <input ref={inputRef} {...props} />;
});
const selectRef = React.createRef<any>();
render(
<Select
ref={selectRef}
getRawInputElement={() => <CustomInputWithNativeElement className="custom-input" />}
/>,
);
// The nativeElement should be a DOM element, not a nested object
const { nativeElement } = selectRef.current;
expect(nativeElement).toBeInstanceOf(HTMLInputElement);
expect(nativeElement.className).toBe('custom-input');
});
});