Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated module names #17

Merged
merged 1 commit into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
218 changes: 218 additions & 0 deletions backend/src/__test__/controller.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
import { Request, Response } from "express";
import { StudentController } from "../controller/studentController";
import { StudentService } from "../services/studentService";
import { getSocketInstance } from "../services/socketService";
import { Student } from "../entity/student";

jest.mock("../services/studentService");
jest.mock("../services/socketService");
jest.mock("../entity/Student");

const mockSave = jest.fn();
const mockFindOne = jest.fn();
jest.mock("../entity/Student", () => ({
PrimaryGeneratedColumn: jest.fn(),
Entity: jest.fn(),
Column: jest.fn(),
PrimaryColumn: jest.fn(),
getConnection: jest.fn().mockReturnValue({
getRepository: jest.fn().mockReturnValue({
save: mockSave,
findOne: mockFindOne,
}),
}),
}));

describe("StudentController", () => {
const mockRequest = {};
const mockResponse = {
status: jest.fn().mockReturnThis(),
send: jest.fn(),
};
beforeEach(() => {
jest.clearAllMocks();
});

it("should send all students successfully", async () => {
const expectedStudents = [
{ id: 1, name: "John Doe" },
{ id: 2, name: "Jane Doe" },
];
(StudentService.prototype.getAllStudents as jest.Mock).mockResolvedValue(
expectedStudents
);

const studentController = new StudentController();

await studentController.all(mockRequest as Request, mockResponse as any);

expect(mockResponse.status).toHaveBeenCalledWith(200);
expect(mockResponse.send).toHaveBeenCalledWith(expectedStudents);
});

it("should handle an error and send a 500 response", async () => {
const expectedError = new Error("An error occurred");
(StudentService.prototype.getAllStudents as jest.Mock).mockRejectedValue(
expectedError
);

const studentController = new StudentController();

await studentController.all(mockRequest as Request, mockResponse as any);

expect(mockResponse.status).toHaveBeenCalledWith(500);
expect(mockResponse.send).toHaveBeenCalledWith(expectedError);
});
});

//add one student test

describe("StudentController", () => {
const mockRequest = {
body: {
id: 1,
name: "John Doe",
gender: "Male",
address: "123 Main St",
mobile: "123-456-7890",
birthday: "2000-01-01",
age: 21,
},
params: {
userId: "user123",
},
};
const mockResponse = {
status: jest.fn().mockReturnThis(),
send: jest.fn(),
};

beforeEach(() => {
jest.clearAllMocks();
});

it("should add one student successfully", async () => {
const expectedStudent = {};
(StudentService.prototype.createStudent as jest.Mock).mockResolvedValue(
expectedStudent
);

const studentController = new StudentController();

await studentController.add(mockRequest as any, mockResponse as any);

expect(mockResponse.status).toHaveBeenCalledWith(200);
expect(mockResponse.send).toHaveBeenCalledWith(expectedStudent);
});

it("should handle an error and send a 500 response", async () => {
const expectedError = new Error("An error occurred");
(StudentService.prototype.createStudent as jest.Mock).mockRejectedValue(
expectedError
);

const studentController = new StudentController();

await studentController.add(mockRequest as any, mockResponse as any);

expect(mockResponse.status).toHaveBeenCalledWith(500);
expect(mockResponse.send).toHaveBeenCalledWith(expectedError);
});
});

//update one student test

describe("StudentController", () => {
const mockRequest = {
body: {
id: 1,
name: "John Doe",
},
params: {
userId: "user123",
},
};
const mockResponse = {
status: jest.fn().mockReturnThis(),
send: jest.fn(),
};

beforeEach(() => {
jest.clearAllMocks();
});

it("should update one student successfully", async () => {
const expectedStudent = {};
(StudentService.prototype.updateStudent as jest.Mock).mockResolvedValue(
expectedStudent
);

const studentController = new StudentController();

await studentController.update(mockRequest as any, mockResponse as any);

expect(mockResponse.status).toHaveBeenCalledWith(200);
expect(mockResponse.send).toHaveBeenCalledWith(expectedStudent);
});

it("should handle an error and send a 500 response", async () => {
const expectedError = new Error("An error occurred");
(StudentService.prototype.updateStudent as jest.Mock).mockRejectedValue(
expectedError
);

const studentController = new StudentController();

await studentController.update(mockRequest as any, mockResponse as any);

expect(mockResponse.status).toHaveBeenCalledWith(500);
expect(mockResponse.send).toHaveBeenCalledWith(expectedError);
});
});

//delete one student test

describe("StudentController", () => {
const mockRequest = {
params: {
id: 1,
userId: "user123",
},
};
const mockResponse = {
status: jest.fn().mockReturnThis(),
send: jest.fn(),
};

beforeEach(() => {
jest.clearAllMocks();
});

it("should delete one student successfully", async () => {
const expectedStudent = {};
(StudentService.prototype.removeStudent as jest.Mock).mockResolvedValue(
expectedStudent
);

const studentController = new StudentController();

await studentController.remove(mockRequest as any, mockResponse as any);

expect(mockResponse.status).toHaveBeenCalledWith(200);
expect(mockResponse.send).toHaveBeenCalledWith(expectedStudent);
});

it("should handle an error and send a 500 response", async () => {
const expectedError = new Error("An error occurred");
(StudentService.prototype.removeStudent as jest.Mock).mockRejectedValue(
expectedError
);

const studentController = new StudentController();

await studentController.remove(mockRequest as any, mockResponse as any);

expect(mockResponse.status).toHaveBeenCalledWith(500);
expect(mockResponse.send).toHaveBeenCalledWith(expectedError);
});
});
127 changes: 127 additions & 0 deletions backend/src/__test__/service.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
// StudentService.test.ts

import { StudentService } from "../services/studentService";
import { AppDataSource } from "../config/data-source";
import { Student } from "../entity/student";

jest.mock("../config/data-source");

// Mock the AppDataSource getRepository method
const mockGetRepository = jest.fn();

// Mock the repository methods
const mockFind = jest.fn();
const mockFindOne = jest.fn();
const mockCreate = jest.fn();
const mockSave = jest.fn();
const mockRemove = jest.fn();
const mockUpdate = jest.fn();

beforeEach(() => {
// Reset mocks and create fresh instances for each test
jest.clearAllMocks();

// Mock the AppDataSource getRepository method to return the mock repository methods
(AppDataSource.getRepository as jest.Mock).mockImplementation(() => ({
find: mockFind,
findOne: mockFindOne,
create: mockCreate,
save: mockSave,
remove: mockRemove,
update: mockUpdate,
}));
});

describe("StudentService", () => {
let studentService: StudentService;

beforeEach(() => {
studentService = new StudentService();
});

it("should get all students successfully", async () => {
// Arrange
const expectedStudents = [
{ id: 1, name: "John Doe" },
{ id: 2, name: "Jane Doe" },
];
mockFind.mockResolvedValue(expectedStudents);

// Act
const result = await studentService.getAllStudents();

// Assert
expect(result).toEqual(expectedStudents);
});

it("should get a student by id successfully", async () => {
// Arrange
const expectedStudent = { id: 1, name: "John Doe" };
mockFindOne.mockResolvedValue(expectedStudent);

// Act
const result = await studentService.getStudentById(1);

// Assert
expect(result).toEqual(expectedStudent);
});

it("should create a new student successfully", async () => {
const student = new Student();
const expectedStudent = {
id: 1,
name: "John Doe",
gender: "Male",
address: "123 Main St",
mobile: "123-456-7890",
birthday: new Date(),
age: 21,
};

mockCreate.mockReturnValue(expectedStudent);
mockSave.mockResolvedValue(expectedStudent);

// Act
const result = await studentService.createStudent(
1,
"John Doe",
"Male",
"123 Main St",
"123-456-7890",
new Date(),
21
);

// Assert
expect(result).toEqual(expectedStudent);
expect(mockSave).toHaveBeenCalledWith(expectedStudent);
});

it("should remove a student successfully", async () => {
// Arrange
const expectedMessage = "Student has been removed";
mockFindOne.mockResolvedValue({ id: 1, name: "John Doe" });

// Act
const result = await studentService.removeStudent(1);

// Assert
expect(result).toEqual(expectedMessage);
expect(mockRemove).toHaveBeenCalledWith({ id: 1, name: "John Doe" });
});

it("should update a student successfully", async () => {
// Arrange
const expectedUpdatedStudent = { affected: 1 };
mockUpdate.mockResolvedValue({ affected: 1 });

// Act
const result = await studentService.updateStudent(1, {
name: "Updated John Doe",
});

// Assert
expect(result).toEqual(expectedUpdatedStudent);
expect(mockUpdate).toHaveBeenCalledWith(1, { name: "Updated John Doe" });
});
});
18 changes: 18 additions & 0 deletions backend/src/config/data-source.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import "reflect-metadata";
import { DataSource } from "typeorm";
import { Student } from "../entity/student";
import * as dotenv from "dotenv";
dotenv.config();

export const AppDataSource = new DataSource({
url: process.env.PG_DATABASE_URL,
ssl: {
rejectUnauthorized: false,
},
type: "postgres",
synchronize: true,
logging: false,
entities: [Student],
migrations: [],
subscribers: [],
});
Loading