forked from hngprojects/hng_boilerplate_expressjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Xcoder
committed
Jul 24, 2024
1 parent
8bcc99f
commit 8c1df57
Showing
1 changed file
with
64 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// src/services/AdminOrganisationService.spec.ts | ||
import AppDataSource from "../data-source"; | ||
import { Organization } from "../models"; | ||
import { HttpError } from "../middleware"; | ||
import { AdminOrganisationService } from "../services/admin.services"; | ||
import { validate as uuidValidate } from "uuid"; | ||
|
||
jest.mock("../data-source", () => { | ||
return { | ||
AppDataSource: { | ||
getRepository: jest.fn(), | ||
}, | ||
}; | ||
}); | ||
|
||
jest.mock("../models"); | ||
jest.mock("uuid", () => ({ | ||
validate: jest.fn(), | ||
})); | ||
|
||
describe("AdminOrganisationService - getById", () => { | ||
let adminOrganisationService: AdminOrganisationService; | ||
let mockRepository; | ||
|
||
beforeEach(() => { | ||
adminOrganisationService = new AdminOrganisationService(); | ||
mockRepository = { | ||
findOne: jest.fn(), | ||
}; | ||
(AppDataSource.getRepository as jest.Mock).mockReturnValue(mockRepository); | ||
}); | ||
|
||
it("should return the organization if it exists", async () => { | ||
const org_id = "valid-uuid"; | ||
const mockOrganization = { id: org_id, name: "Org" }; | ||
|
||
(uuidValidate as jest.Mock).mockReturnValue(true); | ||
mockRepository.findOne.mockResolvedValue(mockOrganization); | ||
|
||
const result = await adminOrganisationService.getSingleOrgById({ params: { id: org_id } } as any); | ||
|
||
expect(result).toEqual(mockOrganization); | ||
expect(mockRepository.findOne).toHaveBeenCalledWith({ where: { id: org_id } }); | ||
}); | ||
|
||
it("should throw a 400 error for an invalid UUID", async () => { | ||
const org_id = "invalid-uuid"; | ||
|
||
(uuidValidate as jest.Mock).mockReturnValue(false); | ||
|
||
await expect(adminOrganisationService.getSingleOrgById({ params: { id: org_id } } as any)) | ||
.rejects.toThrow(new HttpError(400, "Invalid organization ID.")); | ||
}); | ||
|
||
it("should throw a 404 error if the organization is not found", async () => { | ||
const org_id = "valid-uuid"; | ||
|
||
(uuidValidate as jest.Mock).mockReturnValue(true); | ||
mockRepository.findOne.mockResolvedValue(null); | ||
|
||
await expect(adminOrganisationService.getSingleOrgById({ params: { id: org_id } } as any)) | ||
.rejects.toThrow(new HttpError(404, "Organization not found.")); | ||
}); | ||
}); |