2024-03-29 20:08:34 -04:00
|
|
|
//TeacherModeQuiz.test.tsx
|
2025-01-11 02:22:14 -05:00
|
|
|
import React from 'react';
|
2024-09-15 21:41:24 -04:00
|
|
|
import { render, fireEvent, act } from '@testing-library/react';
|
|
|
|
|
import { screen } from '@testing-library/dom';
|
2024-03-29 20:08:34 -04:00
|
|
|
import '@testing-library/jest-dom';
|
2025-03-07 16:33:57 -05:00
|
|
|
import { BaseQuestion, MultipleChoiceQuestion, parse } from 'gift-pegjs';
|
2025-01-16 12:37:07 -05:00
|
|
|
import TeacherModeQuiz from 'src/components/TeacherModeQuiz/TeacherModeQuiz';
|
2024-09-15 01:26:06 -04:00
|
|
|
import { MemoryRouter } from 'react-router-dom';
|
2025-03-07 16:33:57 -05:00
|
|
|
import { QuestionType } from 'src/Types/QuestionType';
|
2024-09-15 21:41:24 -04:00
|
|
|
|
|
|
|
|
const mockGiftQuestions = parse(
|
2025-03-07 16:33:57 -05:00
|
|
|
`::Sample Question 1:: Sample Question 1 {=Option A ~Option B}
|
|
|
|
|
|
|
|
|
|
::Sample Question 2:: Sample Question 2 {=Option A ~Option B}`);
|
2024-09-15 21:41:24 -04:00
|
|
|
|
2025-03-07 16:33:57 -05:00
|
|
|
const mockQuestions: QuestionType[] = mockGiftQuestions.map((question, index) => {
|
|
|
|
|
if (question.type !== "Category")
|
|
|
|
|
question.id = (index + 1).toString();
|
|
|
|
|
const newMockQuestion = question;
|
|
|
|
|
return {question : newMockQuestion as BaseQuestion};
|
|
|
|
|
});
|
2024-03-29 20:08:34 -04:00
|
|
|
|
2025-03-07 13:39:56 -05:00
|
|
|
describe('TeacherModeQuiz', () => {
|
2025-03-07 16:33:57 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
let mockQuestion = mockQuestions[0].question as MultipleChoiceQuestion;
|
2024-09-15 21:41:24 -04:00
|
|
|
mockQuestion.id = '1';
|
2024-03-29 20:08:34 -04:00
|
|
|
|
|
|
|
|
const mockSubmitAnswer = jest.fn();
|
|
|
|
|
const mockDisconnectWebSocket = jest.fn();
|
|
|
|
|
|
2025-03-07 16:33:57 -05:00
|
|
|
let rerender: (ui: React.ReactElement) => void;
|
|
|
|
|
|
2024-09-15 21:41:24 -04:00
|
|
|
beforeEach(async () => {
|
2025-03-07 16:33:57 -05:00
|
|
|
const utils = render(
|
2024-09-15 01:26:06 -04:00
|
|
|
<MemoryRouter>
|
|
|
|
|
<TeacherModeQuiz
|
|
|
|
|
questionInfos={{ question: mockQuestion }}
|
|
|
|
|
submitAnswer={mockSubmitAnswer}
|
2024-09-15 21:41:24 -04:00
|
|
|
disconnectWebSocket={mockDisconnectWebSocket} />
|
2024-09-15 01:26:06 -04:00
|
|
|
</MemoryRouter>
|
2024-03-29 20:08:34 -04:00
|
|
|
);
|
2025-03-07 16:33:57 -05:00
|
|
|
rerender = utils.rerender;
|
2024-03-29 20:08:34 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('renders the initial question', () => {
|
2025-03-07 12:20:39 -05:00
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
expect(screen.getByText('Question 1')).toBeInTheDocument();
|
2025-03-07 16:33:57 -05:00
|
|
|
expect(screen.getByText('Sample Question 1')).toBeInTheDocument();
|
2024-03-29 20:08:34 -04:00
|
|
|
expect(screen.getByText('Option A')).toBeInTheDocument();
|
|
|
|
|
expect(screen.getByText('Option B')).toBeInTheDocument();
|
2024-09-15 01:26:06 -04:00
|
|
|
expect(screen.getByText('Quitter')).toBeInTheDocument();
|
2024-03-29 20:08:34 -04:00
|
|
|
expect(screen.getByText('Répondre')).toBeInTheDocument();
|
|
|
|
|
});
|
|
|
|
|
|
2024-09-24 19:56:47 -04:00
|
|
|
test('handles answer submission and displays feedback', () => {
|
2024-03-29 20:08:34 -04:00
|
|
|
|
2024-09-15 21:41:24 -04:00
|
|
|
act(() => {
|
|
|
|
|
fireEvent.click(screen.getByText('Option A'));
|
|
|
|
|
});
|
|
|
|
|
act(() => {
|
|
|
|
|
fireEvent.click(screen.getByText('Répondre'));
|
|
|
|
|
});
|
2024-09-26 00:34:30 -04:00
|
|
|
expect(mockSubmitAnswer).toHaveBeenCalledWith('Option A', 1);
|
2024-03-29 20:08:34 -04:00
|
|
|
});
|
|
|
|
|
|
2025-03-07 16:33:57 -05:00
|
|
|
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);
|
|
|
|
|
mockQuestion = mockQuestions[1].question as MultipleChoiceQuestion;
|
|
|
|
|
// Navigate to the next question by re-rendering with new props
|
|
|
|
|
act(() => {
|
|
|
|
|
rerender(
|
|
|
|
|
<MemoryRouter>
|
|
|
|
|
<TeacherModeQuiz
|
|
|
|
|
questionInfos={{ question: mockQuestion }}
|
|
|
|
|
submitAnswer={mockSubmitAnswer}
|
|
|
|
|
disconnectWebSocket={mockDisconnectWebSocket}
|
|
|
|
|
/>
|
|
|
|
|
</MemoryRouter>
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
mockQuestion = mockQuestions[0].question as MultipleChoiceQuestion;
|
|
|
|
|
|
|
|
|
|
act(() => {
|
|
|
|
|
rerender(
|
|
|
|
|
<MemoryRouter>
|
|
|
|
|
<TeacherModeQuiz
|
|
|
|
|
questionInfos={{ question: mockQuestion }}
|
|
|
|
|
submitAnswer={mockSubmitAnswer}
|
|
|
|
|
disconnectWebSocket={mockDisconnectWebSocket}
|
|
|
|
|
/>
|
|
|
|
|
</MemoryRouter>
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Check if the feedback dialog is shown again
|
|
|
|
|
expect(screen.getByText('Rétroaction')).toBeInTheDocument();
|
|
|
|
|
});
|
|
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
test('handles disconnect button click', () => {
|
2024-09-15 21:41:24 -04:00
|
|
|
act(() => {
|
|
|
|
|
fireEvent.click(screen.getByText('Quitter'));
|
|
|
|
|
});
|
2024-03-29 20:08:34 -04:00
|
|
|
expect(mockDisconnectWebSocket).toHaveBeenCalled();
|
|
|
|
|
});
|
2024-09-15 00:34:41 -04:00
|
|
|
});
|