mirror of
https://github.com/ets-cfuhrman-pfe/EvalueTonSavoir.git
synced 2025-08-11 21:23:54 -04:00
Some checks failed
CI/CD Pipeline for Backend / build_and_push_backend (push) Failing after 19s
CI/CD Pipeline for Nginx Router / build_and_push_nginx (push) Failing after 18s
CI/CD Pipeline for Frontend / build_and_push_frontend (push) Failing after 18s
Tests / lint-and-tests (client) (push) Failing after 1m5s
Tests / lint-and-tests (server) (push) Failing after 59s
120 lines
4.5 KiB
TypeScript
120 lines
4.5 KiB
TypeScript
//TeacherModeQuiz.test.tsx
|
|
import React from 'react';
|
|
import { render, fireEvent, act } from '@testing-library/react';
|
|
import { screen } from '@testing-library/dom';
|
|
import '@testing-library/jest-dom';
|
|
import { BaseQuestion, MultipleChoiceQuestion, parse } from 'gift-pegjs';
|
|
import TeacherModeQuiz from 'src/components/TeacherModeQuiz/TeacherModeQuiz';
|
|
import { MemoryRouter } from 'react-router-dom';
|
|
import { QuestionType } from 'src/Types/QuestionType';
|
|
import { AnswerSubmissionToBackendType } from 'src/services/WebsocketService';
|
|
|
|
const mockGiftQuestions = parse(
|
|
`::Sample Question 1:: Sample Question 1 {=Option A ~Option B}
|
|
|
|
::Sample Question 2:: Sample Question 2 {=Option A ~Option B}`);
|
|
|
|
const mockQuestions: QuestionType[] = mockGiftQuestions.map((question, index) => {
|
|
if (question.type !== "Category")
|
|
question.id = (index + 1).toString();
|
|
const newMockQuestion = question;
|
|
return {question : newMockQuestion as BaseQuestion};
|
|
});
|
|
|
|
describe('TeacherModeQuiz', () => {
|
|
|
|
|
|
let mockQuestion = mockQuestions[0].question as MultipleChoiceQuestion;
|
|
mockQuestion.id = '1';
|
|
|
|
const mockSubmitAnswer = jest.fn();
|
|
const mockDisconnectWebSocket = jest.fn();
|
|
|
|
let rerender: (ui: React.ReactElement) => void;
|
|
|
|
beforeEach(async () => {
|
|
const utils = render(
|
|
<MemoryRouter>
|
|
<TeacherModeQuiz
|
|
questionInfos={{ question: mockQuestion }}
|
|
answers={Array(mockQuestions.length).fill({} as AnswerSubmissionToBackendType)}
|
|
submitAnswer={mockSubmitAnswer}
|
|
disconnectWebSocket={mockDisconnectWebSocket} />
|
|
</MemoryRouter>
|
|
);
|
|
rerender = utils.rerender;
|
|
});
|
|
|
|
test('renders the initial question', () => {
|
|
|
|
expect(screen.getByText('Question 1')).toBeInTheDocument();
|
|
expect(screen.getByText('Sample Question 1')).toBeInTheDocument();
|
|
expect(screen.getByText('Option A')).toBeInTheDocument();
|
|
expect(screen.getByText('Option B')).toBeInTheDocument();
|
|
expect(screen.getByText('Quitter')).toBeInTheDocument();
|
|
expect(screen.getByText('Répondre')).toBeInTheDocument();
|
|
});
|
|
|
|
test('handles answer submission and displays feedback', () => {
|
|
|
|
act(() => {
|
|
fireEvent.click(screen.getByText('Option A'));
|
|
});
|
|
act(() => {
|
|
fireEvent.click(screen.getByText('Répondre'));
|
|
});
|
|
expect(mockSubmitAnswer).toHaveBeenCalledWith(['Option A'], 1);
|
|
mockSubmitAnswer.mockClear();
|
|
});
|
|
|
|
test('handles shows feedback for an already answered question', () => {
|
|
// Answer the first question
|
|
act(() => {
|
|
fireEvent.click(screen.getByText('Option A'));
|
|
});
|
|
act(() => {
|
|
fireEvent.click(screen.getByText('Répondre'));
|
|
});
|
|
expect(mockSubmitAnswer).toHaveBeenCalledWith(['Option A'], 1);
|
|
mockSubmitAnswer.mockClear();
|
|
mockQuestion = mockQuestions[1].question as MultipleChoiceQuestion;
|
|
// Navigate to the next question by re-rendering with new props
|
|
act(() => {
|
|
rerender(
|
|
<MemoryRouter>
|
|
<TeacherModeQuiz
|
|
questionInfos={{ question: mockQuestion }}
|
|
answers={Array(mockQuestions.length).fill({} as AnswerSubmissionToBackendType)}
|
|
submitAnswer={mockSubmitAnswer}
|
|
disconnectWebSocket={mockDisconnectWebSocket}
|
|
/>
|
|
</MemoryRouter>
|
|
);
|
|
});
|
|
|
|
mockQuestion = mockQuestions[0].question as MultipleChoiceQuestion;
|
|
|
|
act(() => {
|
|
rerender(
|
|
<MemoryRouter>
|
|
<TeacherModeQuiz
|
|
questionInfos={{ question: mockQuestion }}
|
|
answers={Array(mockQuestions.length).fill({} as AnswerSubmissionToBackendType)}
|
|
submitAnswer={mockSubmitAnswer}
|
|
disconnectWebSocket={mockDisconnectWebSocket}
|
|
/>
|
|
</MemoryRouter>
|
|
);
|
|
});
|
|
|
|
// Check if the feedback dialog is shown again
|
|
expect(screen.getByText('Rétroaction')).toBeInTheDocument();
|
|
});
|
|
|
|
test('handles disconnect button click', () => {
|
|
act(() => {
|
|
fireEvent.click(screen.getByText('Quitter'));
|
|
});
|
|
expect(mockDisconnectWebSocket).toHaveBeenCalled();
|
|
});
|
|
});
|