Skip to content

Commit

Permalink
chore: fix backend test issues
Browse files Browse the repository at this point in the history
  • Loading branch information
marcellmueller committed Dec 10, 2024
1 parent 757b2b0 commit 0144832
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -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>(
RecreationResourceService,
);
controller = module.get<RecreationResourceController>(
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.");
}
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ export class RecreationResourceController {
) {}

@Get()
getRecreationResources(): Promise<RecreationResourceDto[]> {
return this.recreationResourceService.getAll();
findAll(): Promise<RecreationResourceDto[]> {
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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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>(RecreationResourceService);
Expand All @@ -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);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { RecreationResourceDto } from "./dto/recreation-resource.dto";
export class RecreationResourceService {
constructor(private prisma: PrismaService) {}

async getAll(): Promise<RecreationResourceDto[]> {
async findAll(): Promise<RecreationResourceDto[]> {
const recResources = await this.prisma.recreation_resource.findMany();

return recResources.flatMap((recResource) => {
Expand All @@ -21,7 +21,7 @@ export class RecreationResourceService {
});
}

async getOne(id: string): Promise<RecreationResourceDto> {
async findOne(id: string): Promise<RecreationResourceDto> {
const recResource = await this.prisma.recreation_resource.findUnique({
where: {
forest_file_id: id,
Expand Down
1 change: 1 addition & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export default [
{
endOfLine: "auto",
},
{ usePrettierrc: true },
],

"@typescript-eslint/explicit-module-boundary-types": "off",
Expand Down

0 comments on commit 0144832

Please sign in to comment.