mirror of
https://github.com/streamich/react-use.git
synced 2026-01-18 14:06:52 +00:00
test: Add test coverage for ensuredForwardRef
This commit is contained in:
parent
d487cccf4c
commit
86ea576c7e
@ -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');
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user