EvalueTonSavoir/client/src/__tests__/pages/Student/StudentModeQuiz/StudentModeQuiz.test.tsx

121 lines
4.2 KiB
TypeScript
Raw Normal View History

import React from 'react';
2025-03-07 16:33:57 -05:00
import { render, screen, fireEvent, act, waitFor } from '@testing-library/react';
2024-03-29 20:08:34 -04:00
import '@testing-library/jest-dom';
import { MemoryRouter } from 'react-router-dom';
2025-01-16 12:37:07 -05:00
import StudentModeQuiz from 'src/components/StudentModeQuiz/StudentModeQuiz';
2025-01-25 02:02:18 -05:00
import { BaseQuestion, parse } from 'gift-pegjs';
import { QuestionType } from 'src/Types/QuestionType';
2024-03-29 20:08:34 -04:00
const mockGiftQuestions = parse(
`::Sample Question 1:: Sample Question 1 {=Option A ~Option B}
::Sample Question 2:: Sample Question 2 {T}`);
2025-01-25 02:02:18 -05:00
const mockQuestions: QuestionType[] = mockGiftQuestions.map((question, index) => {
if (question.type !== "Category")
question.id = (index + 1).toString();
const newMockQuestion = question;
2025-03-07 16:33:57 -05:00
return { question: newMockQuestion as BaseQuestion };
});
2024-03-29 20:08:34 -04:00
const mockSubmitAnswer = jest.fn();
const mockDisconnectWebSocket = jest.fn();
2024-03-29 20:08:34 -04:00
beforeEach(() => {
2025-03-07 16:33:57 -05:00
// Clear local storage before each test
localStorage.clear();
render(
<MemoryRouter>
<StudentModeQuiz
2024-03-29 20:08:34 -04:00
questions={mockQuestions}
submitAnswer={mockSubmitAnswer}
disconnectWebSocket={mockDisconnectWebSocket}
/>
2025-03-07 16:33:57 -05:00
</MemoryRouter>
);
});
2024-03-29 20:08:34 -04:00
describe('StudentModeQuiz', () => {
test('renders the initial question', async () => {
expect(screen.getByText('Sample Question 1')).toBeInTheDocument();
expect(screen.getByText('Option A')).toBeInTheDocument();
expect(screen.getByText('Option B')).toBeInTheDocument();
expect(screen.getByText('Quitter')).toBeInTheDocument();
});
2024-03-29 20:08:34 -04:00
test('handles answer submission text', async () => {
act(() => {
fireEvent.click(screen.getByText('Option A'));
});
act(() => {
fireEvent.click(screen.getByText('Répondre'));
});
expect(mockSubmitAnswer).toHaveBeenCalledWith('Option A', 1);
2025-03-07 16:33:57 -05:00
// await waitFor(() => {
// expect(localStorage.getItem('Answer1')).toBe(JSON.stringify('Option A'));
// });
});
test.skip('handles shows feedback for an already answered question', async () => {
// Answer the first question
act(() => {
fireEvent.click(screen.getByText('Option A'));
});
act(() => {
fireEvent.click(screen.getByText('Répondre'));
});
expect(mockSubmitAnswer).toHaveBeenCalledWith('Option A', 1);
await waitFor(() => {
expect(localStorage.getItem('Answer1')).toBe(JSON.stringify('Option A'));
});
expect(screen.queryByText('Répondre')).not.toBeInTheDocument();
// Simulate feedback display (e.g., a checkmark or feedback message)
// This part depends on how feedback is displayed in your component
// For example, if you display a checkmark, you can check for it:
expect(screen.getByText('✅')).toBeInTheDocument();
// Navigate to the next question
act(() => {
fireEvent.click(screen.getByText('Question suivante'));
});
expect(screen.getByText('Sample Question 2')).toBeInTheDocument();
expect(screen.getByText('Répondre')).toBeInTheDocument();
// Navigate back to the first question
act(() => {
fireEvent.click(screen.getByText('Question précédente'));
});
expect(screen.getByText('Sample Question 1')).toBeInTheDocument();
// Check if feedback is shown again
expect(screen.getByText('✅')).toBeInTheDocument();
2024-03-29 20:08:34 -04:00
});
test('handles quit button click', async () => {
act(() => {
fireEvent.click(screen.getByText('Quitter'));
});
expect(mockDisconnectWebSocket).toHaveBeenCalled();
2024-03-29 20:08:34 -04:00
});
test('navigates to the next question', async () => {
act(() => {
fireEvent.click(screen.getByText('Option A'));
});
act(() => {
fireEvent.click(screen.getByText('Répondre'));
2025-03-07 16:33:57 -05:00
});
act(() => {
fireEvent.click(screen.getByText('Question suivante'));
});
2024-03-29 20:08:34 -04:00
2025-03-07 16:33:57 -05:00
expect(screen.getByText('Sample Question 2')).toBeInTheDocument();
expect(screen.getByText('Répondre')).toBeInTheDocument();
2024-03-29 20:08:34 -04:00
});
2025-03-07 16:33:57 -05:00
});