EvalueTonSavoir/client/src/__tests__/services/WebsocketService.test.tsx

92 lines
3.6 KiB
TypeScript
Raw Normal View History

2024-03-29 20:08:34 -04:00
//WebsocketService.test.tsx
import { BaseQuestion, parse } from 'gift-pegjs';
2024-03-29 20:08:34 -04:00
import WebsocketService from '../../services/WebsocketService';
import { io, Socket } from 'socket.io-client';
import { ENV_VARIABLES } from 'src/constants';
import { QuestionType } from 'src/Types/QuestionType';
2024-03-29 20:08:34 -04:00
jest.mock('socket.io-client');
describe('WebSocketService', () => {
let mockSocket: Partial<Socket>;
beforeEach(() => {
mockSocket = {
emit: jest.fn(),
disconnect: jest.fn(),
connect: jest.fn()
};
(io as jest.Mock).mockReturnValue(mockSocket);
});
afterEach(() => {
jest.restoreAllMocks();
});
test('connect should initialize socket connection', () => {
WebsocketService.connect(ENV_VARIABLES.VITE_BACKEND_URL);
2024-03-29 20:08:34 -04:00
expect(io).toHaveBeenCalled();
expect(WebsocketService['socket']).toBe(mockSocket);
});
test('disconnect should terminate socket connection', () => {
mockSocket = WebsocketService.connect(ENV_VARIABLES.VITE_BACKEND_URL);
2024-03-29 20:08:34 -04:00
expect(WebsocketService['socket']).toBeTruthy();
WebsocketService.disconnect();
expect(mockSocket.disconnect).toHaveBeenCalled();
expect(WebsocketService['socket']).toBeNull();
});
test('createRoom should emit create-room event', () => {
2025-02-22 00:20:37 -05:00
const roomName = 'Test Room';
2025-03-04 16:43:11 -05:00
WebsocketService.connect(ENV_VARIABLES.VITE_BACKEND_URL);
2025-02-22 00:20:37 -05:00
WebsocketService.createRoom(roomName);
expect(mockSocket.emit).toHaveBeenCalledWith('create-room', roomName);
2024-03-29 20:08:34 -04:00
});
test('nextQuestion should emit next-question event with correct parameters', () => {
const roomName = 'testRoom';
const mockGiftQuestions = parse('A {T}');
const mockQuestions: QuestionType[] = mockGiftQuestions.map((question, index) => {
if (question.type !== "Category")
question.id = (index + 1).toString();
const newMockQuestion = question;
return {question : newMockQuestion as BaseQuestion};
});
mockSocket = WebsocketService.connect(ENV_VARIABLES.VITE_BACKEND_URL);
WebsocketService.nextQuestion({roomName, questions: mockQuestions, questionIndex: 0, isLaunch: false});
const question = mockQuestions[0];
2024-03-29 20:08:34 -04:00
expect(mockSocket.emit).toHaveBeenCalledWith('next-question', { roomName, question });
});
test('launchStudentModeQuiz should emit launch-student-mode event with correct parameters', () => {
const roomName = 'testRoom';
const questions = [{ id: 1, text: 'Sample Question' }];
mockSocket = WebsocketService.connect(ENV_VARIABLES.VITE_BACKEND_URL);
2024-03-29 20:08:34 -04:00
WebsocketService.launchStudentModeQuiz(roomName, questions);
expect(mockSocket.emit).toHaveBeenCalledWith('launch-student-mode', {
roomName,
questions
});
});
test('endQuiz should emit end-quiz event with correct parameters', () => {
const roomName = 'testRoom';
mockSocket = WebsocketService.connect(ENV_VARIABLES.VITE_BACKEND_URL);
2024-03-29 20:08:34 -04:00
WebsocketService.endQuiz(roomName);
expect(mockSocket.emit).toHaveBeenCalledWith('end-quiz', { roomName });
});
test('joinRoom should emit join-room event with correct parameters', () => {
const enteredRoomName = 'testRoom';
const username = 'testUser';
mockSocket = WebsocketService.connect(ENV_VARIABLES.VITE_BACKEND_URL);
2024-03-29 20:08:34 -04:00
WebsocketService.joinRoom(enteredRoomName, username);
expect(mockSocket.emit).toHaveBeenCalledWith('join-room', { enteredRoomName, username });
});
});