|
| 1 | +import { describe, it, expect } from 'vitest' |
| 2 | +import { renderHook, act } from '@testing-library/react'; |
| 3 | +import useArray from '../useArray'; |
| 4 | + |
| 5 | +describe('useArray', () => { |
| 6 | + it('should initialize with the given array', () => { |
| 7 | + const { result } = renderHook(() => useArray([1, 2, 3])); |
| 8 | + expect(result.current.array).toEqual([1, 2, 3]); |
| 9 | + }); |
| 10 | + |
| 11 | + it('should push an element', () => { |
| 12 | + const { result } = renderHook(() => useArray([1, 2])); |
| 13 | + act(() => { |
| 14 | + result.current.push(3); |
| 15 | + }); |
| 16 | + expect(result.current.array).toEqual([1, 2, 3]); |
| 17 | + }); |
| 18 | + |
| 19 | + it('should filter elements', () => { |
| 20 | + const { result } = renderHook(() => useArray([1, 2, 3, 4])); |
| 21 | + act(() => { |
| 22 | + result.current.filter((n: number) => n % 2 === 0); |
| 23 | + }); |
| 24 | + expect(result.current.array).toEqual([2, 4]); |
| 25 | + }); |
| 26 | + |
| 27 | + it('should update an element at a given index', () => { |
| 28 | + const { result } = renderHook(() => useArray(['a', 'b', 'c'])); |
| 29 | + act(() => { |
| 30 | + result.current.update(1, 'z'); |
| 31 | + }); |
| 32 | + expect(result.current.array).toEqual(['a', 'z', 'c']); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should remove an element at a given index', () => { |
| 36 | + const { result } = renderHook(() => useArray([10, 20, 30])); |
| 37 | + act(() => { |
| 38 | + result.current.remove(1); |
| 39 | + }); |
| 40 | + expect(result.current.array).toEqual([10, 30]); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should clear the array', () => { |
| 44 | + const { result } = renderHook(() => useArray([1, 2, 3])); |
| 45 | + act(() => { |
| 46 | + result.current.clear(); |
| 47 | + }); |
| 48 | + expect(result.current.array).toEqual([]); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should set the array directly', () => { |
| 52 | + const { result } = renderHook(() => useArray([1, 2, 3])); |
| 53 | + act(() => { |
| 54 | + result.current.set([7, 8, 9]); |
| 55 | + }); |
| 56 | + expect(result.current.array).toEqual([7, 8, 9]); |
| 57 | + }); |
| 58 | +}); |
| 59 | + |
0 commit comments