|
| 1 | +import { renderHook, act } from '@testing-library/react-hooks'; |
| 2 | +import { useSafeTimeout } from '../../src'; |
| 3 | + |
| 4 | +jest.useFakeTimers(); |
| 5 | + |
| 6 | +describe('useSafeTimeout hook', () => { |
| 7 | + afterEach(() => { |
| 8 | + jest.clearAllTimers(); |
| 9 | + }); |
| 10 | + |
| 11 | + afterAll(() => { |
| 12 | + jest.useRealTimers(); |
| 13 | + }); |
| 14 | + |
| 15 | + it('should execute callback after specified delay', () => { |
| 16 | + const { result } = renderHook(() => useSafeTimeout()); |
| 17 | + const callback = jest.fn(); |
| 18 | + |
| 19 | + act(() => { |
| 20 | + result.current(callback, 1000); |
| 21 | + }); |
| 22 | + |
| 23 | + expect(callback).not.toHaveBeenCalled(); |
| 24 | + |
| 25 | + act(() => { |
| 26 | + jest.advanceTimersByTime(1000); |
| 27 | + }); |
| 28 | + |
| 29 | + expect(callback).toHaveBeenCalledTimes(1); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should not execute callback if component unmounts before delay', () => { |
| 33 | + const { result, unmount } = renderHook(() => useSafeTimeout()); |
| 34 | + const callback = jest.fn(); |
| 35 | + |
| 36 | + act(() => { |
| 37 | + result.current(callback, 1000); |
| 38 | + }); |
| 39 | + |
| 40 | + unmount(); |
| 41 | + |
| 42 | + act(() => { |
| 43 | + jest.advanceTimersByTime(1000); |
| 44 | + }); |
| 45 | + |
| 46 | + expect(callback).not.toHaveBeenCalled(); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should allow multiple timeouts to run if not overridden', () => { |
| 50 | + const { result } = renderHook(() => useSafeTimeout()); |
| 51 | + const callback1 = jest.fn(); |
| 52 | + const callback2 = jest.fn(); |
| 53 | + |
| 54 | + act(() => { |
| 55 | + result.current(callback1, 1000); |
| 56 | + }); |
| 57 | + |
| 58 | + act(() => { |
| 59 | + result.current(callback2, 500); |
| 60 | + }); |
| 61 | + |
| 62 | + act(() => { |
| 63 | + jest.advanceTimersByTime(500); |
| 64 | + }); |
| 65 | + |
| 66 | + expect(callback2).toHaveBeenCalledTimes(1); |
| 67 | + expect(callback1).not.toHaveBeenCalled(); |
| 68 | + |
| 69 | + act(() => { |
| 70 | + jest.advanceTimersByTime(500); |
| 71 | + }); |
| 72 | + |
| 73 | + expect(callback1).toHaveBeenCalledTimes(1); |
| 74 | + expect(callback2).toHaveBeenCalledTimes(1); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should handle multiple timeouts in sequence', () => { |
| 78 | + const { result } = renderHook(() => useSafeTimeout()); |
| 79 | + const callback1 = jest.fn(); |
| 80 | + const callback2 = jest.fn(); |
| 81 | + |
| 82 | + act(() => { |
| 83 | + result.current(callback1, 500); |
| 84 | + }); |
| 85 | + |
| 86 | + act(() => { |
| 87 | + jest.advanceTimersByTime(500); |
| 88 | + }); |
| 89 | + |
| 90 | + expect(callback1).toHaveBeenCalledTimes(1); |
| 91 | + |
| 92 | + act(() => { |
| 93 | + result.current(callback2, 300); |
| 94 | + }); |
| 95 | + |
| 96 | + act(() => { |
| 97 | + jest.advanceTimersByTime(300); |
| 98 | + }); |
| 99 | + |
| 100 | + expect(callback2).toHaveBeenCalledTimes(1); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should handle zero delay', () => { |
| 104 | + const { result } = renderHook(() => useSafeTimeout()); |
| 105 | + const callback = jest.fn(); |
| 106 | + |
| 107 | + act(() => { |
| 108 | + result.current(callback, 0); |
| 109 | + }); |
| 110 | + |
| 111 | + act(() => { |
| 112 | + jest.advanceTimersByTime(0); |
| 113 | + }); |
| 114 | + |
| 115 | + expect(callback).toHaveBeenCalledTimes(1); |
| 116 | + }); |
| 117 | + |
| 118 | + it('should clean up timeout on unmount', () => { |
| 119 | + const clearTimeoutSpy = jest.spyOn(global, 'clearTimeout'); |
| 120 | + const { result, unmount } = renderHook(() => useSafeTimeout()); |
| 121 | + const callback = jest.fn(); |
| 122 | + |
| 123 | + act(() => { |
| 124 | + result.current(callback, 1000); |
| 125 | + }); |
| 126 | + |
| 127 | + unmount(); |
| 128 | + |
| 129 | + expect(clearTimeoutSpy).toHaveBeenCalled(); |
| 130 | + clearTimeoutSpy.mockRestore(); |
| 131 | + }); |
| 132 | + |
| 133 | + it('should return the same function reference across renders', () => { |
| 134 | + const { result, rerender } = renderHook(() => useSafeTimeout()); |
| 135 | + const firstReference = result.current; |
| 136 | + |
| 137 | + rerender(); |
| 138 | + const secondReference = result.current; |
| 139 | + |
| 140 | + expect(firstReference).toBe(secondReference); |
| 141 | + }); |
| 142 | + |
| 143 | + it('should handle callback that throws an error', () => { |
| 144 | + const { result } = renderHook(() => useSafeTimeout()); |
| 145 | + const errorCallback = jest.fn(() => { |
| 146 | + throw new Error('Test error'); |
| 147 | + }); |
| 148 | + |
| 149 | + act(() => { |
| 150 | + result.current(errorCallback, 100); |
| 151 | + }); |
| 152 | + |
| 153 | + expect(() => { |
| 154 | + act(() => { |
| 155 | + jest.advanceTimersByTime(100); |
| 156 | + }); |
| 157 | + }).toThrow('Test error'); |
| 158 | + |
| 159 | + expect(errorCallback).toHaveBeenCalledTimes(1); |
| 160 | + }); |
| 161 | +}); |
0 commit comments