|
1 | | -import { HubConnection, HubConnectionBuilder, LogLevel } from '@microsoft/signalr'; |
| 1 | +import { HubConnection, HubConnectionBuilder, HubConnectionState, LogLevel } from '@microsoft/signalr'; |
2 | 2 |
|
3 | 3 | import { logger } from '@/lib/logging'; |
4 | 4 |
|
@@ -72,6 +72,7 @@ describe('SignalRService - Enhanced Features', () => { |
72 | 72 | onclose: jest.fn(), |
73 | 73 | onreconnecting: jest.fn(), |
74 | 74 | onreconnected: jest.fn(), |
| 75 | + state: HubConnectionState.Disconnected, |
75 | 76 | } as any; |
76 | 77 |
|
77 | 78 | // Mock HubConnectionBuilder |
@@ -268,25 +269,26 @@ describe('SignalRService - Enhanced Features', () => { |
268 | 269 |
|
269 | 270 | jest.useFakeTimers(); |
270 | 271 |
|
271 | | - // Mock the connections map to indicate connection exists |
272 | | - const connectionsMap = (service as any).connections; |
273 | | - const originalHas = connectionsMap.has; |
274 | | - connectionsMap.has = jest.fn().mockReturnValue(true); |
| 272 | + // Mock connection state to be Connected |
| 273 | + Object.defineProperty(mockConnection, 'state', { |
| 274 | + value: HubConnectionState.Connected, |
| 275 | + writable: true, |
| 276 | + }); |
275 | 277 |
|
276 | | - // Trigger connection close |
| 278 | + // Clear previous logs to isolate subsequent logging |
| 279 | + jest.clearAllMocks(); |
| 280 | + |
| 281 | + // Trigger connection close to schedule a reconnect |
277 | 282 | onCloseCallback(); |
278 | 283 |
|
279 | | - // Fast forward time |
| 284 | + // Fast forward time to trigger the scheduled reconnect |
280 | 285 | jest.advanceTimersByTime(5000); |
281 | 286 |
|
282 | 287 | // Should log skip message |
283 | 288 | expect(mockLogger.debug).toHaveBeenCalledWith({ |
284 | 289 | message: `Hub ${mockConfig.name} is already connected, skipping reconnection attempt`, |
285 | 290 | }); |
286 | 291 |
|
287 | | - // Restore original method |
288 | | - connectionsMap.has = originalHas; |
289 | | - |
290 | 292 | jest.useRealTimers(); |
291 | 293 | }); |
292 | 294 |
|
|
0 commit comments