Skip to content

Commit d67a61c

Browse files
authored
Merge pull request #361 from ets-cfuhrman-pfe/copilot/refactor-student-type-and-answer-classes
Refactor StudentType and Answer from interfaces to classes, rename to Student Note: my review was only with "read-only permissions" so I had to bypass... Not sure why ? Was co-pilot supposed to merge?
2 parents 438115f + 8add1de commit d67a61c

34 files changed

Lines changed: 229 additions & 249 deletions

client/src/Types/StudentType.tsx

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,35 @@
11
import { AnswerType } from "src/pages/Student/JoinRoom/JoinRoom";
22

3-
export interface Answer {
3+
export class Answer {
44
answer: AnswerType;
55
isCorrect: boolean;
66
idQuestion: number;
7+
8+
constructor(answer: AnswerType, isCorrect: boolean, idQuestion: number) {
9+
this.answer = answer;
10+
this.isCorrect = isCorrect;
11+
this.idQuestion = idQuestion;
12+
}
713
}
814

9-
export interface StudentType {
15+
export class Student {
1016
name: string;
1117
id: string;
12-
room?: string;
18+
room: string;
1319
answers: Answer[];
14-
isConnected?: boolean;
20+
isConnected: boolean;
21+
22+
constructor(
23+
name: string,
24+
id: string,
25+
room: string,
26+
answers: Answer[] = [],
27+
isConnected: boolean = false
28+
) {
29+
this.name = name;
30+
this.id = id;
31+
this.room = room;
32+
this.answers = answers;
33+
this.isConnected = isConnected;
34+
}
1535
}
Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,29 @@
1-
//StudentType.test.tsx
2-
import { StudentType, Answer } from "../../Types/StudentType";
1+
//Student.test.tsx
2+
import { Student, Answer } from "../../Types/StudentType";
33

4-
const user : StudentType = {
5-
name: 'Student',
6-
id: '123',
7-
answers: new Array<Answer>()
8-
}
9-
10-
describe('StudentType', () => {
11-
test('creates a student with name, id and answers', () => {
4+
const user = new Student('Student', '123', 'TestRoom');
125

6+
describe('Student', () => {
7+
test('creates a student with name, id, room, answers and isConnected', () => {
138
expect(user.name).toBe('Student');
149
expect(user.id).toBe('123');
10+
expect(user.room).toBe('TestRoom');
1511
expect(user.answers).toHaveLength(0);
12+
expect(user.isConnected).toBe(false);
13+
});
14+
15+
test('creates student with custom answers', () => {
16+
const answer = new Answer(['test'], true, 1);
17+
const studentWithAnswers = new Student('Student2', '456', 'Room2', [answer]);
18+
19+
expect(studentWithAnswers.answers).toHaveLength(1);
20+
expect(studentWithAnswers.answers[0].answer).toEqual(['test']);
21+
expect(studentWithAnswers.answers[0].isCorrect).toBe(true);
22+
expect(studentWithAnswers.answers[0].idQuestion).toBe(1);
23+
});
24+
25+
test('creates student with custom isConnected', () => {
26+
const connectedStudent = new Student('Student3', '789', 'Room3', [], true);
27+
expect(connectedStudent.isConnected).toBe(true);
1628
});
1729
});

client/src/__tests__/components/GiftTemplate/LiveResults/LiveResults.test.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { render, screen, fireEvent } from '@testing-library/react';
33
import '@testing-library/jest-dom';
44
import LiveResults from 'src/components/LiveResults/LiveResults';
55
import { QuestionType } from 'src/Types/QuestionType';
6-
import { StudentType } from 'src/Types/StudentType';
6+
import { Student, Answer } from 'src/Types/StudentType';
77
import { BaseQuestion, parse } from 'gift-pegjs';
88

99
const mockGiftQuestions = parse(
@@ -18,9 +18,9 @@ const mockQuestions: QuestionType[] = mockGiftQuestions.map((question, index) =>
1818
return {question : newMockQuestion as BaseQuestion};
1919
});
2020

21-
const mockStudents: StudentType[] = [
22-
{ id: "1", name: 'Student 1', answers: [{ idQuestion: 1, answer: ['Answer 1'], isCorrect: true }] },
23-
{ id: "2", name: 'Student 2', answers: [{ idQuestion: 2, answer: ['Answer 2'], isCorrect: false }] },
21+
const mockStudents: Student[] = [
22+
new Student('Student 1', '1', 'TestRoom', [new Answer(['Answer 1'], true, 1)]),
23+
new Student('Student 2', '2', 'TestRoom', [new Answer(['Answer 2'], false, 2)]),
2424
];
2525

2626
const mockShowSelectedQuestion = jest.fn();

client/src/__tests__/components/GiftTemplate/LiveResults/LiveResultsTable/LiveResultsTable.test.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22
import { render, screen, fireEvent } from '@testing-library/react';
33
import '@testing-library/jest-dom';
4-
import { StudentType } from 'src/Types/StudentType';
4+
import { Student, Answer } from 'src/Types/StudentType';
55
import LiveResultsTable from 'src/components/LiveResults/LiveResultsTable/LiveResultsTable';
66
import { QuestionType } from 'src/Types/QuestionType';
77
import { BaseQuestion, parse } from 'gift-pegjs';
@@ -19,9 +19,9 @@ const mockQuestions: QuestionType[] = mockGiftQuestions.map((question, index) =>
1919
});
2020

2121

22-
const mockStudents: StudentType[] = [
23-
{ id: "1", name: 'Student 1', answers: [{ idQuestion: 1, answer: ['Answer 1'], isCorrect: true }] },
24-
{ id: "2", name: 'Student 2', answers: [{ idQuestion: 2, answer: ['Answer 2'], isCorrect: false }] },
22+
const mockStudents: Student[] = [
23+
new Student('Student 1', '1', 'TestRoom', [new Answer(['Answer 1'], true, 1)]),
24+
new Student('Student 2', '2', 'TestRoom', [new Answer(['Answer 2'], false, 2)]),
2525
];
2626

2727
const mockShowSelectedQuestion = jest.fn();

client/src/__tests__/components/GiftTemplate/LiveResults/LiveResultsTable/TableComponents/LiveResultsTableBody.test.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22
import { render, screen } from '@testing-library/react';
33
import '@testing-library/jest-dom';
4-
import { StudentType } from 'src/Types/StudentType';
4+
import { Student, Answer } from 'src/Types/StudentType';
55
import LiveResultsTableBody from 'src/components/LiveResults/LiveResultsTable/TableComponents/LiveResultsTableBody';
66
import { QuestionType } from 'src/Types/QuestionType';
77
import { BaseQuestion, parse } from 'gift-pegjs';
@@ -19,12 +19,12 @@ const mockQuestions: QuestionType[] = mockGiftQuestions.map((question, index) =>
1919
return {question : newMockQuestion as BaseQuestion};
2020
});
2121

22-
const mockStudents: StudentType[] = [
23-
{ id: "1", name: 'Student 1', answers: [{ idQuestion: 1, answer: ['Answer 1'], isCorrect: true }] },
24-
{ id: "2", name: 'Student 2', answers: [{ idQuestion: 2, answer: ['Answer 2'], isCorrect: false }] },
22+
const mockStudents: Student[] = [
23+
new Student('Student 1', '1', 'TestRoom', [new Answer(['Answer 1'], true, 1)]),
24+
new Student('Student 2', '2', 'TestRoom', [new Answer(['Answer 2'], false, 2)]),
2525
];
2626

27-
const mockGetStudentGrade = jest.fn((student: StudentType) => {
27+
const mockGetStudentGrade = jest.fn((student: Student) => {
2828
const correctAnswers = student.answers.filter(answer => answer.isCorrect).length;
2929
return (correctAnswers / mockQuestions.length) * 100;
3030
});

client/src/__tests__/components/GiftTemplate/LiveResults/LiveResultsTable/TableComponents/LiveResultsTableFooter.test.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import React from 'react';
22
import { render, screen } from '@testing-library/react';
33
import '@testing-library/jest-dom';
4-
import { StudentType } from 'src/Types/StudentType';
4+
import { Student, Answer } from 'src/Types/StudentType';
55
import LiveResultsTableFooter from 'src/components/LiveResults/LiveResultsTable/TableComponents/LiveResultTableFooter';
66

77

8-
const mockStudents: StudentType[] = [
9-
{ id: "1", name: 'Student 1', answers: [{ idQuestion: 1, answer: ['Answer 1'], isCorrect: true }] },
10-
{ id: "2", name: 'Student 2', answers: [{ idQuestion: 2, answer: ['Answer 2'], isCorrect: false }] },
8+
const mockStudents: Student[] = [
9+
new Student('Student 1', '1', 'TestRoom', [new Answer(['Answer 1'], true, 1)]),
10+
new Student('Student 2', '2', 'TestRoom', [new Answer(['Answer 2'], false, 2)]),
1111
];
1212

13-
const mockGetStudentGrade = jest.fn((student: StudentType) => {
13+
const mockGetStudentGrade = jest.fn((student: Student) => {
1414
const correctAnswers = student.answers.filter(answer => answer.isCorrect).length;
1515
return (correctAnswers / 2) * 100; // Assuming there are 2 questions
1616
});

client/src/__tests__/components/LiveResults/LiveResults.test.tsx

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { render, screen, fireEvent } from '@testing-library/react';
33
import '@testing-library/jest-dom';
44
import LiveResults from 'src/components/LiveResults/LiveResults';
55
import { QuestionType } from 'src/Types/QuestionType';
6-
import { StudentType } from 'src/Types/StudentType';
6+
import { Student, Answer } from 'src/Types/StudentType';
77
import { Socket } from 'socket.io-client';
88
import { BaseQuestion, parse } from 'gift-pegjs';
99

@@ -37,10 +37,16 @@ const mockQuestions: QuestionType[] = mockGiftQuestions.map((question, index) =>
3737
console.log(`mockQuestions: ${JSON.stringify(mockQuestions)}`);
3838

3939
// each student should have a different score for the tests to pass
40-
const mockStudents: StudentType[] = [
41-
{ id: '1', name: 'Student 1', answers: [] },
42-
{ id: '2', name: 'Student 2', answers: [{ idQuestion: 1, answer: ['Choice 3'], isCorrect: false }, { idQuestion: 2, answer: [true], isCorrect: true}] },
43-
{ id: '3', name: 'Student 3', answers: [{ idQuestion: 1, answer: ['Choice 1', 'Choice 2'], isCorrect: true }, { idQuestion: 2, answer: [true], isCorrect: true}] },
40+
const mockStudents: Student[] = [
41+
new Student('Student 1', '1', 'TestRoom'),
42+
new Student('Student 2', '2', 'TestRoom', [
43+
new Answer(['Choice 3'], false, 1),
44+
new Answer([true], true, 2)
45+
]),
46+
new Student('Student 3', '3', 'TestRoom', [
47+
new Answer(['Choice 1', 'Choice 2'], true, 1),
48+
new Answer([true], true, 2)
49+
]),
4450
];
4551

4652
describe('LiveResults', () => {

client/src/__tests__/components/LiveResults/LiveResultsV2.test.tsx

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { render, screen, fireEvent } from '@testing-library/react';
33
import '@testing-library/jest-dom';
44
import LiveResultsV2 from 'src/components/LiveResults/LiveResultsV2';
55
import { QuestionType } from 'src/Types/QuestionType';
6-
import { StudentType } from 'src/Types/StudentType';
6+
import { Student, Answer } from 'src/Types/StudentType';
77
import { Socket } from 'socket.io-client';
88
import { BaseQuestion, parse } from 'gift-pegjs';
99

@@ -34,33 +34,19 @@ const mockQuestions: QuestionType[] = mockGiftQuestions.map((question, index) =>
3434
return { question: newMockQuestion as BaseQuestion };
3535
});
3636

37-
const mockStudents: StudentType[] = [
38-
{
39-
id: '1',
40-
name: 'Connected Student',
41-
answers: [
42-
{ idQuestion: 1, answer: ['Choice 1'], isCorrect: true },
43-
{ idQuestion: 2, answer: [true], isCorrect: true }
44-
],
45-
isConnected: true
46-
},
47-
{
48-
id: '2',
49-
name: 'Disconnected Student',
50-
answers: [
51-
{ idQuestion: 1, answer: ['Choice 2'], isCorrect: false },
52-
{ idQuestion: 2, answer: [false], isCorrect: false }
53-
],
54-
isConnected: false
55-
},
56-
{
57-
id: '3',
58-
name: 'Another Connected Student',
59-
answers: [
60-
{ idQuestion: 1, answer: ['Choice 1'], isCorrect: true },
61-
{ idQuestion: 2, answer: [true], isCorrect: true }
62-
]
63-
}
37+
const mockStudents: Student[] = [
38+
new Student('Connected Student', '1', 'TestRoom', [
39+
new Answer(['Choice 1'], true, 1),
40+
new Answer([true], true, 2)
41+
], true),
42+
new Student('Disconnected Student', '2', 'TestRoom', [
43+
new Answer(['Choice 2'], false, 1),
44+
new Answer([false], false, 2)
45+
], false),
46+
new Student('Another Connected Student', '3', 'TestRoom', [
47+
new Answer(['Choice 1'], true, 1),
48+
new Answer([true], true, 2)
49+
], true)
6450
];
6551

6652
const mockShowSelectedQuestion = jest.fn();
@@ -428,4 +414,4 @@ describe('LiveResultsV2', () => {
428414
}
429415
});
430416
});
431-
});
417+
});

client/src/__tests__/components/QuestionsDisplay/QuestionDisplayV2.test.tsx

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { render, screen, fireEvent, within } from '@testing-library/react';
44
import '@testing-library/jest-dom';
55
import QuestionDisplayV2 from 'src/components/QuestionsDisplay/QuestionDisplayV2';
66
import { parse, Question } from 'gift-pegjs';
7-
import { StudentType } from 'src/Types/StudentType';
7+
import { Student, Answer } from 'src/Types/StudentType';
88

99
describe('QuestionDisplayV2 Component', () => {
1010
const mockHandleSubmitAnswer = jest.fn();
@@ -25,30 +25,18 @@ describe('QuestionDisplayV2 Component', () => {
2525
(sampleTrueFalseQuestion as any).id = 1;
2626
(sampleMultipleChoiceQuestion as any).id = 2;
2727

28-
const mockStudents: StudentType[] = [
29-
{
30-
id: 'student1',
31-
name: 'John Doe',
32-
answers: [{ idQuestion: 1, answer: [true], isCorrect: true }]
33-
},
34-
{
35-
id: 'student2',
36-
name: 'Jane Smith',
37-
answers: [{ idQuestion: 1, answer: [false], isCorrect: false }]
38-
},
39-
{
40-
id: 'student3',
41-
name: 'Bob Johnson',
42-
answers: [{ idQuestion: 1, answer: [true], isCorrect: true }]
43-
}
28+
const mockStudents: Student[] = [
29+
new Student('John Doe', 'student1', 'TestRoom', [new Answer([true], true, 1)]),
30+
new Student('Jane Smith', 'student2', 'TestRoom', [new Answer([false], false, 1)]),
31+
new Student('Bob Johnson', 'student3', 'TestRoom', [new Answer([true], true, 1)]),
4432
];
4533

4634
const sampleProps = {
4735
handleOnSubmitAnswer: mockHandleSubmitAnswer,
4836
showAnswer: false,
4937
answer: undefined as any,
5038
disabled: false,
51-
students: [] as StudentType[],
39+
students: [] as Student[],
5240
showStatistics: false
5341
};
5442

@@ -155,17 +143,9 @@ describe('QuestionDisplayV2 Component', () => {
155143
});
156144

157145
it('shows statistics for multiple choice when enabled', () => {
158-
const mcStudents: StudentType[] = [
159-
{
160-
id: 'student1',
161-
name: 'John Doe',
162-
answers: [{ idQuestion: 2, answer: ['Choice 1'], isCorrect: true }]
163-
},
164-
{
165-
id: 'student2',
166-
name: 'Jane Smith',
167-
answers: [{ idQuestion: 2, answer: ['Choice 2'], isCorrect: false }]
168-
}
146+
const mcStudents: Student[] = [
147+
new Student('John Doe', 'student1', 'TestRoom', [new Answer(['Choice 1'], true, 2)]),
148+
new Student('Jane Smith', 'student2', 'TestRoom', [new Answer(['Choice 2'], false, 2)]),
169149
];
170150

171151
renderComponent(sampleMultipleChoiceQuestion, {

0 commit comments

Comments
 (0)