diff --git a/backend/src/recreation-resource/recreation-resource.controller.spec.ts b/backend/src/recreation-resource/recreation-resource.controller.spec.ts index a49fcd20..4d049d73 100644 --- a/backend/src/recreation-resource/recreation-resource.controller.spec.ts +++ b/backend/src/recreation-resource/recreation-resource.controller.spec.ts @@ -1,20 +1,80 @@ import { Test, TestingModule } from "@nestjs/testing"; +import { INestApplication } from "@nestjs/common"; import { RecreationResourceController } from "./recreation-resource.controller"; +import { RecreationResourceService } from "./recreation-resource.service"; +import { PrismaService } from "nestjs-prisma"; +import { HttpException } from "@nestjs/common"; describe("RecreationResourceController", () => { + let recService: RecreationResourceService; let controller: RecreationResourceController; + let app: INestApplication; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ controllers: [RecreationResourceController], + providers: [ + RecreationResourceService, + { + provide: PrismaService, + useValue: {}, + }, + ], }).compile(); + recService = module.get( + RecreationResourceService, + ); controller = module.get( RecreationResourceController, ); + app = module.createNestApplication(); + await app.init(); + }); + + // Close the app after each test + afterEach(async () => { + await app.close(); }); it("should be defined", () => { expect(controller).toBeDefined(); }); + + describe("findAll", () => { + it("should return an array of users", async () => { + const result = []; + result.push({ + forest_file_id: "REC0001", + name: "Rec site 1", + description: "Rec site 1 description", + site_location: "Rec site 1 location", + }); + vi.spyOn(recService, "findAll").mockResolvedValue(result); + expect(await controller.findAll()).toBe(result); + }); + }); + + describe("findOne", () => { + it("should return a Recreation Resource object", async () => { + const result = { + forest_file_id: "REC0001", + name: "Rec site 1", + description: "Rec site 1 description", + site_location: "Rec site 1 location", + }; + vi.spyOn(recService, "findOne").mockResolvedValue(result); + expect(await controller.findOne("REC0001")).toBe(result); + }); + + it("should throw error if recreation resource not found", async () => { + vi.spyOn(recService, "findOne").mockResolvedValue(undefined); + try { + await controller.findOne("REC0001"); + } catch (e) { + expect(e).toBeInstanceOf(HttpException); + expect(e.message).toBe("Recreation Resource not found."); + } + }); + }); }); diff --git a/backend/src/recreation-resource/recreation-resource.controller.ts b/backend/src/recreation-resource/recreation-resource.controller.ts index f625b9f1..28f1733a 100644 --- a/backend/src/recreation-resource/recreation-resource.controller.ts +++ b/backend/src/recreation-resource/recreation-resource.controller.ts @@ -11,13 +11,13 @@ export class RecreationResourceController { ) {} @Get() - getRecreationResources(): Promise { - return this.recreationResourceService.getAll(); + findAll(): Promise { + return this.recreationResourceService.findAll(); } @Get(":id") - async getRecreationResource(@Param("id") id: string) { - const recResource = await this.recreationResourceService.getOne(id); + async findOne(@Param("id") id: string) { + const recResource = await this.recreationResourceService.findOne(id); if (!recResource) { throw new HttpException("Recreation Resource not found.", 404); } diff --git a/backend/src/recreation-resource/recreation-resource.service.spec.ts b/backend/src/recreation-resource/recreation-resource.service.spec.ts index 5071efc2..4426ac2b 100644 --- a/backend/src/recreation-resource/recreation-resource.service.spec.ts +++ b/backend/src/recreation-resource/recreation-resource.service.spec.ts @@ -1,12 +1,40 @@ import { Test, TestingModule } from "@nestjs/testing"; +import { PrismaService } from "nestjs-prisma"; import { RecreationResourceService } from "./recreation-resource.service"; describe("RecreationResourceService", () => { let service: RecreationResourceService; + const recreationResource1 = { + forest_file_id: "REC0001", + name: "Rec site 1", + description: "Rec site 1 description", + site_location: "Rec site 1 location", + }; + + const recreationResource2 = { + forest_file_id: "REC0002", + name: "Rec site 2", + description: "Rec site 2 description", + site_location: "Rec site 2 location", + }; + + const recresourceArray = [recreationResource1, recreationResource2]; + beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ - providers: [RecreationResourceService], + providers: [ + RecreationResourceService, + { + provide: PrismaService, + useValue: { + recreation_resource: { + findMany: vi.fn().mockResolvedValue(recresourceArray), + findUnique: vi.fn().mockResolvedValue(recreationResource1), + }, + }, + }, + ], }).compile(); service = module.get(RecreationResourceService); @@ -15,4 +43,17 @@ describe("RecreationResourceService", () => { it("should be defined", () => { expect(service).toBeDefined(); }); + + describe("findOne", () => { + it("should return a Recreation Resource", async () => { + expect(service.findOne("REC0001")).resolves.toEqual(recreationResource1); + }); + }); + + describe("findAll", () => { + it("should return an array of Recreation Resources", async () => { + const users = await service.findAll(); + expect(users).toEqual(recresourceArray); + }); + }); }); diff --git a/backend/src/recreation-resource/recreation-resource.service.ts b/backend/src/recreation-resource/recreation-resource.service.ts index 31fcac95..a9125669 100644 --- a/backend/src/recreation-resource/recreation-resource.service.ts +++ b/backend/src/recreation-resource/recreation-resource.service.ts @@ -6,7 +6,7 @@ import { RecreationResourceDto } from "./dto/recreation-resource.dto"; export class RecreationResourceService { constructor(private prisma: PrismaService) {} - async getAll(): Promise { + async findAll(): Promise { const recResources = await this.prisma.recreation_resource.findMany(); return recResources.flatMap((recResource) => { @@ -21,7 +21,7 @@ export class RecreationResourceService { }); } - async getOne(id: string): Promise { + async findOne(id: string): Promise { const recResource = await this.prisma.recreation_resource.findUnique({ where: { forest_file_id: id, diff --git a/eslint.config.mjs b/eslint.config.mjs index ad64e9d2..9a45dca8 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -76,6 +76,7 @@ export default [ { endOfLine: "auto", }, + { usePrettierrc: true }, ], "@typescript-eslint/explicit-module-boundary-types": "off",