@@ -224,6 +224,7 @@ const setupWebSocketService = ({
224
224
// Create mock messenger with all required methods
225
225
const mockMessenger = {
226
226
registerActionHandler : jest . fn ( ) ,
227
+ registerMethodActionHandlers : jest . fn ( ) ,
227
228
registerInitialEventPayload : jest . fn ( ) ,
228
229
publish : jest . fn ( ) ,
229
230
call : jest . fn ( ) ,
@@ -232,7 +233,7 @@ const setupWebSocketService = ({
232
233
} as any as jest . Mocked < WebSocketServiceMessenger > ;
233
234
234
235
// Default test options (shorter timeouts for faster tests)
235
- const defaultOptions : WebSocketServiceOptions = {
236
+ const defaultOptions = {
236
237
url : TEST_CONSTANTS . WS_URL ,
237
238
timeout : TEST_CONSTANTS . TIMEOUT_MS ,
238
239
reconnectDelay : TEST_CONSTANTS . RECONNECT_DELAY ,
@@ -368,14 +369,28 @@ describe('WebSocketService', () => {
368
369
mockWebSocketOptions : { autoConnect : false } , // This prevents any connection
369
370
} ) ;
370
371
371
- // Wait for the automatic init() from constructor to complete and fail
372
+ // Service should start in disconnected state since we removed auto-init
373
+ expect ( service . getConnectionInfo ( ) . state ) . toBe ( WebSocketState . DISCONNECTED ) ;
374
+
375
+ // Use expect.assertions to ensure error handling is tested
376
+ expect . assertions ( 4 ) ;
377
+
378
+ // Start connection and then advance timers to trigger timeout
379
+ const connectPromise = service . connect ( ) ;
380
+
381
+ // Handle the promise rejection properly
382
+ connectPromise . catch ( ( ) => {
383
+ // Expected rejection - do nothing to avoid unhandled promise warning
384
+ } ) ;
385
+
372
386
await completeAsyncOperations ( TEST_CONSTANTS . TIMEOUT_MS + 50 ) ;
373
387
374
- // Verify we're in error state from the failed init()
388
+ // Now check that the connection failed as expected
389
+ await expect ( connectPromise ) . rejects . toThrow ( `Failed to connect to WebSocket: Connection timeout after ${ TEST_CONSTANTS . TIMEOUT_MS } ms` ) ;
390
+
391
+ // Verify we're in error state from the failed connection attempt
375
392
expect ( service . getConnectionInfo ( ) . state ) . toBe ( WebSocketState . ERROR ) ;
376
393
377
- // The timeout behavior is already tested by the constructor's init() call
378
- // We can verify that the error was due to timeout by checking the last error
379
394
const info = service . getConnectionInfo ( ) ;
380
395
expect ( info . lastError ) . toContain ( `Connection timeout after ${ TEST_CONSTANTS . TIMEOUT_MS } ms` ) ;
381
396
@@ -467,12 +482,12 @@ describe('WebSocketService', () => {
467
482
} , 10000 ) ;
468
483
469
484
it ( 'should throw error when not connected' , async ( ) => {
470
- const { service, completeAsyncOperations , cleanup } = setupWebSocketService ( {
485
+ const { service, cleanup } = setupWebSocketService ( {
471
486
mockWebSocketOptions : { autoConnect : false } ,
472
487
} ) ;
473
488
474
- // Wait for automatic init() to fail and transition to error state
475
- await completeAsyncOperations ( 150 ) ;
489
+ // Service starts in disconnected state since we removed auto-init
490
+ expect ( service . getConnectionInfo ( ) . state ) . toBe ( WebSocketState . DISCONNECTED ) ;
476
491
477
492
const mockCallback = jest . fn ( ) ;
478
493
@@ -481,7 +496,7 @@ describe('WebSocketService', () => {
481
496
channels : [ 'test-channel' ] ,
482
497
callback : mockCallback ,
483
498
} )
484
- ) . rejects . toThrow ( 'Cannot create subscription(s) test-channel: WebSocket is error ' ) ;
499
+ ) . rejects . toThrow ( 'Cannot create subscription(s) test-channel: WebSocket is disconnected ' ) ;
485
500
486
501
cleanup ( ) ;
487
502
} ) ;
@@ -803,8 +818,8 @@ describe('WebSocketService', () => {
803
818
} ,
804
819
} satisfies ClientRequestMessage ;
805
820
806
- // Should throw when not connected (service starts in connecting state)
807
- await expect ( service . sendMessage ( testMessage ) ) . rejects . toThrow ( 'Cannot send message: WebSocket is connecting ' ) ;
821
+ // Should throw when not connected (service starts in disconnected state)
822
+ await expect ( service . sendMessage ( testMessage ) ) . rejects . toThrow ( 'Cannot send message: WebSocket is disconnected ' ) ;
808
823
809
824
cleanup ( ) ;
810
825
} ) ;
@@ -998,8 +1013,24 @@ describe('WebSocketService', () => {
998
1013
mockWebSocketOptions : { autoConnect : false } ,
999
1014
} ) ;
1000
1015
1001
- // Wait for automatic init() to fail
1016
+ // Service should start in disconnected state
1017
+ expect ( service . getConnectionInfo ( ) . state ) . toBe ( WebSocketState . DISCONNECTED ) ;
1018
+
1019
+ // Use expect.assertions to ensure error handling is tested
1020
+ expect . assertions ( 5 ) ;
1021
+
1022
+ // Start connection and then advance timers to trigger timeout
1023
+ const connectPromise = service . connect ( ) ;
1024
+
1025
+ // Handle the promise rejection properly
1026
+ connectPromise . catch ( ( ) => {
1027
+ // Expected rejection - do nothing to avoid unhandled promise warning
1028
+ } ) ;
1029
+
1002
1030
await completeAsyncOperations ( TEST_CONSTANTS . TIMEOUT_MS + 50 ) ;
1031
+
1032
+ // Wait for connection to fail
1033
+ await expect ( connectPromise ) . rejects . toThrow ( `Failed to connect to WebSocket: Connection timeout after ${ TEST_CONSTANTS . TIMEOUT_MS } ms` ) ;
1003
1034
1004
1035
const info = service . getConnectionInfo ( ) ;
1005
1036
expect ( info . state ) . toBe ( WebSocketState . ERROR ) ;
0 commit comments