File tree 2 files changed +21
-2
lines changed
2 files changed +21
-2
lines changed Original file line number Diff line number Diff line change @@ -16,7 +16,11 @@ const Client =
16
16
17
17
let retries = 0 ;
18
18
let 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 ;
20
24
21
25
/**
22
26
* @param {string } url
Original file line number Diff line number Diff line change 5
5
"use strict" ;
6
6
7
7
describe ( "socket" , ( ) => {
8
- afterEach ( ( ) => {
8
+ beforeEach ( ( ) => {
9
9
jest . resetAllMocks ( ) ;
10
10
jest . resetModules ( ) ;
11
11
} ) ;
@@ -77,4 +77,19 @@ describe("socket", () => {
77
77
expect ( mockClientInstance . onMessage . mock . calls ) . toMatchSnapshot ( ) ;
78
78
expect ( mockHandler . mock . calls ) . toMatchSnapshot ( ) ;
79
79
} ) ;
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
+ } ) ;
80
95
} ) ;
You can’t perform that action at this time.
0 commit comments