forked from microsoft/fluentui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseMergedRefs.test.tsx
65 lines (49 loc) · 1.93 KB
/
useMergedRefs.test.tsx
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
import * as React from 'react';
import { mount } from 'enzyme';
import { useMergedRefs } from './useMergedRefs';
describe('useMergedRefs', () => {
it('updates all provided refs', () => {
const refObject: React.RefObject<boolean> = React.createRef<boolean>();
let refValue: boolean | null = null;
const TestComponent: React.FunctionComponent = () => {
const mergedRef = useMergedRefs<boolean>(refObject, val => (refValue = val));
mergedRef(true);
return null;
};
mount(<TestComponent />);
expect(refObject.current).toBe(true);
expect(refValue).toBe(true);
});
it('reuses the same ref callback if refs remain stable', () => {
const refObject: React.RefObject<boolean> = React.createRef<boolean>();
// tslint:disable-next-line:no-empty
const refValueFunc = (val: boolean) => {};
let refCallback: Function | undefined = undefined;
const TestComponent: React.FunctionComponent = () => {
refCallback = useMergedRefs<boolean>(refObject, refValueFunc);
return null;
};
const wrapper = mount(<TestComponent />);
const firstRefCallback = refCallback;
// Re-render the component
wrapper.update();
expect(refCallback).toBe(firstRefCallback);
});
it('handles changing ref callbacks', () => {
const refObject: React.RefObject<boolean> = React.createRef<boolean>();
let firstRefValue: boolean | null = null;
let refValueFunc = (val: boolean) => (firstRefValue = val);
const TestComponent: React.FunctionComponent = () => {
const mergedRef = useMergedRefs<boolean>(refObject, refValueFunc);
mergedRef(true);
return null;
};
const wrapper = mount(<TestComponent />);
let secondRefValue: boolean | null = null;
refValueFunc = (val: boolean) => (secondRefValue = val);
// Re-render the component
wrapper.setProps({});
expect(firstRefValue).toBe(true);
expect(secondRefValue).toBe(true);
});
});