Skip to content

Commit

Permalink
chore: add student service tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tyboro2002 committed Mar 30, 2024
1 parent 833a7d5 commit 22a4782
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 4 deletions.
69 changes: 65 additions & 4 deletions frontend/src/test/unit/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ const projects = [
const courses = [
{
"id": "1",
"teachers": "https://localhost/api/courses/1/teachers/",
"assistants": "https://localhost/api/courses/1/assistants/",
"students": "https://localhost/api/courses/1/students/",
"projects": "https://localhost/api/courses/1/projects/",
"teachers": [],
"assistants": [],
"students": [],
"projects": [],
"parent_course": null,
"name": "Math",
"academic_startyear": 2023,
Expand Down Expand Up @@ -135,12 +135,68 @@ const faculties = [
{name: "voetbal"}
]

const students = [
{
id: "1",
last_login: null,
username: "jdoe",
is_staff: false,
email: "[email protected]",
first_name: "John",
last_name: "Doe",
last_enrolled: 2023,
create_time: new Date("July 21, 2024 01:15:00"),
student_id: null
},
{
id: "2",
last_login: null,
username: "bverhae",
is_staff: false,
email: "[email protected]",
first_name: "Bartje",
last_name: "Verhaege",
last_enrolled: 2023,
create_time: new Date("July 21, 2024 01:15:00"),
student_id: null
},
{
id: "000201247011",
last_login: new Date("July 30, 2024 01:15:00"),
username: "tverslyp",
is_staff: true,
email: "[email protected]",
first_name: "Tybo",
last_name: "Verslype",
last_enrolled: 2023,
create_time: new Date("July 21, 2024 01:15:00"),
student_id: "02012470"
},
{
id: "3",
last_login: null,
username: "somtin",
is_staff: false,
email: "[email protected]",
first_name: "somtin",
last_name: "somtin",
last_enrolled: 2023,
create_time: new Date("July 21, 2024 01:15:00"),
student_id: null
}
]

export const restHandlers = [
http.get(baseUrl + endpoints.groups.retrieve.replace('{id}', ':id'),
({ params }) => {
return HttpResponse.json(groups.find(x => x.id == params.id))
}
),
http.get(baseUrl + endpoints.students.retrieve.replace('{id}', ':id'),
({ params }) => {
return HttpResponse.json(students.find(x => x.id == params.id))
}
),
http.get(baseUrl + endpoints.projects.retrieve.replace('{id}', ':id'),
({ params }) => {
return HttpResponse.json(projects.find(x => x.id == params.id))
Expand Down Expand Up @@ -175,6 +231,11 @@ export const restHandlers = [
({}) => {
return HttpResponse.json(courses)
}
),
http.get(baseUrl + endpoints.students.index,
({}) => {
return HttpResponse.json(students)
}
)

/*
Expand Down
100 changes: 100 additions & 0 deletions frontend/src/test/unit/student_service.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import {describe, it, expect, beforeEach} from 'vitest'
import { useStudents } from '@/composables/services/students.service.ts'

const {
students,
student,

response,

getStudentByID,
getStudents,
getStudentsByCourse,
getStudentsByGroup,

createStudent,
deleteStudent,

studentJoinCourse,
studentLeaveCourse,
studentJoinGroup,
studentLeaveGroup
} = useStudents();

describe("students", (): void => {
it("gets student data by id", async () => {
await getStudentByID("1")
expect(student.value).not.toBeNull()
expect(student.value?.username).toBe("jdoe")
expect(student.value?.is_staff).toBe(false)
expect(student.value?.email).toBe("[email protected]")
expect(student.value?.first_name).toBe("John")
expect(student.value?.last_name).toBe("Doe")
expect(student.value?.last_enrolled).toBe(2023)
expect(student.value?.student_id).toBeNull()
expect(student.value?.last_login).toBeNull()
expect(student.value?.create_time).toEqual(new Date("July 21, 2024 01:15:00"))
expect(student.value?.courses).toEqual([])
expect(student.value?.groups).toEqual([])
expect(student.value?.faculties).toEqual([])
})

it("gets students data", async () => {
await getStudents()
expect(students).not.toBeNull()
expect(Array.isArray(students.value)).toBe(true)
expect(students.value?.length).toBe(4);

expect(students.value?.[0]?.username).toBe("jdoe")
expect(students.value?.[0]?.is_staff).toBe(false)
expect(students.value?.[0]?.email).toBe("[email protected]")
expect(students.value?.[0]?.first_name).toBe("John")
expect(students.value?.[0]?.last_name).toBe("Doe")
expect(students.value?.[0]?.last_enrolled).toBe(2023)
expect(students.value?.[0]?.student_id).toBeNull()
expect(students.value?.[0]?.last_login).toBeNull()
expect(students.value?.[0]?.create_time).toEqual(new Date("July 21, 2024 01:15:00"))
expect(students.value?.[0]?.courses).toEqual([])
expect(students.value?.[0]?.groups).toEqual([])
expect(students.value?.[0]?.faculties).toEqual([])

expect(students.value?.[1]?.username).toBe("bverhae")
expect(students.value?.[1]?.is_staff).toBe(false)
expect(students.value?.[1]?.email).toBe("[email protected]")
expect(students.value?.[1]?.first_name).toBe("Bartje")
expect(students.value?.[1]?.last_name).toBe("Verhaege")
expect(students.value?.[1]?.last_enrolled).toBe(2023)
expect(students.value?.[1]?.student_id).toBeNull()
expect(students.value?.[1]?.last_login).toBeNull()
expect(students.value?.[1]?.create_time).toEqual(new Date("July 21, 2024 01:15:00"))
expect(students.value?.[1]?.courses).toEqual([])
expect(students.value?.[1]?.groups).toEqual([])
expect(students.value?.[1]?.faculties).toEqual([])

expect(students.value?.[2]?.username).toBe("tverslyp")
expect(students.value?.[2]?.is_staff).toBe(true)
expect(students.value?.[2]?.email).toBe("[email protected]")
expect(students.value?.[2]?.first_name).toBe("Tybo")
expect(students.value?.[2]?.last_name).toBe("Verslype")
expect(students.value?.[2]?.last_enrolled).toBe(2023)
expect(students.value?.[2]?.student_id).toBe("02012470")
expect(students.value?.[2]?.last_login).toEqual(new Date("July 30, 2024 01:15:00"))
expect(students.value?.[2]?.create_time).toEqual(new Date("July 21, 2024 01:15:00"))
expect(students.value?.[2]?.courses).toEqual([])
expect(students.value?.[2]?.groups).toEqual([])
expect(students.value?.[2]?.faculties).toEqual([])

expect(students.value?.[3]?.username).toBe("somtin")
expect(students.value?.[3]?.is_staff).toBe(false)
expect(students.value?.[3]?.email).toBe("[email protected]")
expect(students.value?.[3]?.first_name).toBe("somtin")
expect(students.value?.[3]?.last_name).toBe("somtin")
expect(students.value?.[3]?.last_enrolled).toBe(2023)
expect(students.value?.[3]?.student_id).toBeNull()
expect(students.value?.[3]?.last_login).toBeNull()
expect(students.value?.[3]?.create_time).toEqual(new Date("July 21, 2024 01:15:00"))
expect(students.value?.[3]?.courses).toEqual([])
expect(students.value?.[3]?.groups).toEqual([])
expect(students.value?.[3]?.faculties).toEqual([])
})
})

0 comments on commit 22a4782

Please sign in to comment.