|
| 1 | +import { act, renderHook } from '@testing-library/react'; |
| 2 | + |
| 3 | +import { useInactivityTimer } from './useInactivityTimer'; |
| 4 | + |
| 5 | +// Mock timers for testing |
| 6 | +jest.useFakeTimers(); |
| 7 | + |
| 8 | +describe('hooks/useInactivityTimer.ts', () => { |
| 9 | + afterEach(() => { |
| 10 | + jest.clearAllTimers(); |
| 11 | + // Clear any event listeners |
| 12 | + document.removeEventListener = jest.fn(); |
| 13 | + document.addEventListener = jest.fn(); |
| 14 | + }); |
| 15 | + |
| 16 | + afterAll(() => { |
| 17 | + jest.useRealTimers(); |
| 18 | + }); |
| 19 | + |
| 20 | + it('should call callback after inactivity period', () => { |
| 21 | + const mockCallback = jest.fn(); |
| 22 | + const delay = 60000; // 60 seconds |
| 23 | + |
| 24 | + renderHook(() => useInactivityTimer(mockCallback, delay)); |
| 25 | + |
| 26 | + // Fast-forward time |
| 27 | + act(() => { |
| 28 | + jest.advanceTimersByTime(delay); |
| 29 | + }); |
| 30 | + |
| 31 | + expect(mockCallback).toHaveBeenCalledTimes(1); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should not call callback before inactivity period', () => { |
| 35 | + const mockCallback = jest.fn(); |
| 36 | + const delay = 60000; // 60 seconds |
| 37 | + |
| 38 | + renderHook(() => useInactivityTimer(mockCallback, delay)); |
| 39 | + |
| 40 | + // Fast-forward time but not enough |
| 41 | + act(() => { |
| 42 | + jest.advanceTimersByTime(delay - 1000); |
| 43 | + }); |
| 44 | + |
| 45 | + expect(mockCallback).not.toHaveBeenCalled(); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should reset timer on user activity', () => { |
| 49 | + const mockCallback = jest.fn(); |
| 50 | + const delay = 60000; // 60 seconds |
| 51 | + |
| 52 | + // Mock document event handling |
| 53 | + const addEventListenerSpy = jest.spyOn(document, 'addEventListener'); |
| 54 | + const removeEventListenerSpy = jest.spyOn(document, 'removeEventListener'); |
| 55 | + |
| 56 | + const { unmount } = renderHook(() => |
| 57 | + useInactivityTimer(mockCallback, delay), |
| 58 | + ); |
| 59 | + |
| 60 | + // Verify event listeners were added |
| 61 | + expect(addEventListenerSpy).toHaveBeenCalledWith( |
| 62 | + 'mousedown', |
| 63 | + expect.any(Function), |
| 64 | + { passive: true }, |
| 65 | + ); |
| 66 | + expect(addEventListenerSpy).toHaveBeenCalledWith( |
| 67 | + 'mousemove', |
| 68 | + expect.any(Function), |
| 69 | + { passive: true }, |
| 70 | + ); |
| 71 | + expect(addEventListenerSpy).toHaveBeenCalledWith( |
| 72 | + 'keypress', |
| 73 | + expect.any(Function), |
| 74 | + { passive: true }, |
| 75 | + ); |
| 76 | + expect(addEventListenerSpy).toHaveBeenCalledWith( |
| 77 | + 'scroll', |
| 78 | + expect.any(Function), |
| 79 | + { passive: true }, |
| 80 | + ); |
| 81 | + expect(addEventListenerSpy).toHaveBeenCalledWith( |
| 82 | + 'touchstart', |
| 83 | + expect.any(Function), |
| 84 | + { passive: true }, |
| 85 | + ); |
| 86 | + expect(addEventListenerSpy).toHaveBeenCalledWith( |
| 87 | + 'click', |
| 88 | + expect.any(Function), |
| 89 | + { passive: true }, |
| 90 | + ); |
| 91 | + |
| 92 | + // Simulate time passing |
| 93 | + act(() => { |
| 94 | + jest.advanceTimersByTime(30000); // 30 seconds |
| 95 | + }); |
| 96 | + |
| 97 | + expect(mockCallback).not.toHaveBeenCalled(); |
| 98 | + |
| 99 | + // Simulate user activity (get the reset function from the event listener) |
| 100 | + const resetTimerFn = addEventListenerSpy.mock.calls.find( |
| 101 | + (call) => call[0] === 'click', |
| 102 | + )?.[1] as () => void; |
| 103 | + |
| 104 | + act(() => { |
| 105 | + resetTimerFn(); // Simulate click |
| 106 | + }); |
| 107 | + |
| 108 | + // Continue time, but timer should be reset |
| 109 | + act(() => { |
| 110 | + jest.advanceTimersByTime(30000); // Another 30 seconds (total 60) |
| 111 | + }); |
| 112 | + |
| 113 | + // Callback should not have been called yet because timer was reset |
| 114 | + expect(mockCallback).not.toHaveBeenCalled(); |
| 115 | + |
| 116 | + // Now advance the full delay from the reset |
| 117 | + act(() => { |
| 118 | + jest.advanceTimersByTime(60000); |
| 119 | + }); |
| 120 | + |
| 121 | + expect(mockCallback).toHaveBeenCalledTimes(1); |
| 122 | + |
| 123 | + // Cleanup |
| 124 | + unmount(); |
| 125 | + expect(removeEventListenerSpy).toHaveBeenCalledWith( |
| 126 | + 'mousedown', |
| 127 | + expect.any(Function), |
| 128 | + ); |
| 129 | + expect(removeEventListenerSpy).toHaveBeenCalledWith( |
| 130 | + 'mousemove', |
| 131 | + expect.any(Function), |
| 132 | + ); |
| 133 | + expect(removeEventListenerSpy).toHaveBeenCalledWith( |
| 134 | + 'keypress', |
| 135 | + expect.any(Function), |
| 136 | + ); |
| 137 | + expect(removeEventListenerSpy).toHaveBeenCalledWith( |
| 138 | + 'scroll', |
| 139 | + expect.any(Function), |
| 140 | + ); |
| 141 | + expect(removeEventListenerSpy).toHaveBeenCalledWith( |
| 142 | + 'touchstart', |
| 143 | + expect.any(Function), |
| 144 | + ); |
| 145 | + expect(removeEventListenerSpy).toHaveBeenCalledWith( |
| 146 | + 'click', |
| 147 | + expect.any(Function), |
| 148 | + ); |
| 149 | + }); |
| 150 | + |
| 151 | + it('should not set timer when delay is null', () => { |
| 152 | + const mockCallback = jest.fn(); |
| 153 | + |
| 154 | + renderHook(() => useInactivityTimer(mockCallback, null as any)); |
| 155 | + |
| 156 | + act(() => { |
| 157 | + jest.advanceTimersByTime(60000); |
| 158 | + }); |
| 159 | + |
| 160 | + expect(mockCallback).not.toHaveBeenCalled(); |
| 161 | + }); |
| 162 | + |
| 163 | + it('should update callback when it changes', () => { |
| 164 | + const mockCallback1 = jest.fn(); |
| 165 | + const mockCallback2 = jest.fn(); |
| 166 | + const delay = 60000; |
| 167 | + |
| 168 | + const { rerender } = renderHook( |
| 169 | + ({ callback }) => useInactivityTimer(callback, delay), |
| 170 | + { initialProps: { callback: mockCallback1 } }, |
| 171 | + ); |
| 172 | + |
| 173 | + // Change the callback |
| 174 | + rerender({ callback: mockCallback2 }); |
| 175 | + |
| 176 | + act(() => { |
| 177 | + jest.advanceTimersByTime(delay); |
| 178 | + }); |
| 179 | + |
| 180 | + expect(mockCallback1).not.toHaveBeenCalled(); |
| 181 | + expect(mockCallback2).toHaveBeenCalledTimes(1); |
| 182 | + }); |
| 183 | +}); |
0 commit comments