|
| 1 | +import { renderHook, act } from '@testing-library/react'; |
| 2 | +import type { Socket } from 'socket.io-client'; |
| 3 | +import { useSocketIO } from '@/hooks/useSocketIO'; |
| 4 | +import { |
| 5 | + getSocket, |
| 6 | + resetSocketClientForTests, |
| 7 | +} from '@/services/socketClient'; |
| 8 | + |
| 9 | +type MockedSocket = jest.Mocked<Pick<Socket, 'on' | 'off' | 'emit' | 'disconnect' | 'connect' | 'removeAllListeners'>> & { |
| 10 | + onAny: jest.Mock; |
| 11 | + connected: boolean; |
| 12 | + auth: Record<string, unknown>; |
| 13 | +}; |
| 14 | + |
| 15 | +function getMockedSocket(): MockedSocket { |
| 16 | + return getSocket() as unknown as MockedSocket; |
| 17 | +} |
| 18 | + |
| 19 | +jest.mock('socket.io-client', () => { |
| 20 | + const mockSocket: MockedSocket = { |
| 21 | + on: jest.fn(), |
| 22 | + off: jest.fn(), |
| 23 | + emit: jest.fn(), |
| 24 | + disconnect: jest.fn(), |
| 25 | + connect: jest.fn(), |
| 26 | + removeAllListeners: jest.fn(), |
| 27 | + connected: false, |
| 28 | + auth: {}, |
| 29 | + onAny: jest.fn(), |
| 30 | + }; |
| 31 | + return { |
| 32 | + io: jest.fn(() => mockSocket), |
| 33 | + }; |
| 34 | +}); |
| 35 | + |
| 36 | +const mockUseEvents = jest.fn().mockReturnValue({ |
| 37 | + emit: jest.fn(), |
| 38 | + timeline: [], |
| 39 | + clear: jest.fn(), |
| 40 | +}); |
| 41 | + |
| 42 | +jest.mock('@/hooks/useEvents', () => ({ |
| 43 | + useEvents: (opts?: { maxEvents?: number }) => mockUseEvents(opts), |
| 44 | +})); |
| 45 | + |
| 46 | +const mockInvalidateChainState = jest.fn(); |
| 47 | +jest.mock('@/hooks/useChainState', () => ({ |
| 48 | + invalidateChainState: (...args: unknown[]) => mockInvalidateChainState(...args), |
| 49 | +})); |
| 50 | + |
| 51 | +const OLD_ENV = process.env; |
| 52 | + |
| 53 | +beforeEach(() => { |
| 54 | + jest.resetModules(); |
| 55 | + process.env = { ...OLD_ENV }; |
| 56 | + process.env.NEXT_PUBLIC_SOCKET_IO_URL = 'http://localhost:3001'; |
| 57 | +}); |
| 58 | + |
| 59 | +afterEach(() => { |
| 60 | + process.env = OLD_ENV; |
| 61 | + resetSocketClientForTests(); |
| 62 | + jest.clearAllMocks(); |
| 63 | +}); |
| 64 | + |
| 65 | +describe('useSocketIO', () => { |
| 66 | + it('returns initial disconnected status', () => { |
| 67 | + const { result } = renderHook(() => useSocketIO({ autoConnect: false })); |
| 68 | + expect(result.current.status).toBe('disconnected'); |
| 69 | + expect(result.current.isConnected).toBe(false); |
| 70 | + expect(result.current.lastEvent).toBeNull(); |
| 71 | + }); |
| 72 | + |
| 73 | + it('connects on autoConnect by default', () => { |
| 74 | + renderHook(() => useSocketIO()); |
| 75 | + expect(getSocket()).not.toBeNull(); |
| 76 | + }); |
| 77 | + |
| 78 | + it('respects autoConnect=false', () => { |
| 79 | + renderHook(() => useSocketIO({ autoConnect: false })); |
| 80 | + expect(getSocket()).toBeNull(); |
| 81 | + }); |
| 82 | + |
| 83 | + it('connect function triggers connection', () => { |
| 84 | + const { result } = renderHook(() => useSocketIO({ autoConnect: false })); |
| 85 | + act(() => { result.current.connect(); }); |
| 86 | + expect(getSocket()).not.toBeNull(); |
| 87 | + }); |
| 88 | + |
| 89 | + it('disconnect function disconnects', () => { |
| 90 | + const { result } = renderHook(() => useSocketIO({ autoConnect: true })); |
| 91 | + act(() => { result.current.disconnect(); }); |
| 92 | + expect(getSocket()).toBeNull(); |
| 93 | + }); |
| 94 | + |
| 95 | + it('receives lastEvent when Socket.IO event fires', () => { |
| 96 | + const { result } = renderHook(() => |
| 97 | + useSocketIO({ autoConnect: true, invalidateOnEvent: false }), |
| 98 | + ); |
| 99 | + |
| 100 | + const socket = getMockedSocket(); |
| 101 | + const onAnyCalls = (socket.onAny as jest.Mock).mock.calls; |
| 102 | + const onAnyHandler = onAnyCalls[0]?.[0]; |
| 103 | + |
| 104 | + act(() => { |
| 105 | + if (onAnyHandler) onAnyHandler('vote:cast', { prId: 42 }); |
| 106 | + }); |
| 107 | + |
| 108 | + expect(result.current.lastEvent).toEqual({ |
| 109 | + event: 'vote:cast', |
| 110 | + data: { prId: 42 }, |
| 111 | + }); |
| 112 | + }); |
| 113 | + |
| 114 | + it('calls invalidateChainState on events', () => { |
| 115 | + renderHook(() => useSocketIO({ autoConnect: true, invalidateOnEvent: true })); |
| 116 | + |
| 117 | + const socket = getMockedSocket(); |
| 118 | + const onAnyCalls = (socket.onAny as jest.Mock).mock.calls; |
| 119 | + const onAnyHandler = onAnyCalls[0]?.[0]; |
| 120 | + |
| 121 | + act(() => { |
| 122 | + if (onAnyHandler) onAnyHandler('pr:update', { id: 42 }); |
| 123 | + }); |
| 124 | + |
| 125 | + expect(mockInvalidateChainState).toHaveBeenCalledWith( |
| 126 | + expect.arrayContaining(['prs', 'dashboard']), |
| 127 | + 'websocket', |
| 128 | + ); |
| 129 | + }); |
| 130 | + |
| 131 | + it('invokes useEvents emit when events arrive', () => { |
| 132 | + const mockEmit = jest.fn(); |
| 133 | + mockUseEvents.mockReturnValue({ emit: mockEmit, timeline: [], clear: jest.fn() }); |
| 134 | + |
| 135 | + renderHook(() => useSocketIO({ autoConnect: true })); |
| 136 | + |
| 137 | + const socket = getMockedSocket(); |
| 138 | + const onAnyCalls = (socket.onAny as jest.Mock).mock.calls; |
| 139 | + const onAnyHandler = onAnyCalls[0]?.[0]; |
| 140 | + |
| 141 | + act(() => { |
| 142 | + if (onAnyHandler) onAnyHandler('reputation:change', { score: 100 }); |
| 143 | + }); |
| 144 | + |
| 145 | + expect(mockEmit).toHaveBeenCalledWith( |
| 146 | + expect.objectContaining({ type: 'reputation_change', resource: 'socket.io' }), |
| 147 | + ); |
| 148 | + }); |
| 149 | + |
| 150 | + it('updateToken calls socket client updateAuthToken', () => { |
| 151 | + const { result } = renderHook(() => useSocketIO({ autoConnect: true })); |
| 152 | + |
| 153 | + const socket = getMockedSocket(); |
| 154 | + |
| 155 | + act(() => { result.current.updateToken('new-token'); }); |
| 156 | + |
| 157 | + expect(socket.disconnect as jest.Mock).toHaveBeenCalled(); |
| 158 | + expect(socket.connect as jest.Mock).toHaveBeenCalled(); |
| 159 | + }); |
| 160 | +}); |
0 commit comments