|
| 1 | +import { renderHook } from '@testing-library/react'; |
| 2 | +import React from 'react'; |
| 3 | +import type { SuggestionItem } from '../index'; |
| 4 | +import useActive from '../useActive'; |
| 5 | + |
| 6 | +describe('useActive', () => { |
| 7 | + it('should initialize activePaths with first item value when open is true and items is valid array', () => { |
| 8 | + const items: SuggestionItem[] = [ |
| 9 | + { label: 'Item 1', value: 'item1' }, |
| 10 | + { label: 'Item 2', value: 'item2' }, |
| 11 | + ]; |
| 12 | + |
| 13 | + const { result } = renderHook(() => useActive(items, true, false, jest.fn(), jest.fn())); |
| 14 | + |
| 15 | + const [activePaths] = result.current; |
| 16 | + expect(activePaths).toEqual(['item1']); |
| 17 | + }); |
| 18 | + |
| 19 | + it('should not initialize activePaths when open is false', () => { |
| 20 | + const items: SuggestionItem[] = [{ label: 'Item 1', value: 'item1' }]; |
| 21 | + |
| 22 | + const { result } = renderHook(() => useActive(items, false, false, jest.fn(), jest.fn())); |
| 23 | + |
| 24 | + const [activePaths] = result.current; |
| 25 | + expect(activePaths).toEqual([]); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should not initialize activePaths when items is empty array', () => { |
| 29 | + const { result } = renderHook(() => useActive([], true, false, jest.fn(), jest.fn())); |
| 30 | + |
| 31 | + const [activePaths] = result.current; |
| 32 | + expect(activePaths).toEqual([]); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should not initialize activePaths when items is not a valid array', () => { |
| 36 | + // Test with null |
| 37 | + const { result: result1 } = renderHook(() => |
| 38 | + useActive(null as any, true, false, jest.fn(), jest.fn()), |
| 39 | + ); |
| 40 | + |
| 41 | + const [activePaths1] = result1.current; |
| 42 | + expect(activePaths1).toEqual([]); |
| 43 | + |
| 44 | + // Test with undefined |
| 45 | + const { result: result2 } = renderHook(() => |
| 46 | + useActive(undefined as any, true, false, jest.fn(), jest.fn()), |
| 47 | + ); |
| 48 | + |
| 49 | + const [activePaths2] = result2.current; |
| 50 | + expect(activePaths2).toEqual([]); |
| 51 | + |
| 52 | + // Test with non-array value |
| 53 | + const { result: result3 } = renderHook(() => |
| 54 | + useActive('not-an-array' as any, true, false, jest.fn(), jest.fn()), |
| 55 | + ); |
| 56 | + |
| 57 | + const [activePaths3] = result3.current; |
| 58 | + expect(activePaths3).toEqual([]); |
| 59 | + }); |
| 60 | +}); |
0 commit comments