From 3aa51900d71f56d6fc25394ea9db9beba4a688c5 Mon Sep 17 00:00:00 2001
From: JubaAzul <118773284+JubaAzul@users.noreply.github.com>
Date: Fri, 7 Feb 2025 13:33:38 -0500
Subject: [PATCH] =?UTF-8?q?Cr=C3=A9ation=20de=20classes=20de=20tests.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../LiveResults/LiveResults.test.tsx | 95 +++++++++++++++
.../LiveResults/LiveResultsTable.test.tsx | 110 ++++++++++++++++++
2 files changed, 205 insertions(+)
create mode 100644 client/src/__tests__/components/GiftTemplate/LiveResults/LiveResults.test.tsx
create mode 100644 client/src/__tests__/components/GiftTemplate/LiveResults/LiveResultsTable.test.tsx
diff --git a/client/src/__tests__/components/GiftTemplate/LiveResults/LiveResults.test.tsx b/client/src/__tests__/components/GiftTemplate/LiveResults/LiveResults.test.tsx
new file mode 100644
index 0000000..3431b73
--- /dev/null
+++ b/client/src/__tests__/components/GiftTemplate/LiveResults/LiveResults.test.tsx
@@ -0,0 +1,95 @@
+import React from 'react';
+import { render, screen, fireEvent } from '@testing-library/react';
+import '@testing-library/jest-dom';
+import LiveResults from 'src/components/LiveResults/LiveResults';
+import { QuestionType } from 'src/Types/QuestionType';
+import { StudentType } from 'src/Types/StudentType';
+import { BaseQuestion, parse } from 'gift-pegjs';
+
+const mockGiftQuestions = parse(
+ `::Sample Question 1:: Sample Question 1 {=Answer 1 ~Answer 2}
+
+ ::Sample Question 2:: Sample Question 2 {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};
+});
+
+const mockStudents: StudentType[] = [
+ { id: "1", name: 'Student 1', answers: [{ idQuestion: 1, answer: 'Answer 1', isCorrect: true }] },
+ { id: "2", name: 'Student 2', answers: [{ idQuestion: 2, answer: 'Answer 2', isCorrect: false }] },
+];
+
+const mockShowSelectedQuestion = jest.fn();
+
+describe('LiveResults', () => {
+ test('renders LiveResults component', () => {
+ render(
+
+ );
+
+ expect(screen.getByText('Résultats du quiz')).toBeInTheDocument();
+ });
+
+ test('toggles show usernames switch', () => {
+ render(
+
+ );
+
+ const switchElement = screen.getByLabelText('Afficher les noms');
+ expect(switchElement).toBeInTheDocument();
+
+ fireEvent.click(switchElement);
+ expect(switchElement).toBeChecked();
+ });
+
+ test('toggles show correct answers switch', () => {
+ render(
+
+ );
+
+ const switchElement = screen.getByLabelText('Afficher les réponses');
+ expect(switchElement).toBeInTheDocument();
+
+ fireEvent.click(switchElement);
+ expect(switchElement).toBeChecked();
+ });
+
+ test('calls showSelectedQuestion when a table cell is clicked', () => {
+ render(
+
+ );
+
+ const tableCell = screen.getByText('Q1');
+ fireEvent.click(tableCell);
+
+ expect(mockShowSelectedQuestion).toHaveBeenCalled();
+ });
+});
\ No newline at end of file
diff --git a/client/src/__tests__/components/GiftTemplate/LiveResults/LiveResultsTable.test.tsx b/client/src/__tests__/components/GiftTemplate/LiveResults/LiveResultsTable.test.tsx
new file mode 100644
index 0000000..7f4c8a0
--- /dev/null
+++ b/client/src/__tests__/components/GiftTemplate/LiveResults/LiveResultsTable.test.tsx
@@ -0,0 +1,110 @@
+import React from 'react';
+import { render, screen, fireEvent } from '@testing-library/react';
+import '@testing-library/jest-dom';
+import { StudentType } from 'src/Types/StudentType';
+import LiveResultsTable from 'src/components/LiveResults/LiveResultsTable';
+import { QuestionType } from 'src/Types/QuestionType';
+import { BaseQuestion, parse } from 'gift-pegjs';
+
+const mockGiftQuestions = parse(
+ `::Sample Question 1:: Sample Question 1 {=Answer 1 ~Answer 2}
+
+ ::Sample Question 2:: Sample Question 2 {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};
+});
+
+
+const mockStudents: StudentType[] = [
+ { id: "1", name: 'Student 1', answers: [{ idQuestion: 1, answer: 'Answer 1', isCorrect: true }] },
+ { id: "2", name: 'Student 2', answers: [{ idQuestion: 2, answer: 'Answer 2', isCorrect: false }] },
+];
+
+const mockShowSelectedQuestion = jest.fn();
+
+describe('LiveResultsTable', () => {
+ test('renders LiveResultsTable component', () => {
+ render(
+
+ );
+
+ expect(screen.getByText('Student 1')).toBeInTheDocument();
+ expect(screen.getByText('Student 2')).toBeInTheDocument();
+ });
+
+ test('displays correct and incorrect answers', () => {
+ render(
+
+ );
+
+ expect(screen.getByText('Answer 1')).toBeInTheDocument();
+ expect(screen.getByText('Answer 2')).toBeInTheDocument();
+ });
+
+ test('calls showSelectedQuestion when a table cell is clicked', () => {
+ render(
+
+ );
+
+ const tableCell = screen.getByText('Q1');
+ fireEvent.click(tableCell);
+
+ expect(mockShowSelectedQuestion).toHaveBeenCalled();
+ });
+
+ test('calculates and displays student grades', () => {
+ render(
+
+ );
+
+ //50% because only one of the two questions have been answered (getALLByText, because there are a value 50% for the %reussite de la question
+ // and a second one for the student grade)
+ const gradeElements = screen.getAllByText('50 %');
+ expect(gradeElements.length).toBe(2);
+
+ const gradeElements2 = screen.getAllByText('0 %');
+ expect(gradeElements2.length).toBe(2); });
+
+ test('calculates and displays class average', () => {
+ render(
+
+ );
+
+ //1 good answer out of 4 possible good answers (the second question has not been answered)
+ expect(screen.getByText('25 %')).toBeInTheDocument();
+ });
+});
\ No newline at end of file