MCPcopy Index your code
hub / github.com/coder/coder / createMockSocket

Function createMockSocket

site/src/utils/reconnectingWebSocket.test.ts:11–31  ·  view source on GitHub ↗

* Minimal mock that satisfies the Closable interface used by the * reconnection utility. Each instance records every `addEventListener` * call and exposes helpers to fire those events.

()

Source from the content-addressed store, hash-verified

9 * call and exposes helpers to fire those events.
10 */
11function createMockSocket() {
12 const listeners: Record<string, Array<(...args: unknown[]) => void>> = {};
13 const socket = {
14 addEventListener: vi.fn(
15 (event: string, handler: (...args: unknown[]) => void) => {
16 if (!listeners[event]) {
17 listeners[event] = [];
18 }
19 listeners[event].push(handler);
20 },
21 ),
22 close: vi.fn(),
23 /** Fire all handlers registered for the given event type. */
24 emit(event: string) {
25 for (const handler of listeners[event] ?? []) {
26 handler();
27 }
28 },
29 };
30 return socket;
31}
32
33const expectReconnectSchedule = (
34 event: { reconnect: ReconnectSchedule; now: number },

Callers 2

makeReconnectFunction · 0.70

Calls 1

pushMethod · 0.80

Tested by

no test coverage detected