|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | +import * as React from 'react'; |
| 3 | +import { composeRef, fillRef, useComposeRef, supportRef, supportNodeRef } from '../ref'; |
| 4 | +import { render, renderHook } from '@testing-library/react'; |
| 5 | + |
| 6 | +// 创建一个变量来保存模拟的React版本 |
| 7 | +let mockReactVersion = '18.0.0'; |
| 8 | + |
| 9 | +// 模拟react模块以允许控制version导出 |
| 10 | +vi.mock('react', async () => { |
| 11 | + const actualReact = await vi.importActual('react'); |
| 12 | + return { |
| 13 | + ...actualReact, |
| 14 | + get version() { |
| 15 | + return mockReactVersion; |
| 16 | + }, |
| 17 | + }; |
| 18 | +}); |
| 19 | + |
| 20 | +describe('ref utilities', () => { |
| 21 | + beforeEach(() => { |
| 22 | + mockReactVersion = '18.0.0'; |
| 23 | + }); |
| 24 | + |
| 25 | + describe('fillRef', () => { |
| 26 | + it('should call function ref with node', () => { |
| 27 | + const ref = vi.fn(); |
| 28 | + const node = document.createElement('div'); |
| 29 | + fillRef(ref, node); |
| 30 | + expect(ref).toHaveBeenCalledWith(node); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should set current property of object ref', () => { |
| 34 | + const ref = { current: null }; |
| 35 | + const node = document.createElement('div'); |
| 36 | + fillRef(ref, node); |
| 37 | + expect(ref.current).toBe(node); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should not throw error for null ref', () => { |
| 41 | + expect(() => fillRef(null, document.createElement('div'))).not.toThrow(); |
| 42 | + }); |
| 43 | + }); |
| 44 | + |
| 45 | + describe('composeRef', () => { |
| 46 | + it('should return undefined when no refs provided', () => { |
| 47 | + expect(composeRef()).toBeUndefined(); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should return the only ref when one ref provided', () => { |
| 51 | + const ref = vi.fn(); |
| 52 | + expect(composeRef(ref)).toBe(ref); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should compose multiple function refs', () => { |
| 56 | + const ref1 = vi.fn(); |
| 57 | + const ref2 = vi.fn(); |
| 58 | + const node = document.createElement('div'); |
| 59 | + |
| 60 | + const composedRef = composeRef(ref1, ref2); |
| 61 | + (composedRef as React.RefCallback<HTMLDivElement>)(node); |
| 62 | + |
| 63 | + expect(ref1).toHaveBeenCalledWith(node); |
| 64 | + expect(ref2).toHaveBeenCalledWith(node); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should compose multiple object refs', () => { |
| 68 | + const ref1 = React.createRef<HTMLDivElement>(); |
| 69 | + const ref2 = React.createRef<HTMLDivElement>(); |
| 70 | + const node = document.createElement('div'); |
| 71 | + |
| 72 | + const composedRef = composeRef(ref1, ref2); |
| 73 | + (composedRef as React.RefCallback<HTMLDivElement>)(node); |
| 74 | + |
| 75 | + expect(ref1.current).toBe(node); |
| 76 | + expect(ref2.current).toBe(node); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should compose mixed refs', () => { |
| 80 | + const ref1 = vi.fn(); |
| 81 | + const ref2 = React.createRef<HTMLDivElement>(); |
| 82 | + const node = document.createElement('div'); |
| 83 | + |
| 84 | + const composedRef = composeRef(ref1, ref2); |
| 85 | + (composedRef as React.RefCallback<HTMLDivElement>)(node); |
| 86 | + |
| 87 | + expect(ref1).toHaveBeenCalledWith(node); |
| 88 | + expect(ref2.current).toBe(node); |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + describe('useComposeRef', () => { |
| 93 | + it('should compose refs with hook', () => { |
| 94 | + const ref1 = vi.fn(); |
| 95 | + const ref2 = React.createRef<HTMLDivElement>(); |
| 96 | + |
| 97 | + const TestComponent = () => { |
| 98 | + const composedRef = useComposeRef(ref1, ref2); |
| 99 | + return ( |
| 100 | + <div ref={composedRef} data-testid="test"> |
| 101 | + Test |
| 102 | + </div> |
| 103 | + ); |
| 104 | + }; |
| 105 | + |
| 106 | + const { getByTestId } = render(<TestComponent />); |
| 107 | + |
| 108 | + expect(ref1).toHaveBeenCalledWith(getByTestId('test')); |
| 109 | + expect(ref2.current).toBe(getByTestId('test')); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should return same ref when refs are not changed', () => { |
| 113 | + const ref1 = vi.fn(); |
| 114 | + const { result, rerender } = renderHook(({ ref1 }) => useComposeRef(ref1), { initialProps: { ref1 } }); |
| 115 | + const firstRef = result.current; |
| 116 | + |
| 117 | + rerender({ ref1 }); |
| 118 | + expect(result.current).toBe(firstRef); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should return new ref when refs changed', () => { |
| 122 | + const ref1 = vi.fn(); |
| 123 | + const ref2 = vi.fn(); |
| 124 | + const { result, rerender } = renderHook(({ refs }) => useComposeRef(...refs), { initialProps: { refs: [ref1] } }); |
| 125 | + const firstRef = result.current; |
| 126 | + |
| 127 | + rerender({ refs: [ref1, ref2] }); |
| 128 | + expect(result.current).not.toBe(firstRef); |
| 129 | + }); |
| 130 | + }); |
| 131 | + |
| 132 | + describe('supportRef', () => { |
| 133 | + it('should return false for falsy values', () => { |
| 134 | + expect(supportRef(null)).toBe(false); |
| 135 | + expect(supportRef(undefined)).toBe(false); |
| 136 | + }); |
| 137 | + |
| 138 | + it('should return true for React 19+ elements', () => { |
| 139 | + mockReactVersion = '19.0.0'; |
| 140 | + |
| 141 | + const element = React.createElement('div'); |
| 142 | + expect(supportRef(element)).toBe(true); |
| 143 | + }); |
| 144 | + |
| 145 | + it('should return false for React 19+ function components without forwardRef', () => { |
| 146 | + mockReactVersion = '19.0.0'; |
| 147 | + |
| 148 | + const FunctionComponent = () => <div />; |
| 149 | + expect(supportRef(FunctionComponent)).toBe(false); |
| 150 | + }); |
| 151 | + |
| 152 | + it('should return true for forwardRef components', () => { |
| 153 | + const ForwardRefComponent = React.forwardRef(() => <div />); |
| 154 | + expect(supportRef(ForwardRefComponent)).toBe(true); |
| 155 | + }); |
| 156 | + |
| 157 | + it('should return true for class components', () => { |
| 158 | + class ClassComponent extends React.Component { |
| 159 | + render() { |
| 160 | + return <div />; |
| 161 | + } |
| 162 | + } |
| 163 | + expect(supportRef(ClassComponent)).toBe(true); |
| 164 | + }); |
| 165 | + |
| 166 | + it('should return false for function components without forwardRef in React 18', () => { |
| 167 | + mockReactVersion = '18.0.0'; |
| 168 | + |
| 169 | + const FunctionComponent = () => <div />; |
| 170 | + expect(supportRef(FunctionComponent)).toBe(false); |
| 171 | + }); |
| 172 | + |
| 173 | + it('should handle memo components', () => { |
| 174 | + const FunctionComponent = () => <div />; |
| 175 | + const MemoComponent = React.memo(FunctionComponent); |
| 176 | + expect(supportRef(MemoComponent)).toBe(false); |
| 177 | + }); |
| 178 | + }); |
| 179 | + |
| 180 | + describe('supportNodeRef', () => { |
| 181 | + it('should return false for non-elements', () => { |
| 182 | + expect(supportNodeRef(null)).toBe(false); |
| 183 | + expect(supportNodeRef('string')).toBe(false); |
| 184 | + expect(supportNodeRef(123)).toBe(false); |
| 185 | + }); |
| 186 | + |
| 187 | + it('should return true for elements that support ref in React 19+', () => { |
| 188 | + mockReactVersion = '19.0.0'; |
| 189 | + |
| 190 | + const element = React.createElement('div'); |
| 191 | + expect(supportNodeRef(element)).toBe(true); |
| 192 | + }); |
| 193 | + |
| 194 | + it('should return false for elements that do not support ref', () => { |
| 195 | + mockReactVersion = '18.0.0'; |
| 196 | + |
| 197 | + const FunctionComponent = () => <div />; |
| 198 | + const element = React.createElement(FunctionComponent); |
| 199 | + |
| 200 | + expect(supportNodeRef(element)).toBe(false); |
| 201 | + }); |
| 202 | + }); |
| 203 | +}); |
0 commit comments