forked from Vero-protocol/vero-guardian-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocketClient.test.ts
More file actions
186 lines (155 loc) · 4.71 KB
/
Copy pathsocketClient.test.ts
File metadata and controls
186 lines (155 loc) · 4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import type { Socket } from 'socket.io-client';
import {
connectSocket,
disconnectSocket,
getSocketStatus,
onSocketEvent,
onSocketStatus,
onSocketError,
resetSocketClientForTests,
emitSocketEvent,
getSocket,
} from '@/services/socketClient';
type MockedSocket = jest.Mocked<Pick<Socket, 'on' | 'off' | 'emit' | 'disconnect' | 'connect' | 'removeAllListeners'>> & {
onAny: jest.Mock;
connected: boolean;
auth: Record<string, unknown>;
};
function getMockedSocket(): MockedSocket {
return getSocket() as unknown as MockedSocket;
}
jest.mock('socket.io-client', () => {
const mockSocket: MockedSocket = {
on: jest.fn(),
off: jest.fn(),
emit: jest.fn(),
disconnect: jest.fn(),
connect: jest.fn(),
removeAllListeners: jest.fn(),
connected: false,
auth: {},
onAny: jest.fn(),
};
return {
io: jest.fn(() => mockSocket),
};
});
const { io } = jest.requireMock('socket.io-client') as {
io: jest.Mock;
};
const OLD_ENV = process.env;
beforeEach(() => {
jest.resetModules();
process.env = { ...OLD_ENV };
process.env.NEXT_PUBLIC_SOCKET_IO_URL = 'http://localhost:3001';
});
afterEach(() => {
process.env = OLD_ENV;
resetSocketClientForTests();
jest.clearAllMocks();
});
describe('socketClient', () => {
it('connects with URL from env var', () => {
connectSocket();
expect(io).toHaveBeenCalledWith(
'http://localhost:3001',
expect.objectContaining({ autoConnect: true }),
);
});
it('connects with a custom URL', () => {
connectSocket('ws://custom:8080');
expect(io).toHaveBeenCalledWith(
'ws://custom:8080',
expect.objectContaining({ autoConnect: true }),
);
});
it('passes auth token when provided', () => {
connectSocket(undefined, 'test-token');
expect(io).toHaveBeenCalledWith(
'http://localhost:3001',
expect.objectContaining({ auth: { token: 'test-token' } }),
);
});
it('throws when no URL is configured', () => {
process.env.NEXT_PUBLIC_SOCKET_IO_URL = '';
expect(() => connectSocket()).toThrow('Socket.IO URL not configured');
});
it('returns connection status', () => {
expect(getSocketStatus()).toBe('disconnected');
connectSocket();
expect(getSocketStatus()).toBe('connecting');
});
it('notifies status listeners', () => {
const listener = jest.fn();
const unsub = onSocketStatus(listener);
connectSocket();
const mockSocket = getMockedSocket();
const onCalls = (mockSocket.on as jest.Mock).mock.calls;
const connectHandler = onCalls.find(
([event]: [string]) => event === 'connect',
)?.[1];
if (connectHandler) {
connectHandler();
}
expect(listener).toHaveBeenCalledWith('connected');
unsub();
});
it('notifies event listeners on any event', () => {
const listener = jest.fn();
const unsub = onSocketEvent(listener);
connectSocket();
const mockSocket = getMockedSocket();
const onAnyCalls = (mockSocket.onAny as jest.Mock).mock.calls;
const onAnyHandler = onAnyCalls[0]?.[0];
if (onAnyHandler) {
onAnyHandler('pr:update', { id: 42 });
}
expect(listener).toHaveBeenCalledWith({
event: 'pr:update',
data: { id: 42 },
});
unsub();
});
it('notifies error listeners on connect_error', () => {
const listener = jest.fn();
const unsub = onSocketError(listener);
connectSocket();
const mockSocket = getMockedSocket();
const onCalls = (mockSocket.on as jest.Mock).mock.calls;
const errorHandler = onCalls.find(
([event]: [string]) => event === 'connect_error',
)?.[1];
if (errorHandler) {
errorHandler(new Error('connection refused'));
}
expect(listener).toHaveBeenCalledWith('connection refused');
unsub();
});
it('emits events', () => {
connectSocket();
const mockSocket = getMockedSocket();
emitSocketEvent('test:event', { foo: 'bar' });
expect(mockSocket.emit as jest.Mock).toHaveBeenCalledWith('test:event', { foo: 'bar' });
});
it('disconnects and resets', () => {
connectSocket();
disconnectSocket();
expect(getSocketStatus()).toBe('disconnected');
expect(getSocket()).toBeNull();
});
it('reset cleans all listeners', () => {
const statusListener = jest.fn();
const eventListener = jest.fn();
const errorListener = jest.fn();
onSocketStatus(statusListener);
onSocketEvent(eventListener);
onSocketError(errorListener);
resetSocketClientForTests();
// After reset, previously registered listeners are cleared
// Connect to trigger status change
connectSocket();
expect(statusListener).not.toHaveBeenCalled();
expect(eventListener).not.toHaveBeenCalled();
expect(errorListener).not.toHaveBeenCalled();
});
});