-
-
Notifications
You must be signed in to change notification settings - Fork 243
Expand file tree
/
Copy pathref.test.tsx
More file actions
85 lines (67 loc) · 2.15 KB
/
ref.test.tsx
File metadata and controls
85 lines (67 loc) · 2.15 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
84
85
/* eslint-disable max-classes-per-file */
import { cleanup, render } from '@testing-library/react';
import { spyElementPrototypes } from '@rc-component/util/lib/test/domHook';
import React from 'react';
import Trigger, { type TriggerRef } from '../src';
describe('Trigger.Ref', () => {
beforeAll(() => {
spyElementPrototypes(HTMLElement, {
offsetParent: {
get: () => document.body,
},
});
});
beforeEach(() => {
jest.useFakeTimers();
});
afterEach(() => {
cleanup();
jest.useRealTimers();
});
it('support nativeElement', () => {
const triggerRef = React.createRef<TriggerRef>();
const { container } = render(
<Trigger ref={triggerRef} popup={<div />}>
<button />
</Trigger>,
);
expect(triggerRef.current.nativeElement).toBe(
container.querySelector('button'),
);
});
it('support popupElement', () => {
const triggerRef = React.createRef<TriggerRef>();
render(
<Trigger ref={triggerRef} popupVisible popup={<div />}>
<button />
</Trigger>,
);
expect(triggerRef.current.popupElement).toBe(
document.querySelector('.rc-trigger-popup'),
);
});
it('should support ref.nativeElement', () => {
const ChildComponent = React.forwardRef<
{ nativeElement: HTMLElement },
React.PropsWithChildren
>((props, ref) => {
const buttonRef = React.useRef<HTMLButtonElement>(null);
React.useImperativeHandle(ref, () => ({
nativeElement: buttonRef.current,
}));
return <button ref={buttonRef} {...props} />;
});
const triggerRef = React.createRef<TriggerRef>();
const childRef = React.createRef<{ nativeElement: HTMLElement }>();
const { container } = render(
<Trigger ref={triggerRef} popup={<div />}>
<ChildComponent ref={childRef} />
</Trigger>,
);
const buttonElement = container.querySelector('button');
// Check child ref returns object with nativeElement
expect(childRef.current.nativeElement).toBe(buttonElement);
// Check Trigger can extract DOM from child ref
expect(triggerRef.current.nativeElement).toBe(buttonElement);
});
});