|
5 | 5 | * and basic hook behaviour. |
6 | 6 | */ |
7 | 7 | import { renderHook, act } from '@testing-library/react'; |
8 | | -import { describe, it, expect, beforeEach, vi } from 'vitest'; |
| 8 | +import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'; |
9 | 9 | import { useNotifications } from '../useNotifications'; |
10 | 10 | import { useNotificationStore } from '@/app/store/notificationStore'; |
11 | 11 | import { AppNotification } from '@/lib/notifications/types'; |
@@ -37,10 +37,20 @@ function resetStore() { |
37 | 37 | useNotificationStore.setState({ notifications: [] }); |
38 | 38 | } |
39 | 39 |
|
| 40 | +async function flushHookEffects() { |
| 41 | + await act(async () => {}); |
| 42 | + await act(async () => {}); |
| 43 | +} |
| 44 | + |
40 | 45 | // ─── Tests ──────────────────────────────────────────────────────────────────── |
41 | 46 |
|
42 | 47 | describe('useNotifications', () => { |
43 | 48 | beforeEach(resetStore); |
| 49 | + afterEach(() => { |
| 50 | + vi.useRealTimers(); |
| 51 | + vi.restoreAllMocks(); |
| 52 | + resetStore(); |
| 53 | + }); |
44 | 54 |
|
45 | 55 | // ── clearNotification ────────────────────────────────────────────────────── |
46 | 56 |
|
@@ -133,4 +143,122 @@ describe('useNotifications', () => { |
133 | 143 |
|
134 | 144 | expect(result.current.unreadCount).toBe(0); |
135 | 145 | }); |
| 146 | + |
| 147 | + // ── preferences heartbeat ───────────────────────────────────────────────── |
| 148 | + |
| 149 | + it('starts a preferences heartbeat and persists the liveness payload', async () => { |
| 150 | + vi.useFakeTimers(); |
| 151 | + vi.setSystemTime(new Date('2026-06-24T12:00:00.000Z')); |
| 152 | + |
| 153 | + const { result } = renderHook(() => |
| 154 | + useNotifications({ |
| 155 | + userId: 'heartbeat-user', |
| 156 | + preferencesHeartbeatIntervalMs: 1000, |
| 157 | + preferencesHeartbeatStaleAfterMs: 3000, |
| 158 | + }), |
| 159 | + ); |
| 160 | + |
| 161 | + await flushHookEffects(); |
| 162 | + |
| 163 | + expect(result.current.isLoading).toBe(false); |
| 164 | + expect(result.current.preferencesHeartbeat.status).toBe('online'); |
| 165 | + expect(result.current.preferencesHeartbeat.lastBeatAt).toBe('2026-06-24T12:00:00.000Z'); |
| 166 | + |
| 167 | + const stored = JSON.parse( |
| 168 | + localStorageMock.getItem('notification_preferences_heartbeat_v1') ?? 'null', |
| 169 | + ); |
| 170 | + expect(stored).toMatchObject({ |
| 171 | + userId: 'heartbeat-user', |
| 172 | + lastBeatAt: '2026-06-24T12:00:00.000Z', |
| 173 | + intervalMs: 1000, |
| 174 | + staleAfterMs: 3000, |
| 175 | + }); |
| 176 | + |
| 177 | + await act(async () => { |
| 178 | + vi.advanceTimersByTime(1000); |
| 179 | + }); |
| 180 | + |
| 181 | + expect(result.current.preferencesHeartbeat.lastBeatAt).toBe('2026-06-24T12:00:01.000Z'); |
| 182 | + }); |
| 183 | + |
| 184 | + it('marks an old preferences heartbeat as stale when refreshed', () => { |
| 185 | + vi.useFakeTimers(); |
| 186 | + vi.setSystemTime(new Date('2026-06-24T12:00:10.000Z')); |
| 187 | + |
| 188 | + localStorageMock.setItem( |
| 189 | + 'notification_preferences_heartbeat_v1', |
| 190 | + JSON.stringify({ |
| 191 | + userId: 'heartbeat-user', |
| 192 | + lastBeatAt: '2026-06-24T12:00:00.000Z', |
| 193 | + intervalMs: 1000, |
| 194 | + staleAfterMs: 3000, |
| 195 | + }), |
| 196 | + ); |
| 197 | + |
| 198 | + const { result } = renderHook(() => |
| 199 | + useNotifications({ |
| 200 | + userId: 'heartbeat-user', |
| 201 | + enablePreferencesHeartbeat: false, |
| 202 | + preferencesHeartbeatIntervalMs: 1000, |
| 203 | + preferencesHeartbeatStaleAfterMs: 3000, |
| 204 | + }), |
| 205 | + ); |
| 206 | + |
| 207 | + act(() => { |
| 208 | + result.current.refreshPreferencesHeartbeat(); |
| 209 | + }); |
| 210 | + |
| 211 | + expect(result.current.preferencesHeartbeat.status).toBe('stale'); |
| 212 | + expect(result.current.preferencesHeartbeat.lastBeatAt).toBe('2026-06-24T12:00:00.000Z'); |
| 213 | + }); |
| 214 | + |
| 215 | + it('stops the preferences heartbeat interval on unmount', async () => { |
| 216 | + vi.useFakeTimers(); |
| 217 | + vi.setSystemTime(new Date('2026-06-24T12:00:00.000Z')); |
| 218 | + |
| 219 | + const { result, unmount } = renderHook(() => |
| 220 | + useNotifications({ |
| 221 | + userId: 'heartbeat-user', |
| 222 | + preferencesHeartbeatIntervalMs: 1000, |
| 223 | + preferencesHeartbeatStaleAfterMs: 3000, |
| 224 | + }), |
| 225 | + ); |
| 226 | + |
| 227 | + await flushHookEffects(); |
| 228 | + |
| 229 | + expect(result.current.preferencesHeartbeat.status).toBe('online'); |
| 230 | + |
| 231 | + unmount(); |
| 232 | + |
| 233 | + await act(async () => { |
| 234 | + vi.advanceTimersByTime(5000); |
| 235 | + }); |
| 236 | + |
| 237 | + const stored = JSON.parse( |
| 238 | + localStorageMock.getItem('notification_preferences_heartbeat_v1') ?? 'null', |
| 239 | + ); |
| 240 | + expect(stored.lastBeatAt).toBe('2026-06-24T12:00:00.000Z'); |
| 241 | + }); |
| 242 | + |
| 243 | + it('reports the preferences heartbeat as offline when storage writes fail', async () => { |
| 244 | + vi.useFakeTimers(); |
| 245 | + vi.setSystemTime(new Date('2026-06-24T12:00:00.000Z')); |
| 246 | + vi.spyOn(localStorageMock, 'setItem').mockImplementation(() => { |
| 247 | + throw new Error('storage unavailable'); |
| 248 | + }); |
| 249 | + |
| 250 | + const { result } = renderHook(() => |
| 251 | + useNotifications({ |
| 252 | + userId: 'heartbeat-user', |
| 253 | + preferencesHeartbeatIntervalMs: 1000, |
| 254 | + preferencesHeartbeatStaleAfterMs: 3000, |
| 255 | + }), |
| 256 | + ); |
| 257 | + |
| 258 | + await flushHookEffects(); |
| 259 | + |
| 260 | + expect(result.current.preferencesHeartbeat.status).toBe('offline'); |
| 261 | + expect(result.current.preferencesHeartbeat.storageAvailable).toBe(false); |
| 262 | + expect(result.current.preferencesHeartbeat.failureCount).toBe(1); |
| 263 | + }); |
136 | 264 | }); |
0 commit comments