-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathobjectIteration.test.js
73 lines (65 loc) · 2.42 KB
/
objectIteration.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const {
getStudentName,
getCourse,
addCourseToStudent,
countCourses,
removeCourseFromStudent,
findStudentById,
} = require("./objectIteration");
describe("Student Data Functions", () => {
let students;
beforeEach(() => {
students = [
{ id: 1, name: "Alice", courses: ["Math", "Science", "History"] },
{ id: 2, name: "Bob", courses: ["History", "English", "Math", "Art"] },
{ id: 3, name: "Charlie", courses: ["Science", "English", "Music"] },
{ id: 4, name: "David", courses: ["Math", "History", "Art", "PE"] },
{ id: 5, name: "Eva", courses: ["Science", "Math", "Music"] },
{ id: 6, name: "Frank", courses: ["English", "Art"] },
{
id: 7,
name: "Grace",
courses: ["Math", "Science", "English", "Music"],
},
{ id: 8, name: "Helen", courses: ["History", "Art", "PE"] },
{ id: 9, name: "Ivy", courses: ["Science", "English", "Art"] },
{ id: 10, name: "Jack", courses: ["Math", "History", "Music"] },
];
});
describe("getStudentName", () => {
it("should return the name of the student", () => {
expect(getStudentName(students[0])).toBe("Alice");
});
});
describe("getCourse", () => {
it("should return the course at the specified index", () => {
expect(getCourse(students[4], 2)).toBe("Music");
});
});
describe("addCourseToStudent", () => {
it("should add a course to the student and return the updated student", () => {
const student = students[7];
const length = student.courses.length;
const updatedStudent = addCourseToStudent(student, "Physics");
expect(updatedStudent.courses.includes("Physics")).toBe(true);
expect(updatedStudent.courses.length).toBe(length + 1);
});
});
describe("countCourses", () => {
it("should return the number of courses a student is enrolled in", () => {
expect(countCourses(students[1])).toBe(4);
});
});
describe("removeCourseFromStudent", () => {
it("should remove a course from a student and return the updated student", () => {
const student = students[6];
const updatedStudent = removeCourseFromStudent(student, "Science");
expect(updatedStudent.courses.includes("Science")).toBe(false);
});
});
describe("findStudentById", () => {
it("should return the student object with the matching id", () => {
expect(findStudentById(students, 10)).toEqual(students[9]);
});
});
});