File tree Expand file tree Collapse file tree 2 files changed +21
-2
lines changed Expand file tree Collapse file tree 2 files changed +21
-2
lines changed Original file line number Diff line number Diff line change @@ -16,7 +16,11 @@ const Client =
1616
1717let retries = 0 ;
1818let maxRetries = 10 ;
19- let client = null ;
19+
20+ // Initialized client is exported so external consumers can utilize the same instance
21+ // It is mutable to enforce singleton
22+ // eslint-disable-next-line import/no-mutable-exports
23+ export let client = null ;
2024
2125/**
2226 * @param {string } url
Original file line number Diff line number Diff line change 55"use strict" ;
66
77describe ( "socket" , ( ) => {
8- afterEach ( ( ) => {
8+ beforeEach ( ( ) => {
99 jest . resetAllMocks ( ) ;
1010 jest . resetModules ( ) ;
1111 } ) ;
@@ -77,4 +77,19 @@ describe("socket", () => {
7777 expect ( mockClientInstance . onMessage . mock . calls ) . toMatchSnapshot ( ) ;
7878 expect ( mockHandler . mock . calls ) . toMatchSnapshot ( ) ;
7979 } ) ;
80+
81+ it ( "should export initialized client" , ( ) => {
82+ const socket = require ( "../../client/socket" ) . default ;
83+
84+ const nonInitializedInstance = require ( "../../client/socket" ) . client ;
85+ expect ( nonInitializedInstance ) . toBe ( null ) ;
86+
87+ socket ( "my.url" , { } ) ;
88+
89+ const initializedInstance = require ( "../../client/socket" ) . client ;
90+ expect ( initializedInstance ) . not . toBe ( null ) ;
91+ expect ( typeof initializedInstance . onClose ) . toBe ( "function" ) ;
92+ expect ( typeof initializedInstance . onMessage ) . toBe ( "function" ) ;
93+ expect ( typeof initializedInstance . onOpen ) . toBe ( "function" ) ;
94+ } ) ;
8095} ) ;
You can’t perform that action at this time.
0 commit comments