Path: blob/master/src/packages/conat/core/client.reconnect.test.ts
14422 views
/*1DEVELOPMENT:23pnpm test ./client.reconnect.test.ts4*/56function mockSocketIO({ emitWithAck = jest.fn() } = {}) {7const socket = {8on: jest.fn(),9emit: jest.fn(),10disconnect: jest.fn(),11close: jest.fn(),12timeout: jest.fn(() => ({ emitWithAck })),13io: {14on: jest.fn(),15connect: jest.fn(),16disconnect: jest.fn(),17},18};19const connectToSocketIO = jest.fn(() => socket);2021jest.doMock("socket.io-client", () => ({22connect: connectToSocketIO,23}));2425return { socket, connectToSocketIO, emitWithAck };26}2728describe("core client socket.io reconnect policy", () => {29it("respects reconnection false passed by callers", async () => {30jest.resetModules();3132const { connectToSocketIO } = mockSocketIO();3334const { connect } = require("./client");35const client = connect({36address: "http://example.com",37reconnection: false,38});3940expect(connectToSocketIO).toHaveBeenCalledWith(41expect.any(String),42expect.objectContaining({43reconnection: false,44}),45);4647client.close();48});4950it("does not auto-connect when callers pass autoConnect false", async () => {51jest.resetModules();5253const { connectToSocketIO, socket } = mockSocketIO();5455const { connect } = require("./client");56const client = connect({57address: "http://example.com",58autoConnect: false,59noCache: true,60});6162expect(connectToSocketIO).toHaveBeenCalledWith(63expect.any(String),64expect.objectContaining({65autoConnect: false,66}),67);68expect(socket.io.connect).not.toHaveBeenCalled();6970client.connect();71expect(socket.io.connect).toHaveBeenCalledTimes(1);7273client.close();74});75});7677describe("core client subscription resync on reconnect", () => {78it("does not unsubscribe subjects the client still wants during resync", async () => {79jest.resetModules();8081const emitWithAck = jest82.fn()83.mockResolvedValueOnce(["wanted.subject"])84.mockResolvedValueOnce([]);85mockSocketIO({ emitWithAck });8687const { Client } = require("./client");88const client = new Client({89address: "http://example.com",90autoConnect: false,91noCache: true,92});93const anyClient = client as any;94anyClient.info = { user: { hub_id: "hub" } };95anyClient.state = "connected";96anyClient.queueGroups = { "wanted.subject": "0" };9798const stable = await anyClient.syncSubscriptions0(1000);99100expect(stable).toBe(true);101expect(emitWithAck).toHaveBeenCalledTimes(1);102expect(emitWithAck).toHaveBeenCalledWith("subscriptions", null);103104client.close();105});106107it("unsubscribes only server-side extras during resync", async () => {108jest.resetModules();109110const emitWithAck = jest111.fn()112.mockResolvedValueOnce(["wanted.subject", "stale.subject"])113.mockResolvedValueOnce([]);114mockSocketIO({ emitWithAck });115116const { Client } = require("./client");117const client = new Client({118address: "http://example.com",119autoConnect: false,120noCache: true,121});122const anyClient = client as any;123anyClient.info = { user: { hub_id: "hub" } };124anyClient.state = "connected";125anyClient.queueGroups = { "wanted.subject": "0" };126127const stable = await anyClient.syncSubscriptions0(1000);128129expect(stable).toBe(false);130expect(emitWithAck).toHaveBeenNthCalledWith(1, "subscriptions", null);131expect(emitWithAck).toHaveBeenNthCalledWith(2, "unsubscribe", [132{ subject: "stale.subject" },133]);134135client.close();136});137});138139140