Skip to content

Commit

Permalink
Merge pull request #1812 from n4bb12/pr/ensuredForwardRef-test-coverage
Browse files Browse the repository at this point in the history
test: Add test coverage for ensuredForwardRef
  • Loading branch information
xobotyi authored Apr 23, 2021
2 parents 5fd995e + 86ea576 commit a6d00b8
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion tests/useEnsuredForwardedRef.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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');
});

0 comments on commit a6d00b8

Please sign in to comment.