@webex/internal-plugin-mercury 3.11.0 → 3.12.0-next.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -653,6 +653,67 @@ describe('plugin-mercury', () => {
653
653
  });
654
654
  assert.calledOnce(socket._ping);
655
655
  });
656
+
657
+ [
658
+ {
659
+ description: 'manually triggers close handler when socket is still connecting',
660
+ closeOptions: {code: 3001, reason: 'Custom close while connecting'},
661
+ expectedCode: 3001,
662
+ expectedReason: 'Custom close while connecting',
663
+ },
664
+ {
665
+ description:
666
+ 'manually triggers close handler with default code when socket is connecting',
667
+ closeOptions: undefined,
668
+ expectedCode: 1000,
669
+ expectedReason: 'Done',
670
+ },
671
+ ].forEach(({description, closeOptions, expectedCode, expectedReason}) => {
672
+ it(description, async () => {
673
+ const s = new Socket();
674
+ let socketInstance;
675
+
676
+ // Save the current stub and replace it
677
+ const previousStub = Socket.getWebSocketConstructor;
678
+ Socket.getWebSocketConstructor = sinon.stub().callsFake(
679
+ () =>
680
+ function (...args) {
681
+ socketInstance = new MockWebSocket(...args);
682
+ return socketInstance;
683
+ }
684
+ );
685
+
686
+ // open the socket
687
+ s.open('ws://example.com', mockoptions);
688
+
689
+ // Keep socket in CONNECTING state (readyState 0)
690
+ socketInstance.readyState = 0;
691
+
692
+ const closeSpy = sinon.spy();
693
+ s.on('close', closeSpy);
694
+
695
+ // Call close and await the result
696
+ const result = await s.close(closeOptions);
697
+
698
+ // Verify the promise resolved with the correct close event
699
+ assert.equal(result.code, expectedCode);
700
+ assert.equal(result.reason, expectedReason);
701
+
702
+ // Verify close handler was called with expected code/reason
703
+ assert.calledOnce(closeSpy);
704
+ assert.calledWith(closeSpy, {
705
+ code: expectedCode,
706
+ reason: expectedReason,
707
+ });
708
+
709
+ // Verify the underlying socket.close was called with the correct params
710
+ assert.calledOnce(socketInstance.close);
711
+ assert.calledWith(socketInstance.close, expectedCode, expectedReason);
712
+
713
+ // Restore the previous stub
714
+ Socket.getWebSocketConstructor = previousStub;
715
+ });
716
+ });
656
717
  });
657
718
 
658
719
  describe('#send()', () => {