test: Add test coverage for ensuredForwardRef

This commit is contained in:
Abraham Schilling 2021-02-10 22:32:04 +01:00
parent d487cccf4c
commit 86ea576c7e

View File

@ -2,7 +2,7 @@ import React, { useRef } from 'react';
import ReactDOM from 'react-dom';
import { renderHook } from '@testing-library/react-hooks';
import TestUtils from 'react-dom/test-utils';
import { useEnsuredForwardedRef } from '../src';
import { ensuredForwardRef, useEnsuredForwardedRef } from '../src';
let container: HTMLDivElement;
@ -51,3 +51,24 @@ test('should return a valid ref when the forwarded ref is undefined', () => {
expect(ensuredRef.current.id).toBe('test_id');
});
test('should return a valid ref when using the wrapper function style', () => {
const { result } = renderHook(() => {
const initialRef = useRef<HTMLDivElement | null>(null);
const WrappedComponent = ensuredForwardRef<HTMLDivElement>((_props, ref) => {
return <div id="test_id" ref={ref} />;
});
TestUtils.act(() => {
ReactDOM.render(<WrappedComponent ref={initialRef} />, container);
});
return { initialRef };
});
const { initialRef } = result.current;
expect(initialRef.current).toBeTruthy();
expect(initialRef.current?.id).toBe('test_id');
});