forked from Vero-protocol/vero-guardian-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseSocketIO.test.ts
More file actions
173 lines (142 loc) · 4.69 KB
/
Copy pathuseSocketIO.test.ts
File metadata and controls
173 lines (142 loc) · 4.69 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
import { renderHook, act } from '@testing-library/react';
import type { Socket } from 'socket.io-client';
import { useSocketIO } from '@/hooks/useSocketIO';
import {
getSocket,
resetSocketClientForTests,
} 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 mockUseEvents = jest.fn().mockReturnValue({
emit: jest.fn(),
timeline: [],
clear: jest.fn(),
});
jest.mock('@/hooks/useEvents', () => ({
useEvents: (opts?: { maxEvents?: number }) => mockUseEvents(opts),
}));
const mockInvalidateChainState = jest.fn();
jest.mock('@/hooks/useChainState', () => ({
invalidateChainState: (...args: unknown[]) => mockInvalidateChainState(...args),
}));
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('useSocketIO', () => {
it('returns initial disconnected status', () => {
const { result } = renderHook(() => useSocketIO({ autoConnect: false }));
expect(result.current.status).toBe('disconnected');
expect(result.current.isConnected).toBe(false);
expect(result.current.lastEvent).toBeNull();
});
it('connects on autoConnect by default', () => {
renderHook(() => useSocketIO());
// Module-level connect is called; socket should be created
expect(getSocket()).not.toBeNull();
});
it('respects autoConnect=false', () => {
renderHook(() => useSocketIO({ autoConnect: false }));
expect(getSocket()).toBeNull();
});
it('connect function triggers connection', () => {
const { result } = renderHook(() => useSocketIO({ autoConnect: false }));
act(() => {
result.current.connect();
});
expect(getSocket()).not.toBeNull();
});
it('disconnect function disconnects', () => {
const { result } = renderHook(() => useSocketIO({ autoConnect: true }));
act(() => {
result.current.disconnect();
});
expect(getSocket()).toBeNull();
});
it('receives lastEvent when Socket.IO event fires', () => {
const { result } = renderHook(() =>
useSocketIO({ autoConnect: true, invalidateOnEvent: false }),
);
const socket = getMockedSocket();
const onAnyCalls = (socket.onAny as jest.Mock).mock.calls;
const onAnyHandler = onAnyCalls[0]?.[0];
act(() => {
if (onAnyHandler) {
onAnyHandler('vote:cast', { prId: 42 });
}
});
expect(result.current.lastEvent).toEqual({
event: 'vote:cast',
data: { prId: 42 },
});
});
it('calls invalidateChainState on events', () => {
renderHook(() => useSocketIO({ autoConnect: true, invalidateOnEvent: true }));
const socket = getMockedSocket();
const onAnyCalls = (socket.onAny as jest.Mock).mock.calls;
const onAnyHandler = onAnyCalls[0]?.[0];
act(() => {
if (onAnyHandler) {
onAnyHandler('pr:update', { id: 42 });
}
});
expect(mockInvalidateChainState).toHaveBeenCalledWith(
expect.arrayContaining(['prs', 'dashboard']),
'websocket',
);
});
it('invokes useEvents emit when events arrive', () => {
const mockEmit = jest.fn();
mockUseEvents.mockReturnValue({ emit: mockEmit, timeline: [], clear: jest.fn() });
renderHook(() => useSocketIO({ autoConnect: true }));
const socket = getMockedSocket();
const onAnyCalls = (socket.onAny as jest.Mock).mock.calls;
const onAnyHandler = onAnyCalls[0]?.[0];
act(() => {
if (onAnyHandler) {
onAnyHandler('reputation:change', { score: 100 });
}
});
expect(mockEmit).toHaveBeenCalledWith(
expect.objectContaining({ type: 'reputation_change', resource: 'socket.io' }),
);
});
it('updateToken calls socket client updateAuthToken', () => {
const { result } = renderHook(() => useSocketIO({ autoConnect: true }));
const socket = getMockedSocket();
act(() => {
result.current.updateToken('new-token');
});
expect(socket.disconnect as jest.Mock).toHaveBeenCalled();
expect(socket.connect as jest.Mock).toHaveBeenCalled();
});
});