Merge pull request #1812 from n4bb12/pr/ensuredForwardRef-test-coverage

test: Add test coverage for ensuredForwardRef
This commit is contained in:
Anton Zinovyev 2021-04-23 10:18:03 +03:00 committed by GitHub
commit a6d00b8f77
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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');
});