|
| 1 | +import { mergeRefs } from './refs'; |
| 2 | + |
| 3 | +// ---------------------------------------------------------------------- |
| 4 | + |
| 5 | +describe('mergeRefs()', () => { |
| 6 | + it('1. Should call all function refs with the value', () => { |
| 7 | + const ref1 = vi.fn(); |
| 8 | + const ref2 = vi.fn(); |
| 9 | + const mergedRef = mergeRefs([ref1, ref2]); |
| 10 | + |
| 11 | + const value = 'test'; |
| 12 | + mergedRef(value); |
| 13 | + |
| 14 | + expect(ref1).toHaveBeenCalledWith(value); |
| 15 | + expect(ref2).toHaveBeenCalledWith(value); |
| 16 | + }); |
| 17 | + |
| 18 | + it('2. Should handle null and undefined refs gracefully', () => { |
| 19 | + const ref1 = vi.fn(); |
| 20 | + const ref2 = { current: null }; |
| 21 | + const mergedRef = mergeRefs([ref1, null, ref2, undefined]); |
| 22 | + |
| 23 | + const value = 'test'; |
| 24 | + mergedRef(value); |
| 25 | + |
| 26 | + expect(ref1).toHaveBeenCalledWith(value); |
| 27 | + expect(ref2.current).toBe(value); |
| 28 | + }); |
| 29 | + |
| 30 | + it('3. Should handle an empty array of refs', () => { |
| 31 | + const mergedRef = mergeRefs([]); |
| 32 | + |
| 33 | + const value = 'test'; |
| 34 | + mergedRef(value); |
| 35 | + |
| 36 | + // No refs to call, so no expectations |
| 37 | + }); |
| 38 | + |
| 39 | + it('4. Should handle an array with only null or undefined refs', () => { |
| 40 | + const mergedRef = mergeRefs([null, undefined]); |
| 41 | + |
| 42 | + const value = 'test'; |
| 43 | + mergedRef(value); |
| 44 | + |
| 45 | + // No refs to call, so no expectations |
| 46 | + }); |
| 47 | + |
| 48 | + it('5. Should handle an array with mixed function and object refs', () => { |
| 49 | + const ref1 = vi.fn(); |
| 50 | + const ref2 = { current: null }; |
| 51 | + const mergedRef = mergeRefs([ref1, ref2]); |
| 52 | + |
| 53 | + const value = 'test'; |
| 54 | + mergedRef(value); |
| 55 | + |
| 56 | + expect(ref1).toHaveBeenCalledWith(value); |
| 57 | + expect(ref2.current).toBe(value); |
| 58 | + }); |
| 59 | +}); |
0 commit comments