mirror of
https://github.com/streamich/react-use.git
synced 2026-01-18 14:06:52 +00:00
chore: move all the tests to the separate directory outside of sources; chore: remove jest.config.js (config moved to the package.json); test: unused import in test; test: 💍 fix tests add back x and y to useMeasure chore: 🤖 add linter to /tests folder ci: 🎡 limit Jest worker count for CircleCI
54 lines
1.3 KiB
TypeScript
54 lines
1.3 KiB
TypeScript
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';
|
|
|
|
let container: HTMLDivElement;
|
|
|
|
beforeEach(() => {
|
|
container = document.createElement('div');
|
|
document.body.appendChild(container);
|
|
});
|
|
|
|
afterEach(() => {
|
|
document.body.removeChild(container);
|
|
container = null;
|
|
});
|
|
|
|
test('should return a valid ref with existing forwardedRef', () => {
|
|
const { result } = renderHook(() => {
|
|
const ref = useRef(null);
|
|
const ensuredRef = useEnsuredForwardedRef(ref);
|
|
|
|
TestUtils.act(() => {
|
|
ReactDOM.render(<div ref={ensuredRef} />, container);
|
|
});
|
|
|
|
return {
|
|
initialRef: ref,
|
|
ensuredForwardedRef: ensuredRef,
|
|
};
|
|
});
|
|
|
|
const { initialRef, ensuredForwardedRef } = result.current;
|
|
|
|
expect(ensuredForwardedRef).toStrictEqual(initialRef);
|
|
});
|
|
|
|
test('should return a valid ref when the forwarded ref is undefined', () => {
|
|
const { result } = renderHook(() => {
|
|
const ref = useEnsuredForwardedRef<HTMLDivElement>(undefined);
|
|
|
|
TestUtils.act(() => {
|
|
ReactDOM.render(<div id="test_id" ref={ref} />, container);
|
|
});
|
|
|
|
return { ensuredRef: ref };
|
|
});
|
|
|
|
const { ensuredRef } = result.current;
|
|
|
|
expect(ensuredRef.current.id).toBe('test_id');
|
|
});
|