|
| 1 | +import { useInterval, useIntervalValue } from './useIntervalValue'; |
| 2 | +import { render, screen, act } from '@testing-library/react'; |
| 3 | +import React from 'react'; |
| 4 | + |
| 5 | +// Test component for useInterval |
| 6 | +const TestIntervalComponent: React.FC<{ |
| 7 | + delay: number | null; |
| 8 | + pauseOnWindowBlur?: boolean; |
| 9 | +}> = ({ delay, pauseOnWindowBlur = true }) => { |
| 10 | + const [count, setCount] = React.useState(0); |
| 11 | + |
| 12 | + useInterval( |
| 13 | + () => { |
| 14 | + setCount((prev) => prev + 1); |
| 15 | + }, |
| 16 | + delay, |
| 17 | + pauseOnWindowBlur, |
| 18 | + ); |
| 19 | + |
| 20 | + return <div data-testid="interval-count">{count}</div>; |
| 21 | +}; |
| 22 | + |
| 23 | +// Test component for useIntervalValue |
| 24 | +const TestIntervalValueComponent: React.FC<{ |
| 25 | + delay: number | null; |
| 26 | + pauseOnWindowBlur?: boolean; |
| 27 | +}> = ({ delay, pauseOnWindowBlur = true }) => { |
| 28 | + const value = useIntervalValue( |
| 29 | + () => Date.now(), |
| 30 | + delay, |
| 31 | + undefined, |
| 32 | + pauseOnWindowBlur, |
| 33 | + ); |
| 34 | + |
| 35 | + return <div data-testid="interval-value">{value}</div>; |
| 36 | +}; |
| 37 | + |
| 38 | +describe('useInterval and useIntervalValue hooks', () => { |
| 39 | + beforeEach(() => { |
| 40 | + jest.useFakeTimers(); |
| 41 | + // Mock document.hidden as false initially (page is visible) |
| 42 | + Object.defineProperty(document, 'hidden', { |
| 43 | + writable: true, |
| 44 | + value: false, |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + afterEach(() => { |
| 49 | + jest.useRealTimers(); |
| 50 | + }); |
| 51 | + |
| 52 | + describe('useInterval', () => { |
| 53 | + it('should increment count at specified interval', () => { |
| 54 | + render(<TestIntervalComponent delay={1000} />); |
| 55 | + |
| 56 | + expect(screen.getByTestId('interval-count')).toHaveTextContent('0'); |
| 57 | + |
| 58 | + act(() => { |
| 59 | + jest.advanceTimersByTime(1000); |
| 60 | + }); |
| 61 | + |
| 62 | + expect(screen.getByTestId('interval-count')).toHaveTextContent('1'); |
| 63 | + |
| 64 | + act(() => { |
| 65 | + jest.advanceTimersByTime(2000); |
| 66 | + }); |
| 67 | + |
| 68 | + expect(screen.getByTestId('interval-count')).toHaveTextContent('3'); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should pause when pauseOnWindowBlur is true and page becomes hidden', () => { |
| 72 | + render(<TestIntervalComponent delay={1000} pauseOnWindowBlur={true} />); |
| 73 | + |
| 74 | + expect(screen.getByTestId('interval-count')).toHaveTextContent('0'); |
| 75 | + |
| 76 | + // Page becomes hidden |
| 77 | + act(() => { |
| 78 | + Object.defineProperty(document, 'hidden', { value: true }); |
| 79 | + document.dispatchEvent(new Event('visibilitychange')); |
| 80 | + }); |
| 81 | + |
| 82 | + // Timer should not increment |
| 83 | + act(() => { |
| 84 | + jest.advanceTimersByTime(2000); |
| 85 | + }); |
| 86 | + |
| 87 | + expect(screen.getByTestId('interval-count')).toHaveTextContent('0'); |
| 88 | + |
| 89 | + // Page becomes visible again |
| 90 | + act(() => { |
| 91 | + Object.defineProperty(document, 'hidden', { value: false }); |
| 92 | + document.dispatchEvent(new Event('visibilitychange')); |
| 93 | + }); |
| 94 | + |
| 95 | + // Should execute immediately when visible again |
| 96 | + expect(screen.getByTestId('interval-count')).toHaveTextContent('1'); |
| 97 | + |
| 98 | + // Continue normal interval |
| 99 | + act(() => { |
| 100 | + jest.advanceTimersByTime(1000); |
| 101 | + }); |
| 102 | + |
| 103 | + expect(screen.getByTestId('interval-count')).toHaveTextContent('2'); |
| 104 | + }); |
| 105 | + |
| 106 | + it('should continue running when pauseOnWindowBlur is false and page becomes hidden', () => { |
| 107 | + render(<TestIntervalComponent delay={1000} pauseOnWindowBlur={false} />); |
| 108 | + |
| 109 | + expect(screen.getByTestId('interval-count')).toHaveTextContent('0'); |
| 110 | + |
| 111 | + // Page becomes hidden |
| 112 | + act(() => { |
| 113 | + Object.defineProperty(document, 'hidden', { value: true }); |
| 114 | + document.dispatchEvent(new Event('visibilitychange')); |
| 115 | + }); |
| 116 | + |
| 117 | + // Timer should continue to increment even when hidden |
| 118 | + act(() => { |
| 119 | + jest.advanceTimersByTime(2000); |
| 120 | + }); |
| 121 | + |
| 122 | + expect(screen.getByTestId('interval-count')).toHaveTextContent('2'); |
| 123 | + }); |
| 124 | + |
| 125 | + it('should not run when delay is null', () => { |
| 126 | + render(<TestIntervalComponent delay={null} />); |
| 127 | + |
| 128 | + expect(screen.getByTestId('interval-count')).toHaveTextContent('0'); |
| 129 | + |
| 130 | + act(() => { |
| 131 | + jest.advanceTimersByTime(5000); |
| 132 | + }); |
| 133 | + |
| 134 | + expect(screen.getByTestId('interval-count')).toHaveTextContent('0'); |
| 135 | + }); |
| 136 | + }); |
| 137 | + |
| 138 | + describe('useIntervalValue', () => { |
| 139 | + it('should update value at specified interval', () => { |
| 140 | + render(<TestIntervalValueComponent delay={1000} />); |
| 141 | + |
| 142 | + const initialValue = screen.getByTestId('interval-value').textContent; |
| 143 | + |
| 144 | + act(() => { |
| 145 | + jest.advanceTimersByTime(1000); |
| 146 | + }); |
| 147 | + |
| 148 | + const updatedValue = screen.getByTestId('interval-value').textContent; |
| 149 | + expect(updatedValue).not.toBe(initialValue); |
| 150 | + }); |
| 151 | + |
| 152 | + it('should pause value updates when page becomes hidden and pauseOnWindowBlur is true', () => { |
| 153 | + render( |
| 154 | + <TestIntervalValueComponent delay={1000} pauseOnWindowBlur={true} />, |
| 155 | + ); |
| 156 | + |
| 157 | + const initialValue = screen.getByTestId('interval-value').textContent; |
| 158 | + |
| 159 | + // Page becomes hidden |
| 160 | + act(() => { |
| 161 | + Object.defineProperty(document, 'hidden', { value: true }); |
| 162 | + document.dispatchEvent(new Event('visibilitychange')); |
| 163 | + }); |
| 164 | + |
| 165 | + act(() => { |
| 166 | + jest.advanceTimersByTime(2000); |
| 167 | + }); |
| 168 | + |
| 169 | + // Value should not change when hidden |
| 170 | + expect(screen.getByTestId('interval-value')).toHaveTextContent( |
| 171 | + initialValue!, |
| 172 | + ); |
| 173 | + |
| 174 | + // Page becomes visible again |
| 175 | + act(() => { |
| 176 | + Object.defineProperty(document, 'hidden', { value: false }); |
| 177 | + document.dispatchEvent(new Event('visibilitychange')); |
| 178 | + }); |
| 179 | + |
| 180 | + // Value should update immediately when visible again |
| 181 | + const newValue = screen.getByTestId('interval-value').textContent; |
| 182 | + expect(newValue).not.toBe(initialValue); |
| 183 | + }); |
| 184 | + |
| 185 | + it('should continue updating when pauseOnWindowBlur is false and page becomes hidden', () => { |
| 186 | + render( |
| 187 | + <TestIntervalValueComponent delay={1000} pauseOnWindowBlur={false} />, |
| 188 | + ); |
| 189 | + |
| 190 | + const initialValue = screen.getByTestId('interval-value').textContent; |
| 191 | + |
| 192 | + // Page becomes hidden |
| 193 | + act(() => { |
| 194 | + Object.defineProperty(document, 'hidden', { value: true }); |
| 195 | + document.dispatchEvent(new Event('visibilitychange')); |
| 196 | + }); |
| 197 | + |
| 198 | + act(() => { |
| 199 | + jest.advanceTimersByTime(1000); |
| 200 | + }); |
| 201 | + |
| 202 | + // Value should continue to update even when hidden |
| 203 | + const updatedValue = screen.getByTestId('interval-value').textContent; |
| 204 | + expect(updatedValue).not.toBe(initialValue); |
| 205 | + }); |
| 206 | + }); |
| 207 | +}); |
0 commit comments