Skip to content

Commit 7920364

Browse files
authored
feat: export initialized socket client (#4304)
1 parent 4e7800e commit 7920364

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

client-src/socket.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ const Client =
1616

1717
let retries = 0;
1818
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;
2024

2125
/**
2226
* @param {string} url

test/client/socket-helper.test.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"use strict";
66

77
describe("socket", () => {
8-
afterEach(() => {
8+
beforeEach(() => {
99
jest.resetAllMocks();
1010
jest.resetModules();
1111
});
@@ -77,4 +77,19 @@ describe("socket", () => {
7777
expect(mockClientInstance.onMessage.mock.calls).toMatchSnapshot();
7878
expect(mockHandler.mock.calls).toMatchSnapshot();
7979
});
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+
});
8095
});

0 commit comments

Comments
 (0)