generated from UK-Export-Finance/nestjs-template
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(DTFS2-7052): tests for ordnance survey API endpoint
- Loading branch information
Showing
7 changed files
with
150 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { GEOSPATIAL } from '@ukef/constants'; | ||
import { GetGeospatialAddressesGenerator } from '@ukef-test/support/generator/get-geospatial-addresses-generator'; | ||
import { RandomValueGenerator } from '@ukef-test/support/generator/random-value-generator'; | ||
import { resetAllWhenMocks, when } from 'jest-when'; | ||
|
||
import { GeospatialController } from './geospatial.controller'; | ||
import { GeospatialService } from './geospatial.service'; | ||
|
||
describe('GeospatialController', () => { | ||
let geospatialServiceGetAddressesByPostcode: jest.Mock; | ||
|
||
let controller: GeospatialController; | ||
|
||
const valueGenerator = new RandomValueGenerator(); | ||
const { getAddressByPostcodeResponse, getAddressByPostcodeMultipleResponse } = new GetGeospatialAddressesGenerator(valueGenerator).generate({ | ||
numberToGenerate: 2, | ||
}); | ||
|
||
beforeEach(() => { | ||
resetAllWhenMocks(); | ||
const geospatialService = new GeospatialService(null); | ||
geospatialServiceGetAddressesByPostcode = jest.fn(); | ||
geospatialService.getAddressesByPostcode = geospatialServiceGetAddressesByPostcode; | ||
|
||
controller = new GeospatialController(geospatialService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(GeospatialController).toBeDefined(); | ||
}); | ||
|
||
describe('getAddressesByPostcode()', () => { | ||
const postcode = GEOSPATIAL.EXAMPLES.POSTCODE; | ||
|
||
it('returns address for postcode', async () => { | ||
when(geospatialServiceGetAddressesByPostcode).calledWith(postcode).mockResolvedValueOnce(getAddressByPostcodeResponse[0]); | ||
|
||
const response = await controller.getAddressesByPostcode({ postcode }); | ||
|
||
expect(geospatialServiceGetAddressesByPostcode).toHaveBeenCalled(); | ||
expect(response).toEqual(getAddressByPostcodeResponse[0]); | ||
}); | ||
|
||
it('returns multiple addressess for postcode', async () => { | ||
when(geospatialServiceGetAddressesByPostcode).calledWith(postcode).mockResolvedValueOnce(getAddressByPostcodeMultipleResponse); | ||
|
||
const response = await controller.getAddressesByPostcode({ postcode }); | ||
|
||
expect(geospatialServiceGetAddressesByPostcode).toHaveBeenCalled(); | ||
expect(response).toEqual(getAddressByPostcodeMultipleResponse); | ||
}); | ||
|
||
it('returns empty response for postcode', async () => { | ||
when(geospatialServiceGetAddressesByPostcode).calledWith(postcode).mockResolvedValueOnce([]); | ||
|
||
const response = await controller.getAddressesByPostcode({ postcode }); | ||
|
||
expect(geospatialServiceGetAddressesByPostcode).toHaveBeenCalled(); | ||
expect(response).toEqual([]); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { ConfigService } from '@nestjs/config'; | ||
import { OrdnanceSurveyService } from '@ukef/helper-modules/ordnance-survey/ordnance-survey.service'; | ||
import { GetGeospatialAddressesGenerator } from '@ukef-test/support/generator/get-geospatial-addresses-generator'; | ||
import { RandomValueGenerator } from '@ukef-test/support/generator/random-value-generator'; | ||
import { resetAllWhenMocks, when } from 'jest-when'; | ||
|
||
import { GeospatialService } from './geospatial.service'; | ||
|
||
jest.mock('@ukef/modules/informatica/informatica.service'); | ||
|
||
describe('CustomerService', () => { | ||
const valueGenerator = new RandomValueGenerator(); | ||
|
||
let service: GeospatialService; | ||
let configServiceGet: jest.Mock; | ||
let informaticaServiceGetAddressesByPostcode: jest.Mock; | ||
|
||
beforeEach(() => { | ||
const configService = new ConfigService(); | ||
configServiceGet = jest.fn().mockReturnValue({ key: valueGenerator.word() }); | ||
configService.get = configServiceGet; | ||
|
||
informaticaServiceGetAddressesByPostcode = jest.fn(); | ||
const ordnanceSurveyService = new OrdnanceSurveyService(null, configService); | ||
ordnanceSurveyService.getAddressesByPostcode = informaticaServiceGetAddressesByPostcode; | ||
resetAllWhenMocks(); | ||
|
||
service = new GeospatialService(ordnanceSurveyService); | ||
}); | ||
|
||
describe('getAddressesByPostcode', () => { | ||
const { | ||
getAddressByPostcodeResponse, | ||
getAddressByPostcodeMultipleResponse, | ||
getAddressOrdnanceSurveyResponse, | ||
getAddressOrdnanceSurveyMultipleResponse, | ||
getAddressOrdnanceSurveyEmptyResponse, | ||
} = new GetGeospatialAddressesGenerator(valueGenerator).generate({ | ||
numberToGenerate: 2, | ||
}); | ||
const postcode = getAddressByPostcodeResponse[0][0].postalCode; | ||
|
||
it('returns address from the backend service', async () => { | ||
when(informaticaServiceGetAddressesByPostcode).calledWith(postcode).mockResolvedValueOnce(getAddressOrdnanceSurveyResponse[0]); | ||
|
||
const response = await service.getAddressesByPostcode(postcode); | ||
|
||
expect(response).toEqual(getAddressByPostcodeResponse[0]); | ||
}); | ||
|
||
it('returns multiple addressess from the backend service', async () => { | ||
when(informaticaServiceGetAddressesByPostcode).calledWith(postcode).mockResolvedValueOnce(getAddressOrdnanceSurveyMultipleResponse); | ||
|
||
const response = await service.getAddressesByPostcode(postcode); | ||
|
||
expect(response).toEqual(getAddressByPostcodeMultipleResponse); | ||
}); | ||
|
||
it('can handle empty backend response', async () => { | ||
when(informaticaServiceGetAddressesByPostcode).calledWith(postcode).mockResolvedValueOnce(getAddressOrdnanceSurveyEmptyResponse[0]); | ||
|
||
const response = await service.getAddressesByPostcode(postcode); | ||
|
||
expect(response).toEqual([]); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters