-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from Wathmiv/testing
unit testing of backend
- Loading branch information
Showing
13 changed files
with
5,249 additions
and
1,630 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
{ | ||
"singleQuote": false, | ||
"semi": false, | ||
"tabWidth": 2, | ||
"printWidth": 80 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/** @type {import('ts-jest').JestConfigWithTsJest} */ | ||
module.exports = { | ||
preset: "ts-jest", | ||
testEnvironment: "node", | ||
}; |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
import { Server } from "socket.io"; | ||
import { StudentController } from "../controllers/student"; | ||
import { StudentService } from "../services/student"; | ||
import { Request, Response } from "express"; | ||
|
||
// Mock StudentService | ||
jest.mock("../services/student", () => ({ | ||
StudentService: { | ||
create: jest.fn(), | ||
findAll: jest.fn(), | ||
edit: jest.fn(), | ||
delete: jest.fn(), | ||
}, | ||
})); | ||
|
||
describe("StudentController", () => { | ||
let io: Server; | ||
let studentController: StudentController; | ||
let req: Partial<Request>; | ||
let res: Partial<Response>; | ||
|
||
beforeEach(() => { | ||
io = new Server(); | ||
studentController = new StudentController(io); | ||
res = { | ||
status: jest.fn().mockReturnThis(), | ||
json: jest.fn(), | ||
}; | ||
}); | ||
|
||
it("should create a new student", async () => { | ||
const mockStudent = { | ||
id: 1, | ||
name: "Test student", | ||
gender: "Male", | ||
address: "123 Test St", | ||
mobile: "1234567890", | ||
dob: new Date("2000-01-01"), | ||
age: 21, | ||
}; | ||
req = { body: { name: "Test student" } }; | ||
|
||
(jest.spyOn(StudentService, "create") as jest.Mock).mockResolvedValue( | ||
mockStudent, | ||
); | ||
await studentController.create(req as Request, res as Response); | ||
expect(res.status).toHaveBeenCalledWith(201); | ||
expect(res.json).toHaveBeenCalledWith(mockStudent); | ||
}); | ||
|
||
it("should return 500 if creating a student fails", async () => { | ||
req = { body: { name: "John Doe" } }; | ||
|
||
(jest.spyOn(StudentService, "create") as jest.Mock).mockRejectedValue( | ||
new Error(), | ||
); | ||
|
||
await studentController.create(req as Request, res as Response); | ||
|
||
expect(res.status).toHaveBeenCalledWith(500); | ||
expect(res.json).toHaveBeenCalledWith({ | ||
message: "An error occurred while creating the student.", | ||
}); | ||
}); | ||
|
||
it("should fetch all students", async () => { | ||
const mockStudents = [ | ||
{ id: 1, name: "Test student 1" }, | ||
{ id: 2, name: "Test student 2" }, | ||
]; | ||
|
||
req = {}; | ||
|
||
(jest.spyOn(StudentService, "findAll") as jest.Mock).mockResolvedValue( | ||
mockStudents, | ||
); | ||
|
||
await studentController.findAll(req as Request, res as Response); | ||
|
||
expect(res.status).toHaveBeenCalledWith(200); | ||
expect(res.json).toHaveBeenCalledWith(mockStudents); | ||
}); | ||
|
||
it("should return 500 if fetching students fails", async () => { | ||
req = {}; | ||
|
||
(jest.spyOn(StudentService, "findAll") as jest.Mock).mockRejectedValue( | ||
new Error(), | ||
); | ||
|
||
await studentController.findAll(req as Request, res as Response); | ||
|
||
expect(res.status).toHaveBeenCalledWith(500); | ||
expect(res.json).toHaveBeenCalledWith({ | ||
message: "An error occurred while fetching the students.", | ||
}); | ||
}); | ||
|
||
it("should edit a student", async () => { | ||
const mockStudent = { id: 1, name: "John Doe" }; | ||
|
||
req = { | ||
params: { id: "1" }, | ||
body: { name: "John Doe" }, | ||
}; | ||
|
||
(jest.spyOn(StudentService, "edit") as jest.Mock).mockResolvedValue( | ||
mockStudent, | ||
); | ||
|
||
await studentController.edit(req as Request, res as Response); | ||
expect(res.status).toHaveBeenCalledWith(200); | ||
expect(res.json).toHaveBeenCalledWith(mockStudent); | ||
}); | ||
|
||
it("should return 500 if editing a student fails", async () => { | ||
req = { | ||
params: { id: "1" }, | ||
body: { name: "John Doe" }, | ||
}; | ||
|
||
(jest.spyOn(StudentService, "edit") as jest.Mock).mockRejectedValue( | ||
new Error(), | ||
); | ||
|
||
await studentController.edit(req as Request, res as Response); | ||
|
||
expect(res.status).toHaveBeenCalledWith(500); | ||
expect(res.json).toHaveBeenCalledWith({ | ||
message: "An error occurred while editing the student.", | ||
}); | ||
}); | ||
|
||
it("should delete a student", async () => { | ||
req = { params: { id: "1" } }; | ||
|
||
(jest.spyOn(StudentService, "delete") as jest.Mock).mockResolvedValue(true); | ||
|
||
await studentController.delete(req as Request, res as Response); | ||
|
||
expect(res.status).toHaveBeenCalledWith(204); | ||
}); | ||
|
||
it("should return 500 if deleting a student fails", async () => { | ||
req = { params: { id: "1" } }; | ||
|
||
(jest.spyOn(StudentService, "delete") as jest.Mock).mockRejectedValue( | ||
new Error(), | ||
); | ||
|
||
await studentController.delete(req as Request, res as Response); | ||
|
||
expect(res.status).toHaveBeenCalledWith(500); | ||
expect(res.json).toHaveBeenCalledWith({ | ||
message: "An error occurred while deleting the student.", | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,66 @@ | ||
import { Request, Response } from "express" | ||
import { StudentService } from "../services/student" | ||
import { Server } from "socket.io" | ||
import { Request, Response } from "express"; | ||
import { StudentService } from "../services/student"; | ||
import { Server } from "socket.io"; | ||
|
||
export class StudentController { | ||
private studentService = new StudentService() | ||
private io: Server | ||
// private studentService = new StudentService() | ||
private io: Server; | ||
|
||
constructor(io: Server) { | ||
this.io = io | ||
this.io = io; | ||
} | ||
|
||
async create(req: Request, res: Response) { | ||
const student = await this.studentService.create(req.body) | ||
this.io.emit("add-student", student) | ||
res.json(student) | ||
try { | ||
const student = await StudentService.create(req.body); | ||
this.io.emit("add-student", student); | ||
res.status(201).json(student); // 201 status code for created | ||
} catch (error) { | ||
console.error(error); | ||
res | ||
.status(500) | ||
.json({ message: "An error occurred while creating the student." }); | ||
} | ||
} | ||
|
||
async findAll(req: Request, res: Response) { | ||
const students = await this.studentService.findAll() | ||
res.json(students) | ||
try { | ||
const students = await StudentService.findAll(); | ||
res.status(200).json(students); | ||
} catch (error) { | ||
console.error(error); | ||
res | ||
.status(500) | ||
.json({ message: "An error occurred while fetching the students." }); | ||
} | ||
} | ||
|
||
async edit(req: Request, res: Response) { | ||
const student = await this.studentService.edit( | ||
Number(req.params.id), | ||
req.body, | ||
) | ||
this.io.emit("edit-student", student) | ||
res.json(student) | ||
try { | ||
const student = await StudentService.edit( | ||
Number(req.params.id), | ||
req.body, | ||
); | ||
this.io.emit("edit-student", student); | ||
res.status(200).json(student); | ||
} catch (error) { | ||
console.error(error); | ||
res | ||
.status(500) | ||
.json({ message: "An error occurred while editing the student." }); | ||
} | ||
} | ||
|
||
async delete(req: Request, res: Response) { | ||
await this.studentService.delete(Number(req.params.id)) | ||
this.io.emit("delete-student") | ||
res.status(204).send() | ||
try { | ||
await StudentService.delete(Number(req.params.id)); | ||
this.io.emit("delete-student"); | ||
res.status(204).send(); | ||
} catch (error) { | ||
console.error(error); | ||
res | ||
.status(500) | ||
.json({ message: "An error occurred while deleting the student." }); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.