Skip to content

Commit

Permalink
IMPROVEMENT: test the onClose callback for each hook consuming compon…
Browse files Browse the repository at this point in the history
…ent is invoked when all subscribers disconnect
  • Loading branch information
Davi de Medeiros committed Dec 20, 2021
1 parent 7ce0e26 commit a0ab1b7
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 additions & 2 deletions src/lib/use-websocket.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,44 @@ test('shared websockets each have callbacks invoked as if unshared', async (done
done();
})

test('Options#fromSocketIO changes the WS url to support socket.io\'s required query params', async (done) => {

test('shared websockets have their onClose callbacks invoked when all subscriber components disconnect', async (done) => {
const initialProps = { initialValue: true };
const onCloseFn1 = jest.fn();
const onCloseFn2 = jest.fn();
const onCloseFn3 = jest.fn();

const { rerender: rerender1 } = renderHook(
({ initialValue }) => useWebSocket(URL, { share: true, onClose: onCloseFn1 }, initialValue),
{ initialProps }
);
await server.connected;

const { rerender: rerender2 } = renderHook(
({ initialValue }) => useWebSocket(URL, { share: true, onClose: onCloseFn2 }, initialValue),
{ initialProps }
);
await server.connected;

const { rerender: rerender3 } = renderHook(
({ initialValue }) => useWebSocket(URL, { share: true, onClose: onCloseFn3 }, initialValue),
{ initialProps }
);
await server.connected;

rerender1({ initialValue: false });
rerender2({ initialValue: false });
rerender3({ initialValue: false });

await sleep(500);

expect(onCloseFn1).toHaveBeenCalledTimes(1);
expect(onCloseFn2).toHaveBeenCalledTimes(1);
expect(onCloseFn3).toHaveBeenCalledTimes(1);
done();
});

test("Options#fromSocketIO changes the WS url to support socket.io's required query params", async (done) => {
options.fromSocketIO = true;

const {
Expand Down Expand Up @@ -500,4 +537,4 @@ test('Options#eventSourceOptions, if provided, instantiates an EventSource inste
done();
});

//TODO: Write companion tests for useSocketIO
//TODO: Write companion tests for useSocketIO

0 comments on commit a0ab1b7

Please sign in to comment.