Skip to content

Commit

Permalink
adding tests to postParcel service
Browse files Browse the repository at this point in the history
  • Loading branch information
TaylorFries committed Feb 21, 2024
1 parent efb0ef8 commit 2df94a3
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 4 deletions.
40 changes: 40 additions & 0 deletions express-api/tests/testUtils/factories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { UUID } from 'crypto';
import { Request, Response } from 'express';
import { Role as RolesEntity } from '@/typeorm/Entities/Role';
import { KeycloakUser } from '@bcgov/citz-imb-kc-express';
import { Parcel } from '@/typeorm/Entities/Parcel';

export class MockRes {
statusValue: any;
Expand Down Expand Up @@ -190,3 +191,42 @@ export const produceKeycloak = (): KeycloakUser => {
family_name: faker.person.lastName(),
};
};

export const produceParcel = (): Parcel => {
return {
Id: faker.number.int({ max: 10 }),
CreatedOn: faker.date.anytime(),
UpdatedOn: faker.date.anytime(),
Name: faker.string.alphanumeric(),
LandLegalDescription: faker.string.alphanumeric(),
PID: undefined,
PIN: undefined,
LandArea: undefined,
Zoning: undefined,
ZoningPotential: undefined,
NotOwned: undefined,
ParentParcelId: undefined,
ParentParcel:undefined,
Description: faker.string.alphanumeric(),
ClassificationId: undefined,
Classification: undefined,
AgencyId: undefined,
Agency: undefined,
AdministrativeAreaId: undefined,
AdministrativeArea: undefined,
IsSensitive: undefined,
IsVisibleToOtherAgencies: undefined,
Location: undefined,
ProjectNumbers: undefined,
PropertyTypeId: undefined,
PropertyType: undefined,
Address1: undefined,
Address2: undefined,
Postal: undefined,
SiteId: undefined,
CreatedById: undefined,
CreatedBy: undefined,
UpdatedById: undefined,
UpdatedBy: undefined
}
}
27 changes: 23 additions & 4 deletions express-api/tests/unit/services/parcels/parcelsService.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,36 @@
import { AppDataSource } from "@/appDataSource";
import { Parcel } from "@/typeorm/Entities/Parcel";
import { produceParcel } from "tests/testUtils/factories";
import { DeepPartial } from "typeorm";
import * as parcelService from "@/services/parcels/parcelServices";

jest.setTimeout(30000);

const parcelRepo = AppDataSource.getRepository(Parcel);

const _parcelSave = jest
.spyOn(parcelRepo, 'save')
.mockImplementation(async (parcel: DeepPartial<Parcel> & Parcel) => parcel);

const _parcelFindOne = jest
.spyOn(parcelRepo, 'findOne')
.mockImplementation(async () => produceParcel());

describe('UNIT - Parcel Services', () => {
describe('addParcel', () => {
it('should add a new parcel and return it', async () => {

})
})
})
_parcelFindOne.mockResolvedValueOnce(null);
const parcel = produceParcel();
const ret = await parcelService.postParcel(parcel);
expect(_parcelSave).toHaveBeenCalledTimes(1);
expect(ret.Id).toBe(parcel.Id);
});
it('should throw an error if the agency already exists', async => {
const parcel = produceParcel();
_parcelFindOne.mockResolvedValueOnce(parcel);
expect(async () => await parcelService.postParcel(parcel)).rejects.toThrow();
});

});

});

0 comments on commit 2df94a3

Please sign in to comment.