Skip to content

Commit 979b406

Browse files
author
Davi de Medeiros
committed
test the onClose callback for each hook consuming component is invoked when all subscribers disconnect
1 parent 7ce0e26 commit 979b406

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

src/lib/use-websocket.test.ts

+39-2
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,44 @@ test('shared websockets each have callbacks invoked as if unshared', async (done
272272
done();
273273
})
274274

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) => {
276313
options.fromSocketIO = true;
277314

278315
const {
@@ -500,4 +537,4 @@ test('Options#eventSourceOptions, if provided, instantiates an EventSource inste
500537
done();
501538
});
502539

503-
//TODO: Write companion tests for useSocketIO
540+
//TODO: Write companion tests for useSocketIO

0 commit comments

Comments
 (0)