-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from DEFRA/fix-redis-addunittests
feat: adding unit tests fo all the services. fixing services varous e…
- Loading branch information
Showing
8 changed files
with
378 additions
and
70 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
const { ServiceBusClient } = require('@azure/service-bus') | ||
jest.mock('@azure/service-bus') | ||
const ASBService = require('../../../server/services/asb.send.js') | ||
|
||
describe('ASBService', () => { | ||
describe('sendMessageToQueue', () => { | ||
it('should send a message to the correct queue', async () => { | ||
// Mock the ServiceBusClient and its methods | ||
const sendMessagesMock = jest.fn() | ||
const createSenderMock = jest | ||
.fn() | ||
.mockReturnValue({ sendMessages: sendMessagesMock }) | ||
const closeMock = jest.fn() | ||
|
||
ServiceBusClient.mockImplementation(() => { | ||
return { | ||
createSender: createSenderMock, | ||
close: closeMock | ||
} | ||
}) | ||
|
||
// Mock the incidentToPublish and incidentType | ||
const incidentToPublish = { | ||
id: 1, | ||
description: 'Illegal fishing incident' | ||
} | ||
const incidentType = 200 | ||
|
||
// Call the function | ||
await ASBService.sendMessageToQueue(incidentToPublish, incidentType) | ||
|
||
// Assert the createSender method call | ||
expect(createSenderMock).toHaveBeenCalledTimes(1) | ||
expect(createSenderMock).toHaveBeenCalledWith(expect.any(String)) | ||
|
||
// Assert the sendMessages method call | ||
expect(sendMessagesMock).toHaveBeenCalledTimes(1) | ||
expect(sendMessagesMock).toHaveBeenCalledWith({ | ||
body: incidentToPublish, | ||
label: incidentType, | ||
userProperties: { myCustomPropertyName: 'Custom property' } | ||
}) | ||
|
||
// Assert the close method call | ||
expect(closeMock).toHaveBeenCalledTimes(2) | ||
}) | ||
}) | ||
}) |
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,50 @@ | ||
const CookieService = require('../../../server/services/cookie.service.js') | ||
|
||
const { SI_SESSION_KEY } = require('../../../server/utils/constants') | ||
|
||
describe('CookieService', () => { | ||
describe('getSessionCookie', () => { | ||
it('should return the session cookie from the request', () => { | ||
// Mock the request object | ||
const request = { | ||
state: { | ||
[SI_SESSION_KEY]: 'sessionCookieValue' | ||
}, | ||
url: { | ||
pathname: '/example' | ||
} | ||
} | ||
|
||
// Call the function | ||
const result = CookieService.getSessionCookie(request) | ||
|
||
// Assert the result | ||
expect(result).toBe('sessionCookieValue') | ||
}) | ||
|
||
it('should log an error message if the session cookie is not found', () => { | ||
// Mock the console.log method | ||
console.log = jest.fn() | ||
|
||
// Mock the request object | ||
const request = { | ||
state: {}, | ||
url: { | ||
pathname: '/example' | ||
} | ||
} | ||
|
||
// Call the function | ||
const result = CookieService.getSessionCookie(request) | ||
|
||
// Assert the result | ||
expect(result).toBeUndefined() | ||
|
||
// Assert the console.log method call | ||
expect(console.log).toHaveBeenCalledTimes(1) | ||
expect(console.log).toHaveBeenCalledWith( | ||
`Session cookie not found for page ${request.url.pathname}` | ||
) | ||
}) | ||
}) | ||
}) |
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,64 @@ | ||
const { | ||
findByPostcode | ||
} = require('../../../server/services/incidentLocation.js') | ||
|
||
const util = require('../../../server/utils/util') | ||
|
||
jest.mock('../../../server/utils/util', () => ({ | ||
getJson: jest.fn() | ||
})) | ||
|
||
describe('OrdnanceService', () => { | ||
describe('findByPostcode', () => { | ||
it('should return the coordinates for a valid postcode', async () => { | ||
// Mock the util.getJson function | ||
util.getJson.mockResolvedValueOnce({ | ||
results: [ | ||
{ | ||
DPA: { | ||
X_COORDINATE: 1.234, | ||
Y_COORDINATE: 2.234 | ||
} | ||
} | ||
] | ||
}) | ||
|
||
// Call the function | ||
const result = await findByPostcode('12345') | ||
|
||
const expectedResults = { | ||
X_COORDINATE: 1.234, | ||
Y_COORDINATE: 2.234 | ||
} | ||
|
||
// Assert the result | ||
expect(result).toEqual(expectedResults) | ||
|
||
// Assert the util.getJson function call | ||
expect(util.getJson).toHaveBeenCalledTimes(1) | ||
expect(util.getJson).toHaveBeenCalledWith( | ||
'https://api.os.uk/search/places/v1/postcode?postcode=12345&key=50br44ij15V5hIAAhLoeFTiY57NZBYHS' | ||
) | ||
}) | ||
|
||
it('should return default coordinates for an invalid postcode', async () => { | ||
// Mock the util.getJson function to throw an error | ||
util.getJson.mockRejectedValueOnce(new Error('Invalid postcode')) | ||
|
||
// Call the function | ||
const result = await findByPostcode('invalid') | ||
|
||
// Assert the result | ||
expect(result).toEqual({ | ||
X_COORDINATE: 51.5, | ||
Y_COORDINATE: 0.0293 | ||
}) | ||
|
||
// Assert the util.getJson function call | ||
// expect(util.getJson).toHaveBeenCalledTimes(1) | ||
expect(util.getJson).toHaveBeenCalledWith( | ||
'https://api.os.uk/search/places/v1/postcode?postcode=invalid&key=50br44ij15V5hIAAhLoeFTiY57NZBYHS' | ||
) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.