@@ -410,19 +410,43 @@ describe("Tests using SimpleSocketIOClient", () => {
410410 `${ PACKET_TYPES . event } ${ expectedDataString }  ` , 
411411 ) ; 
412412 } ) ; 
413+  test ( "emit triggers a reconnect if not connected" ,  ( )  =>  { 
414+  client . webSocket  =  createWebSocketMock ( ) ; 
415+  client . webSocket . readyState  =  WebSocket . CLOSED ; 
416+ 
417+  const  reconnectSpy  =  vi . spyOn ( client ,  "reconnect" ) ; 
418+ 
419+  const  eventName  =  "testEvent" ; 
420+  const  eventData  =  {  foo : "bar"  } ; 
421+ 
422+  client . emit ( eventName ,  eventData ) ; 
423+ 
424+  expect ( reconnectSpy ) . toHaveBeenCalledTimes ( 1 ) ; 
425+ 
426+  const  expectedPayload  =  { 
427+  name : eventName , 
428+  args : [ eventData ] , 
429+  } ; 
430+  const  expectedDataString  =  `:::${ JSON . stringify ( expectedPayload ) }  ` ; 
431+  expect ( client . packetQueue ) . toContainEqual ( 
432+  `${ PACKET_TYPES . event } ${ expectedDataString }  ` , 
433+  ) ; 
434+ 
435+  reconnectSpy . mockRestore ( ) ; 
436+  } ) ; 
413437 describe ( "Reconnection tests" ,  ( )  =>  { 
414-  test ( "attemptReconnect method initialises websocket again" ,  ( )  =>  { 
438+  test ( "attemptReconnect method initialises websocket again" ,  async   ( )  =>  { 
415439 client . initializeWebSocket  =  vi . fn ( ) ; 
416440
417-  client . attemptReconnect ( ) ; 
441+  await   client . attemptReconnect ( ) ; 
418442
419443 expect ( client . initializeWebSocket ) . toHaveBeenCalledTimes ( 1 ) ; 
420444 } ) ; 
421445
422-  test ( "attemptReconnect method increments attempts count" ,  ( )  =>  { 
446+  test ( "attemptReconnect method increments attempts count" ,  async   ( )  =>  { 
423447 const  initialAttempts  =  client . reconnectionAttempts ; 
424448
425-  client . attemptReconnect ( ) ; 
449+  await   client . attemptReconnect ( ) ; 
426450
427451 expect ( client . reconnectionAttempts ) . toBe ( initialAttempts  +  1 ) ; 
428452 } ) ; 
@@ -456,7 +480,7 @@ describe("Tests using SimpleSocketIOClient", () => {
456480 // Reconnect should not be called yet 
457481 expect ( client . attemptReconnect ) . toHaveBeenCalledTimes ( 0 ) ; 
458482 } ) ; 
459-  test ( "reconnect method exponentially increase delay for every attempt, stopping at the max value" ,  ( )  =>  { 
483+  test ( "reconnect method exponentially increase delay for every attempt, stopping at the max value" ,  async   ( )  =>  { 
460484 const  originalRandom  =  Math . random ; 
461485 Math . random  =  vi . fn ( ) . mockReturnValue ( 1 ) ; 
462486 vi . useFakeTimers ( ) ; 
@@ -468,6 +492,7 @@ describe("Tests using SimpleSocketIOClient", () => {
468492 const  expectedMaxDelay  =  expectedMinDelay  *  1.5 ; 
469493 client . reconnecting  =  false ;  // Since it never gets to the actual fail state triggered by 
470494 client . reconnect ( ) ; 
495+  await  client . initializing ; 
471496 vi . advanceTimersByTime ( expectedMaxDelay  +  1 ) ; 
472497 expect ( client . attemptReconnect ) . toHaveBeenCalledTimes ( i  +  1 ) ; 
473498 } 
0 commit comments