Skip to content

Commit

Permalink
Basic unit tests for new endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
GrahamS-Quartech committed Jun 20, 2024
1 parent 3451125 commit a6bbce4
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
22 changes: 22 additions & 0 deletions express-api/tests/testUtils/factories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import { ProjectRisk } from '@/typeorm/Entities/ProjectRisk';
import { PropertyType } from '@/typeorm/Entities/PropertyType';
import { ProjectType } from '@/typeorm/Entities/ProjectType';
import { ProjectStatus } from '@/typeorm/Entities/ProjectStatus';
import { PropertyUnion } from '@/typeorm/Entities/views/PropertyUnionView';

export class MockRes {
statusValue: any;
Expand Down Expand Up @@ -958,6 +959,27 @@ export const produceAgencyResponse = (props?: Partial<ProjectAgencyResponse>) =>
return response;
};

export const producePropertyUnion = (props: Partial<PropertyUnion>) => {
const union: PropertyUnion = {
Id: faker.number.int(),
PID: faker.number.int({ max: 999999999 }),
PIN: faker.number.int({ max: 999999999 }),
PropertyType: ['Building', 'Parcel'][faker.number.int({ min: 0, max: 1 })],
AgencyId: faker.number.int(),
Agency: faker.company.name(),
Address: faker.location.streetAddress(),
IsSensitive: false,
UpdatedOn: new Date(),
ClassificationId: faker.number.int(),
Classification: faker.company.buzzNoun(),
AdministrativeAreaId: faker.number.int(),
AdministrativeArea: faker.location.city(),
LandArea: faker.number.float({ max: 99999 }),
...props,
};
return union;
};

export const produceLtsaOrder = (): ILtsaOrder => ({
order: {
productType: 'title',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import {
produceAgency,
produceBuilding,
produceParcel,
producePropertyUnion,
produceUser,
} from '../../../testUtils/factories';
import { Roles } from '@/constants/roles';
import { AppDataSource } from '@/appDataSource';
import { User } from '@/typeorm/Entities/User';
import { Agency } from '@/typeorm/Entities/Agency';
import { getPropertyUnion } from '@/controllers/properties/propertiesController';

const {
getProperties,
Expand All @@ -39,9 +41,12 @@ const _getPropertiesForMap = jest.fn().mockImplementation(async () => [
},
]);

const _getPropertyUnion = jest.fn().mockImplementation(async () => [producePropertyUnion]);

jest.mock('@/services/properties/propertiesServices', () => ({
propertiesFuzzySearch: () => _propertiesFuzzySearch(),
getPropertiesForMap: () => _getPropertiesForMap(),
getPropertiesUnion: () => _getPropertyUnion(),
}));

describe('UNIT - Properties', () => {
Expand Down Expand Up @@ -181,4 +186,12 @@ describe('UNIT - Properties', () => {
expect(mockResponse.jsonValue.length).toBeGreaterThanOrEqual(1);
});
});

describe('GET /properties/', () => {
it('should return status 200', async () => {
await getPropertyUnion(mockRequest, mockResponse);
expect(mockResponse.statusValue).toBe(200);
expect(Array.isArray(mockResponse.sendValue)).toBe(true);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import propertyServices from '@/services/properties/propertiesServices';
import { Building } from '@/typeorm/Entities/Building';
import { Parcel } from '@/typeorm/Entities/Parcel';
import { MapProperties } from '@/typeorm/Entities/views/MapPropertiesView';
import { produceParcel, produceBuilding } from 'tests/testUtils/factories';
import { PropertyUnion } from '@/typeorm/Entities/views/PropertyUnionView';
import { produceParcel, produceBuilding, producePropertyUnion } from 'tests/testUtils/factories';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const _parcelsCreateQueryBuilder: any = {
Expand Down Expand Up @@ -45,6 +46,10 @@ jest.spyOn(AppDataSource.getRepository(MapProperties), 'find').mockImplementatio
} as MapProperties,
]);

jest
.spyOn(AppDataSource.getRepository(PropertyUnion), 'find')
.mockImplementation(async () => [producePropertyUnion({})]);

describe('UNIT - Property Services', () => {
beforeEach(() => {
jest.clearAllMocks();
Expand All @@ -58,6 +63,30 @@ describe('UNIT - Property Services', () => {
});
});

describe('getPropertyUnion', () => {
it('should return a list of buildings and parcels', async () => {
const result = await propertyServices.getPropertiesUnion({
pid: 'contains,123',
pin: 'contains,456',
administrativeArea: 'contains,aaa',
agency: 'startsWith,aaa',
propertyType: 'contains,Building',
sortKey: 'Agency',
sortOrder: 'DESC',
landArea: 'startsWith,1',
updatedOn: 'after,' + new Date(),
});
expect(Array.isArray(result));
expect(result.at(0)).toHaveProperty('PropertyType');
expect(result.at(0)).toHaveProperty('Id');
expect(result.at(0)).toHaveProperty('PIN');
expect(result.at(0)).toHaveProperty('PID');
expect(result.at(0)).toHaveProperty('Agency');
expect(result.at(0)).toHaveProperty('Classification');
expect(result.at(0)).toHaveProperty('AdministrativeArea');
});
});

describe('getPropertiesForMap', () => {
it('should return a list of map property objects', async () => {
const result = await propertyServices.getPropertiesForMap({
Expand Down

0 comments on commit a6bbce4

Please sign in to comment.