diff --git a/tests/Instructor.test.js b/tests/Instructor.test.js index db83671..0e35ac5 100644 --- a/tests/Instructor.test.js +++ b/tests/Instructor.test.js @@ -1,106 +1,94 @@ -// tests/DepartmentService.test.js -import { describe, it, expect, beforeEach, vi } from 'vitest'; -import { options } from '../src/api/Routes/InstructorRoutes'; -import { readSync } from 'fs'; -const path = require('path'); -const base = path.resolve(__dirname, '../'); -const sequelize = require(path.join(base, 'src', 'config', 'db.js')); -const { DataTypes } = require('sequelize'); -const InstructorModel = require('../src/api/Models/instructor')(sequelize, DataTypes); -const InstructorService = require('../src/api/Services/InstructorServices'); -const newInstructor = undefined - -describe("Instructor Create", ()=>{ - const mockInstructorData = { - first_name: 'Kirukkan', - last_name: 'Kirruki', - email: 'Kirrukki@gmail.com', - phone_number: '017-7454678', - hire_date: Date.now(), - department: null - }; - - beforeEach(() => { - // reset all mocks before each test - vi.restoreAllMocks(); - }); - - it('should create a successfully', async () => { - // Mock Sequelize create method - // vi.spyOn(DepartmentModel, 'create').mockResolvedValue(null); - - const result = await InstructorService.CreateInstructor(mockInstructorData); - - - expect(result).toEqual({ - success: true, - message: "Instructor created successfully", - data: result.data, - }); - // expect(DepartmentModel.create).toHaveBeenCalledWith(mockDepartmentData); - }); -}) - -describe('test update method of', ()=>{ - const mockInstructorData = { - first_name: 'Kiruskkan', - last_name: 'Kirruki', - email: 'Kirrukki@gmais.com', - phone_number: '017-7454678', - hire_date: Date.now(), - department: null - }; - - beforeEach(() => { - // reset all mocks before each test - vi.restoreAllMocks(); - }); - - it('should throw error when Instructor not found with the given id', async () => { - // Mock Sequelize findByPk to return null - vi.spyOn(InstructorModel, 'findByPk').mockResolvedValue(null); - - // Call service and expect rejection - await expect( - InstructorService.UpdateInstructor(999, { name: 'Mock Instructor' }) - ).rejects.toThrow("Instructor not found with the given id"); - - // Optional: also check the custom status property - await expect( - InstructorService.UpdateInstructor(999, { name: 'Mock Instructor' }) - ).rejects.toMatchObject({ status: 404 }); - }); - - - it("should succesfull update the Instructor Data"), async() =>{ - const newInstructor = await InstructorModel.create({ - first_name: 'Maths Teacher', - last_name: 'Kayalvizhi', - email: 'Kayal@gmais.com', - phone_number: '017-7454678', - hire_date: Date.now(), - department: null +// tests/InstructorService.test.js +import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'; +import path from 'path'; +import sequelize from '../src/Config/db.js'; +import { DataTypes } from 'sequelize'; +import InstructorModelFactory from '../src/api/Models/instructor.js'; +import * as InstructorService from '../src/api/Services/InstructorServices.js'; + +const InstructorModel = InstructorModelFactory(sequelize, DataTypes); +let newInstructor; + +describe("Instructor Create", () => { + const mockInstructorData = { + first_name: 'Kirukkan', + last_name: 'Kirruki', + email: 'Kirrukki@gmail.com', + phone_number: '017-7454678', + hire_date: Date.now(), + department: null + }; + + beforeEach(() => { + vi.restoreAllMocks(); + }); + + it('should create successfully', async () => { + const result = await InstructorService.CreateInstructor(mockInstructorData); + + expect(result).toEqual({ + success: true, + message: "Instructor created successfully", + data: result.data, }); + }); +}); + +describe("Instructor Update", () => { + const mockInstructorData = { + first_name: 'Kiruskkan', + last_name: 'Kirruki', + email: 'Kirrukki@gmais.com', + phone_number: '017-7454678', + hire_date: Date.now(), + department: null + }; + + beforeEach(() => { + vi.restoreAllMocks(); + }); + + it('should throw error when instructor not found with the given id', async () => { + vi.spyOn(InstructorModel, 'findByPk').mockResolvedValue(null); + + await expect( + InstructorService.UpdateInstructor(999, { name: 'Mock Instructor' }) + ).rejects.toThrow("Instructor not found with the given id"); + + await expect( + InstructorService.UpdateInstructor(999, { name: 'Mock Instructor' }) + ).rejects.toMatchObject({ status: 404 }); + }); + + it("should successfully update the instructor data", async () => { + newInstructor = await InstructorModel.create({ + first_name: 'Maths Teacher', + last_name: 'Kayalvizhi', + email: 'Kayal@gmais.com', + phone_number: '017-7454678', + hire_date: Date.now(), + department: null + }); + + const mockInstructorId = newInstructor.id; + + const result = await InstructorService.UpdateInstructor(mockInstructorId, mockInstructorData); - const mockInstructorId = newInstructor.id; // 👈 real generated id - - const result = await InstructorService.UpdateInstructor(mockInstructorData); expect(result).toEqual({ - success:true, + success: true, message: "Instructor updated successfully", data: expect.objectContaining(mockInstructorData) }); - console.log(result) - // Fetch from DB again to ensure it's really updated - const updatedInstructor = await Instructor.findByPk(mockInstructorId); + const updatedInstructor = await InstructorModel.findByPk(mockInstructorId); expect(updatedInstructor).not.toBeNull(); expect(updatedInstructor.toJSON()).toEqual(expect.objectContaining(mockInstructorData)); + }); + + afterEach(async () => { + if (newInstructor) { + await InstructorModel.destroy({ where: { id: newInstructor.id } }); + newInstructor = undefined; } - afterEach(async () => { - // Delete the test record to clean DB - if (newInstructor) { - await InstructorModel.destroy({ where: { id: newInstructor.id } }); - } - }); -}) \ No newline at end of file + }); +}); diff --git a/tests/department_test.js b/tests/department_test.test.js similarity index 64% rename from tests/department_test.js rename to tests/department_test.test.js index 40d61a6..288f16b 100644 --- a/tests/department_test.js +++ b/tests/department_test.test.js @@ -1,11 +1,12 @@ // tests/DepartmentService.test.js import { describe, it, expect, beforeEach, vi } from 'vitest'; -const path = require('path'); -const base = path.resolve(__dirname, '../'); -const sequelize = require(path.join(base, 'src', 'config', 'db.js')); -const { DataTypes } = require('sequelize'); -const DepartmentModel = require('../src/api/Models/department')(sequelize, DataTypes) -const DepartmentService = require('../src/api/Services/DepartmentServices') +import path from 'path'; +import sequelize from '../src/Config/db.js' +import { DataTypes } from 'sequelize'; +import DepartmentModelFactory from '../src/api/Models/department.js'; +import * as DepartmentService from '../src/api/Services/DepartmentServices.js'; + +const DepartmentModel = DepartmentModelFactory(sequelize, DataTypes); describe('DepartmentService - CreateDepartment', () => { const mockDepartmentData = { @@ -21,16 +22,12 @@ describe('DepartmentService - CreateDepartment', () => { }); it('should create a department successfully', async () => { - // Mock Sequelize create method -// vi.spyOn(DepartmentModel, 'create').mockResolvedValue(null); - const result = await DepartmentService.CreateDeparment(mockDepartmentData); expect(result).toEqual({ message: 'Department created successfully', department: result.department }); - // expect(DepartmentModel.create).toHaveBeenCalledWith(mockDepartmentData); }); it('should throw error if creation fails due to empty department code', async () => { @@ -39,9 +36,6 @@ describe('DepartmentService - CreateDepartment', () => { department_code: '' // force empty code }; - // // Mock create to simulate DB returning null - // vi.spyOn(DepartmentModel, 'create').mockResolvedValue(null); - await expect(DepartmentService.CreateDeparment(invalidDepartmentData)) .rejects .toThrow('Validation error'); @@ -65,26 +59,24 @@ describe("UpdateDepartment Service", () => { ).rejects.toThrow("At-Least One Department fields must be provided to update"); }); -it("should throw error if department not found", async () => { - vi.spyOn(DepartmentModel, "findOne").mockResolvedValue(null); - await expect( - DepartmentService.UpdateDepartment({ - department_id: 123, // make sure this matches your service - department_code:"DDSF", - department_name: "Finance", - }) - ).rejects.toThrow("No Deparment found with the given department id"); -}); + it("should throw error if department not found", async () => { + vi.spyOn(DepartmentModel, "findOne").mockResolvedValue(null); + await expect( + DepartmentService.UpdateDepartment({ + department_id: 123, + department_code:"DDSF", + department_name: "Finance", + }) + ).rejects.toThrow("No Deparment found with the given department id"); + }); it("should update department successfully using real DB record", async () => { - // First, get an existing department from the database - const existingDepartment = await DepartmentModel.findOne(); // fetch first available record + const existingDepartment = await DepartmentModel.findOne(); if (!existingDepartment) { throw new Error("No department found in DB to test update"); } - // Update the department name - const newName = "Finance_" + Date.now(); // to avoid duplicate names + const newName = "Finance_" + Date.now(); const result = await DepartmentService.UpdateDepartment({ department_id: existingDepartment.department_id, department_name: newName, @@ -92,13 +84,13 @@ it("should throw error if department not found", async () => { expect(result).toBe("Department updated successfully"); - // Verify the update in DB const updatedDepartment = await DepartmentModel.findOne({ where: { department_id: existingDepartment.department_id }, }); expect(updatedDepartment.department_name).toBe(newName); }); }); + describe("Delete-Department", () => { it("should throw 'ID Must Be Provided'", async () => { await expect(DepartmentService.DeleteDepartment()) @@ -106,8 +98,9 @@ describe("Delete-Department", () => { .toThrow("ID must be provided"); }); - it("should throw 'No department found with the given ID'", async()=>{ + it("should throw 'No department found with the given ID'", async () => { await expect(DepartmentService.DeleteDepartment(1231)) - .rejects.toThrow("No department found with the given ID"); - }) + .rejects + .toThrow("No department found with the given ID"); + }); });