Skip to content

Commit

Permalink
Add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
pratishta committed Jun 18, 2024
1 parent d2e9be2 commit 719563b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/borough/borough.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { BoroughRepositoryMock } from "../../test/borough/borough.repository.moc
import { Test } from "@nestjs/testing";
import {
findBoroughsQueryResponseSchema,
findCapitalProjectsByBoroughIdCommunityDistrictIdQueryResponseSchema,
findCommunityDistrictsByBoroughIdQueryResponseSchema,
} from "src/gen";
import { ResourceNotFoundException } from "src/exception";
Expand Down Expand Up @@ -53,4 +54,21 @@ describe("Borough service unit", () => {
expect(zoningDistrict).rejects.toThrow(ResourceNotFoundException);
});
});

describe("findCapitalProjectsByBoroughIdCommunityDistrictId", () => {
it("service should return a capital projects compliant object", async () => {
const boroughId = boroughRepositoryMock.checkBoroughByIdMocks[0].id;
const communityDistrictId = boroughRepositoryMock.checkCommunityDistrictByIdMocks[0].id;

const capitalProjects =
await boroughService.findCapitalProjectsByBoroughIdCommunityDistrictId({boroughId, communityDistrictId})

expect(() =>
findCapitalProjectsByBoroughIdCommunityDistrictIdQueryResponseSchema.parse(
capitalProjects,
),
).not.toThrow();
});

});
});
28 changes: 28 additions & 0 deletions test/borough/borough.repository.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import {
findManyRepoSchema,
checkByIdRepoSchema,
findCommunityDistrictsByBoroughIdRepoSchema,
findCapitalProjectsByBoroughIdCommunityDistrictIdRepoSchema,
} from "src/borough/borough.repository.schema";
import { generateMock } from "@anatine/zod-mock";
import { capitalProject, communityDistrict } from "src/schema";

Check failure on line 8 in test/borough/borough.repository.mock.ts

View workflow job for this annotation

GitHub Actions / 🔎 Lint

'capitalProject' is defined but never used

Check failure on line 8 in test/borough/borough.repository.mock.ts

View workflow job for this annotation

GitHub Actions / 🔎 Lint

'communityDistrict' is defined but never used

export class BoroughRepositoryMock {
numberOfMocks = 1;
Expand All @@ -16,6 +18,14 @@ export class BoroughRepositoryMock {
return this.checkBoroughByIdMocks.find((row) => row.id === id);
}

checkCommunityDistrictByIdMocks = Array.from(Array(this.numberOfMocks), (_, seed) =>
generateMock(checkByIdRepoSchema, { seed: seed + 1 }),
);

async checkCommunityDistrictById(id: string) {
return this.checkCommunityDistrictByIdMocks.find((row) => row.id === id);
}

findManyMocks = generateMock(findManyRepoSchema);

async findMany() {
Expand All @@ -39,4 +49,22 @@ export class BoroughRepositoryMock {

return results === undefined ? [] : results[id];
}

findCapitalProjectsByBoroughIdCommunityDistrictIdMocks = this.checkCommunityDistrictByIdMocks.map(
(checkCommunityDistrict) => {
return {
[checkCommunityDistrict.id]: generateMock(
findCapitalProjectsByBoroughIdCommunityDistrictIdRepoSchema,

)
}
}
)
async findCapitalProjectsByBoroughIdCommunityDistrictId(communityDistrictId: string) {
const results = this.findCapitalProjectsByBoroughIdCommunityDistrictIdMocks.find(
(capitalProjects) => communityDistrictId in capitalProjects,
);

return results == undefined ? [] : results[communityDistrictId];
}
}

0 comments on commit 719563b

Please sign in to comment.