@@ -272,7 +272,44 @@ test('shared websockets each have callbacks invoked as if unshared', async (done
272
272
done ( ) ;
273
273
} )
274
274
275
- test ( 'Options#fromSocketIO changes the WS url to support socket.io\'s required query params' , async ( done ) => {
275
+
276
+ test ( 'shared websockets have their onClose callbacks invoked when all subscriber components disconnect' , async ( done ) => {
277
+ const initialProps = { initialValue : true } ;
278
+ const onCloseFn1 = jest . fn ( ) ;
279
+ const onCloseFn2 = jest . fn ( ) ;
280
+ const onCloseFn3 = jest . fn ( ) ;
281
+
282
+ const { rerender : rerender1 } = renderHook (
283
+ ( { initialValue } ) => useWebSocket ( URL , { share : true , onClose : onCloseFn1 } , initialValue ) ,
284
+ { initialProps }
285
+ ) ;
286
+ await server . connected ;
287
+
288
+ const { rerender : rerender2 } = renderHook (
289
+ ( { initialValue } ) => useWebSocket ( URL , { share : true , onClose : onCloseFn2 } , initialValue ) ,
290
+ { initialProps }
291
+ ) ;
292
+ await server . connected ;
293
+
294
+ const { rerender : rerender3 } = renderHook (
295
+ ( { initialValue } ) => useWebSocket ( URL , { share : true , onClose : onCloseFn3 } , initialValue ) ,
296
+ { initialProps }
297
+ ) ;
298
+ await server . connected ;
299
+
300
+ rerender1 ( { initialValue : false } ) ;
301
+ rerender2 ( { initialValue : false } ) ;
302
+ rerender3 ( { initialValue : false } ) ;
303
+
304
+ await sleep ( 500 ) ;
305
+
306
+ expect ( onCloseFn1 ) . toHaveBeenCalledTimes ( 1 ) ;
307
+ expect ( onCloseFn2 ) . toHaveBeenCalledTimes ( 1 ) ;
308
+ expect ( onCloseFn3 ) . toHaveBeenCalledTimes ( 1 ) ;
309
+ done ( ) ;
310
+ } ) ;
311
+
312
+ test ( "Options#fromSocketIO changes the WS url to support socket.io's required query params" , async ( done ) => {
276
313
options . fromSocketIO = true ;
277
314
278
315
const {
@@ -500,4 +537,4 @@ test('Options#eventSourceOptions, if provided, instantiates an EventSource inste
500
537
done ( ) ;
501
538
} ) ;
502
539
503
- //TODO: Write companion tests for useSocketIO
540
+ //TODO: Write companion tests for useSocketIO
0 commit comments