|
| 1 | +import {render, renderHook} from '@testing-library/react' |
| 2 | +import React, {forwardRef, type RefObject} from 'react' |
| 3 | +import {describe, expect, it, vi} from 'vitest' |
| 4 | +import {useMergedRefs} from '../useMergedRefs' |
| 5 | + |
| 6 | +type InputOrButtonRef = RefObject<HTMLInputElement & HTMLButtonElement> |
| 7 | + |
| 8 | +const Component = forwardRef<HTMLInputElement & HTMLButtonElement, {asButton?: boolean}>(({asButton}, forwardedRef) => { |
| 9 | + const ref: InputOrButtonRef = React.useRef(null) |
| 10 | + |
| 11 | + const combinedRef = useMergedRefs(forwardedRef, ref) |
| 12 | + |
| 13 | + return asButton ? <button type="button" ref={combinedRef} /> : <input ref={combinedRef} /> |
| 14 | +}) |
| 15 | + |
| 16 | +describe('useMergedRefs', () => { |
| 17 | + describe('combining a forwarded ref with an internal ref', () => { |
| 18 | + describe('object refs', () => { |
| 19 | + it('forwards the ref to the underlying element', async () => { |
| 20 | + const ref: InputOrButtonRef = {current: null} |
| 21 | + |
| 22 | + render(<Component ref={ref} />) |
| 23 | + |
| 24 | + expect(ref.current).toBeInstanceOf(HTMLInputElement) |
| 25 | + }) |
| 26 | + |
| 27 | + it('avoids dangling references by clearing the ref on unmount', () => { |
| 28 | + const ref: InputOrButtonRef = {current: null} |
| 29 | + |
| 30 | + const {unmount} = render(<Component ref={ref} />) |
| 31 | + |
| 32 | + expect(ref.current).toBeInstanceOf(HTMLInputElement) |
| 33 | + |
| 34 | + unmount() |
| 35 | + |
| 36 | + expect(ref.current).toBeNull() |
| 37 | + }) |
| 38 | + |
| 39 | + it('updates the ref if the target changes', () => { |
| 40 | + const ref: InputOrButtonRef = {current: null} |
| 41 | + |
| 42 | + const {rerender} = render(<Component ref={ref} />) |
| 43 | + |
| 44 | + expect(ref.current).toBeInstanceOf(HTMLInputElement) |
| 45 | + |
| 46 | + rerender(<Component ref={ref} asButton />) |
| 47 | + |
| 48 | + expect(ref.current).toBeInstanceOf(HTMLButtonElement) |
| 49 | + }) |
| 50 | + |
| 51 | + it('is correctly set during an initial effect', () => { |
| 52 | + // This can break if the hook delays setting the initial value until a subsequent render |
| 53 | + |
| 54 | + const ComponentWithEffect = () => { |
| 55 | + const ref = React.useRef<HTMLInputElement & HTMLButtonElement>(null) |
| 56 | + |
| 57 | + React.useEffect(() => { |
| 58 | + expect(ref.current).toBeInstanceOf(HTMLInputElement) |
| 59 | + }, []) |
| 60 | + |
| 61 | + return <Component ref={ref} /> |
| 62 | + } |
| 63 | + |
| 64 | + render(<ComponentWithEffect />) |
| 65 | + }) |
| 66 | + }) |
| 67 | + |
| 68 | + describe('callback refs', () => { |
| 69 | + it('calls the ref on mount and unmount', async () => { |
| 70 | + const ref = vi.fn() |
| 71 | + |
| 72 | + const {unmount} = render(<Component ref={ref} />) |
| 73 | + |
| 74 | + expect(ref).toHaveBeenCalledWith(expect.any(HTMLInputElement)) |
| 75 | + |
| 76 | + unmount() |
| 77 | + |
| 78 | + expect(ref).toHaveBeenCalledWith(null) |
| 79 | + }) |
| 80 | + |
| 81 | + it('calls the ref if the target changes', () => { |
| 82 | + const ref = vi.fn() |
| 83 | + |
| 84 | + const {rerender} = render(<Component ref={ref} />) |
| 85 | + |
| 86 | + expect(ref).toHaveBeenCalledWith(expect.any(HTMLInputElement)) |
| 87 | + |
| 88 | + rerender(<Component ref={ref} asButton />) |
| 89 | + |
| 90 | + expect(ref).toHaveBeenCalledWith(expect.any(HTMLButtonElement)) |
| 91 | + }) |
| 92 | + |
| 93 | + it('does not call the ref on re-render if the target does not change', () => { |
| 94 | + const ref = vi.fn() |
| 95 | + |
| 96 | + const {rerender} = render(<Component ref={ref} />) |
| 97 | + |
| 98 | + rerender(<Component ref={ref} />) |
| 99 | + |
| 100 | + expect(ref).toHaveBeenCalledExactlyOnceWith(expect.any(HTMLInputElement)) |
| 101 | + }) |
| 102 | + }) |
| 103 | + }) |
| 104 | + |
| 105 | + describe('combining two callback refs', () => { |
| 106 | + it('calls both refs when the combined ref is called', () => { |
| 107 | + const refA = vi.fn() |
| 108 | + const refB = vi.fn() |
| 109 | + |
| 110 | + const combined = renderHook(() => useMergedRefs(refA, refB)) |
| 111 | + |
| 112 | + combined.result.current('test') |
| 113 | + expect(refA).toHaveBeenCalledExactlyOnceWith('test') |
| 114 | + expect(refB).toHaveBeenCalledExactlyOnceWith('test') |
| 115 | + }) |
| 116 | + |
| 117 | + it('handles React 18 null values correctly', () => { |
| 118 | + const refA = vi.fn() |
| 119 | + const refB = vi.fn() |
| 120 | + |
| 121 | + const combined = renderHook(() => useMergedRefs(refA, refB)) |
| 122 | + |
| 123 | + combined.result.current('test') |
| 124 | + expect(refA).toHaveBeenCalledWith('test') |
| 125 | + expect(refB).toHaveBeenCalledWith('test') |
| 126 | + |
| 127 | + // on React 18, cleanup fn will be ignored and ref will be called with null |
| 128 | + combined.result.current(null) |
| 129 | + |
| 130 | + expect(refA).toHaveBeenCalledWith(null) |
| 131 | + expect(refB).toHaveBeenCalledWith(null) |
| 132 | + }) |
| 133 | + |
| 134 | + it('handles React 19 cleanup functions correctly and independently', () => { |
| 135 | + const refA = vi.fn() |
| 136 | + const cleanupRefB = vi.fn() |
| 137 | + const refB = vi.fn().mockReturnValue(cleanupRefB) |
| 138 | + |
| 139 | + const combined = renderHook(() => useMergedRefs(refA, refB)) |
| 140 | + |
| 141 | + const cleanup = combined.result.current('test') |
| 142 | + expect(refA).toHaveBeenCalledWith('test') |
| 143 | + expect(refB).toHaveBeenCalledWith('test') |
| 144 | + |
| 145 | + // React 19 will call cleanup function and not pass null |
| 146 | + cleanup() |
| 147 | + |
| 148 | + expect(refA).toHaveBeenCalledWith(null) |
| 149 | + expect(refB).not.toHaveBeenCalledWith(null) |
| 150 | + expect(cleanupRefB).toHaveBeenCalledOnce() |
| 151 | + }) |
| 152 | + }) |
| 153 | +}) |
0 commit comments