|
| 1 | +// Mock LoggedIn and Socket modules before any imports |
| 2 | +jest.mock('../../../LoggedIn', () => ({ |
| 3 | + useGlobal: () => ({ |
| 4 | + loggedIn: true, |
| 5 | + setLoggedIn: jest.fn(), |
| 6 | + }), |
| 7 | +})); |
| 8 | + |
| 9 | +// Mock socket.io-client |
| 10 | +jest.mock('socket.io-client', () => ({ |
| 11 | + __esModule: true, |
| 12 | + default: jest.fn(() => ({ |
| 13 | + on: jest.fn(), |
| 14 | + off: jest.fn(), |
| 15 | + emit: jest.fn(), |
| 16 | + disconnect: jest.fn(), |
| 17 | + })), |
| 18 | +})); |
| 19 | + |
| 20 | +import React from 'react'; |
| 21 | +import { render, screen, fireEvent, waitFor } from '@testing-library/react'; |
| 22 | +import '@testing-library/jest-dom'; |
| 23 | +import ChatInterface from '../ChatInterface'; |
| 24 | + |
| 25 | +const mockChatRooms = [ |
| 26 | + { |
| 27 | + id: 1, |
| 28 | + name: 'Test Group Chat', |
| 29 | + room_type: 'group', |
| 30 | + message_count: 5, |
| 31 | + }, |
| 32 | + { |
| 33 | + id: 2, |
| 34 | + name: null, |
| 35 | + room_type: 'direct', |
| 36 | + message_count: 2, |
| 37 | + }, |
| 38 | +]; |
| 39 | + |
| 40 | +const mockMessages = [ |
| 41 | + { |
| 42 | + id: 1, |
| 43 | + content: 'Hello there!', |
| 44 | + sender_id: 1, |
| 45 | + sender_email: 'user1@example.com', |
| 46 | + created_at: '2025-01-20T10:00:00Z', |
| 47 | + is_deleted: false, |
| 48 | + is_edited: false, |
| 49 | + }, |
| 50 | + { |
| 51 | + id: 2, |
| 52 | + content: 'How are you?', |
| 53 | + sender_id: 2, |
| 54 | + sender_email: 'user2@example.com', |
| 55 | + created_at: '2025-01-20T10:05:00Z', |
| 56 | + is_deleted: false, |
| 57 | + is_edited: false, |
| 58 | + }, |
| 59 | +]; |
| 60 | + |
| 61 | +// Mock fetch |
| 62 | +beforeEach(() => { |
| 63 | + global.fetch = jest.fn(); |
| 64 | + sessionStorage.setItem('token', 'Bearer mocktoken'); |
| 65 | +}); |
| 66 | + |
| 67 | +afterEach(() => { |
| 68 | + jest.restoreAllMocks(); |
| 69 | + sessionStorage.clear(); |
| 70 | +}); |
| 71 | + |
| 72 | +describe('ChatInterface', () => { |
| 73 | + it('renders chat interface when logged in', async () => { |
| 74 | + global.fetch.mockResolvedValueOnce({ |
| 75 | + ok: true, |
| 76 | + json: () => Promise.resolve(mockChatRooms), |
| 77 | + }); |
| 78 | + |
| 79 | + render(<ChatInterface />); |
| 80 | + |
| 81 | + await waitFor(() => { |
| 82 | + expect(screen.getByText('Chats')).toBeInTheDocument(); |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + it('displays "Please log in" message when not logged in', () => { |
| 87 | + // Mock the useGlobal hook to return loggedIn: false |
| 88 | + jest.doMock('../../../LoggedIn', () => ({ |
| 89 | + useGlobal: () => ({ |
| 90 | + loggedIn: false, |
| 91 | + setLoggedIn: jest.fn(), |
| 92 | + }), |
| 93 | + })); |
| 94 | + |
| 95 | + const { ChatInterface: ChatInterfaceNotLoggedIn } = require('../ChatInterface'); |
| 96 | + render(<ChatInterfaceNotLoggedIn />); |
| 97 | + |
| 98 | + expect(screen.getByText('Please log in to access chat.')).toBeInTheDocument(); |
| 99 | + }); |
| 100 | + |
| 101 | + it('fetches and displays chat rooms', async () => { |
| 102 | + global.fetch.mockResolvedValueOnce({ |
| 103 | + ok: true, |
| 104 | + json: () => Promise.resolve(mockChatRooms), |
| 105 | + }); |
| 106 | + |
| 107 | + render(<ChatInterface />); |
| 108 | + |
| 109 | + await waitFor(() => { |
| 110 | + expect(screen.getByText('Test Group Chat')).toBeInTheDocument(); |
| 111 | + expect(screen.getByText('Direct Message')).toBeInTheDocument(); |
| 112 | + }); |
| 113 | + |
| 114 | + expect(global.fetch).toHaveBeenCalledWith( |
| 115 | + expect.stringContaining('/api/chat/rooms'), |
| 116 | + expect.objectContaining({ |
| 117 | + headers: { |
| 118 | + Authorization: 'Bearer mocktoken', |
| 119 | + }, |
| 120 | + }) |
| 121 | + ); |
| 122 | + }); |
| 123 | + |
| 124 | + it('handles room selection and fetches messages', async () => { |
| 125 | + global.fetch |
| 126 | + .mockResolvedValueOnce({ |
| 127 | + ok: true, |
| 128 | + json: () => Promise.resolve(mockChatRooms), |
| 129 | + }) |
| 130 | + .mockResolvedValueOnce({ |
| 131 | + ok: true, |
| 132 | + json: () => Promise.resolve(mockMessages), |
| 133 | + }); |
| 134 | + |
| 135 | + render(<ChatInterface />); |
| 136 | + |
| 137 | + await waitFor(() => { |
| 138 | + expect(screen.getByText('Test Group Chat')).toBeInTheDocument(); |
| 139 | + }); |
| 140 | + |
| 141 | + // Click on a chat room |
| 142 | + fireEvent.click(screen.getByText('Test Group Chat')); |
| 143 | + |
| 144 | + await waitFor(() => { |
| 145 | + expect(global.fetch).toHaveBeenCalledWith( |
| 146 | + expect.stringContaining('/api/chat/messages?room_id=1'), |
| 147 | + expect.objectContaining({ |
| 148 | + headers: { |
| 149 | + Authorization: 'Bearer mocktoken', |
| 150 | + }, |
| 151 | + }) |
| 152 | + ); |
| 153 | + }); |
| 154 | + }); |
| 155 | + |
| 156 | + it('shows create chat modal when "+" button is clicked', async () => { |
| 157 | + global.fetch.mockResolvedValueOnce({ |
| 158 | + ok: true, |
| 159 | + json: () => Promise.resolve(mockChatRooms), |
| 160 | + }); |
| 161 | + |
| 162 | + render(<ChatInterface />); |
| 163 | + |
| 164 | + await waitFor(() => { |
| 165 | + expect(screen.getByText('Chats')).toBeInTheDocument(); |
| 166 | + }); |
| 167 | + |
| 168 | + const createButton = screen.getByText('+'); |
| 169 | + fireEvent.click(createButton); |
| 170 | + |
| 171 | + await waitFor(() => { |
| 172 | + expect(screen.getByText('Create New Chat')).toBeInTheDocument(); |
| 173 | + }); |
| 174 | + }); |
| 175 | + |
| 176 | + it('handles chat creation and updates room list', async () => { |
| 177 | + const newRoom = { |
| 178 | + id: 3, |
| 179 | + name: 'New Test Chat', |
| 180 | + room_type: 'group', |
| 181 | + message_count: 0, |
| 182 | + }; |
| 183 | + |
| 184 | + global.fetch.mockResolvedValueOnce({ |
| 185 | + ok: true, |
| 186 | + json: () => Promise.resolve(mockChatRooms), |
| 187 | + }); |
| 188 | + |
| 189 | + render(<ChatInterface />); |
| 190 | + |
| 191 | + await waitFor(() => { |
| 192 | + expect(screen.getByText('Chats')).toBeInTheDocument(); |
| 193 | + }); |
| 194 | + |
| 195 | + // Simulate chat creation |
| 196 | + const createButton = screen.getByText('+'); |
| 197 | + fireEvent.click(createButton); |
| 198 | + |
| 199 | + await waitFor(() => { |
| 200 | + expect(screen.getByText('Create New Chat')).toBeInTheDocument(); |
| 201 | + }); |
| 202 | + |
| 203 | + // Find and trigger the create chat modal's onChatCreated callback |
| 204 | + // This would typically be done through user interaction in the modal |
| 205 | + // For testing purposes, we'll simulate it directly |
| 206 | + const chatInterface = screen.getByTestId ? screen.getByTestId('chat-interface') : document.querySelector('.chat-container'); |
| 207 | + |
| 208 | + // Simulate successful chat creation by manually calling the handler |
| 209 | + // In a real test, this would be triggered by modal interaction |
| 210 | + }); |
| 211 | + |
| 212 | + it('handles fetch errors gracefully', async () => { |
| 213 | + global.fetch.mockRejectedValueOnce(new Error('Network error')); |
| 214 | + |
| 215 | + const consoleSpy = jest.spyOn(console, 'error').mockImplementation(); |
| 216 | + |
| 217 | + render(<ChatInterface />); |
| 218 | + |
| 219 | + await waitFor(() => { |
| 220 | + expect(consoleSpy).toHaveBeenCalledWith('Error fetching chat rooms:', expect.any(Error)); |
| 221 | + }); |
| 222 | + |
| 223 | + consoleSpy.mockRestore(); |
| 224 | + }); |
| 225 | + |
| 226 | + it('initializes socket connection when logged in', async () => { |
| 227 | + global.fetch.mockResolvedValueOnce({ |
| 228 | + ok: true, |
| 229 | + json: () => Promise.resolve(mockChatRooms), |
| 230 | + }); |
| 231 | + |
| 232 | + render(<ChatInterface />); |
| 233 | + |
| 234 | + await waitFor(() => { |
| 235 | + expect(screen.getByText('Chats')).toBeInTheDocument(); |
| 236 | + }); |
| 237 | + |
| 238 | + // Verify socket.io was called with correct parameters |
| 239 | + const io = require('socket.io-client').default; |
| 240 | + await waitFor(() => { |
| 241 | + expect(io).toHaveBeenCalledWith( |
| 242 | + expect.stringContaining(''), |
| 243 | + expect.objectContaining({ |
| 244 | + auth: { token: 'Bearer mocktoken' }, |
| 245 | + }) |
| 246 | + ); |
| 247 | + }); |
| 248 | + }); |
| 249 | + |
| 250 | + it('cleans up socket connection on unmount', async () => { |
| 251 | + global.fetch.mockResolvedValueOnce({ |
| 252 | + ok: true, |
| 253 | + json: () => Promise.resolve(mockChatRooms), |
| 254 | + }); |
| 255 | + |
| 256 | + const { unmount } = render(<ChatInterface />); |
| 257 | + |
| 258 | + await waitFor(() => { |
| 259 | + expect(screen.getByText('Chats')).toBeInTheDocument(); |
| 260 | + }); |
| 261 | + |
| 262 | + const io = require('socket.io-client').default; |
| 263 | + const mockSocket = io(); |
| 264 | + |
| 265 | + unmount(); |
| 266 | + |
| 267 | + expect(mockSocket.disconnect).toHaveBeenCalled(); |
| 268 | + }); |
| 269 | +}); |
0 commit comments