aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/helpers/refs.test.tsx
blob: 93e5f895755b2d3afea9046a78962e2b72215302 (plain)
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
import { describe, it, jest } from '@jest/globals';
import { render } from '@testing-library/react';
import { forwardRef, useImperativeHandle } from 'react';
import { mergeRefs } from './refs';

const refValue = 'minus architecto qui';
const TestComponentWithForwardedRef = forwardRef((_, ref) => {
  useImperativeHandle(ref, () => refValue);
  return null;
});
TestComponentWithForwardedRef.displayName = 'TestComponentWithForwardedRef';

describe('merge-refs', () => {
  it('can merge a ref function with a ref object', () => {
    const refFn = jest.fn();
    const refObj = { current: null };

    const TestComponent = () => (
      <TestComponentWithForwardedRef ref={mergeRefs([refFn, refObj])} />
    );

    render(<TestComponent />);

    expect(refFn).toHaveBeenCalledTimes(1);
    expect(refFn).toHaveBeenLastCalledWith(refValue);
    expect(refObj.current).toBe(refValue);
  });
});