@@ -17,7 +17,6 @@ import {
1717 generateExpectedItems ,
1818 generateMockEvent ,
1919 mockDynamoDBClient ,
20- mockSQSClient ,
2120 TASK_VALUES
2221} from "./utils/testUtils"
2322
@@ -38,7 +37,21 @@ import {
3837import { QueryCommand , TransactionCanceledException , TransactWriteItemsCommand } from "@aws-sdk/client-dynamodb"
3938
4039const { mockSend : dynamoDBMockSend } = mockDynamoDBClient ( )
41- const { mockSend : sqsMockSend } = mockSQSClient ( )
40+
41+ const mockPushPrescriptionToNotificationSQS = jest . fn ( ) . mockImplementation ( async ( ) => Promise . resolve ( ) )
42+ jest . unstable_mockModule ( "../src/utils/sqsClient" , async ( ) => ( {
43+ __esModule : true ,
44+ pushPrescriptionToNotificationSQS : mockPushPrescriptionToNotificationSQS
45+ } ) )
46+
47+ export const mockGetParameter = jest . fn ( async ( ) => Promise . resolve ( "false" ) )
48+ jest . unstable_mockModule (
49+ "@aws-lambda-powertools/parameters/ssm" ,
50+ async ( ) => ( {
51+ __esModule : true ,
52+ getParameter : mockGetParameter
53+ } )
54+ )
4255
4356const { handler, logger} = await import ( "../src/updatePrescriptionStatus" )
4457
@@ -416,7 +429,8 @@ describe("Integration tests for updatePrescriptionStatus handler", () => {
416429 } )
417430
418431 it ( "when the notification SQS push fails, the response still succeeds" , async ( ) => {
419- sqsMockSend . mockImplementation (
432+ mockGetParameter . mockImplementation ( async ( ) => Promise . resolve ( "true" ) )
433+ mockPushPrescriptionToNotificationSQS . mockImplementation (
420434 async ( ) => {
421435 throw new Error ( "Test error" )
422436 }
@@ -425,13 +439,12 @@ describe("Integration tests for updatePrescriptionStatus handler", () => {
425439 const event : APIGatewayProxyEvent = generateMockEvent ( requestDispatched )
426440 const response : APIGatewayProxyResult = await handler ( event , { } )
427441 expect ( response . statusCode ) . toBe ( 201 )
442+ expect ( mockPushPrescriptionToNotificationSQS ) . toHaveBeenCalled ( )
428443 } )
429444
430- it ( "when SQS environment variables are not set, the response still succeeds" , async ( ) => {
431- process . env . NHS_NOTIFY_PRESCRIPTIONS_SQS_QUEUE_URL = undefined
432- process . env . AWS_REGION = undefined
433-
434- sqsMockSend . mockImplementation (
445+ it ( "when SQS push throws an error, the response still succeeds" , async ( ) => {
446+ mockGetParameter . mockImplementation ( async ( ) => Promise . resolve ( "true" ) )
447+ mockPushPrescriptionToNotificationSQS . mockImplementation (
435448 async ( ) => {
436449 throw new Error ( "Test error" )
437450 }
@@ -440,5 +453,33 @@ describe("Integration tests for updatePrescriptionStatus handler", () => {
440453 const event : APIGatewayProxyEvent = generateMockEvent ( requestDispatched )
441454 const response : APIGatewayProxyResult = await handler ( event , { } )
442455 expect ( response . statusCode ) . toBe ( 201 )
456+ expect ( mockPushPrescriptionToNotificationSQS ) . toHaveBeenCalled ( )
457+ } )
458+
459+ it ( "When the get parameter call throws an error, the request succeeds and the sqs queue is untouched" , async ( ) => {
460+ mockGetParameter . mockImplementation ( async ( ) => Promise . reject ( new Error ( "Failed" ) ) )
461+
462+ const rejected_event : APIGatewayProxyEvent = generateMockEvent ( requestDispatched )
463+ const rejected_response : APIGatewayProxyResult = await handler ( rejected_event , { } )
464+ expect ( rejected_response . statusCode ) . toBe ( 201 )
465+ expect ( mockPushPrescriptionToNotificationSQS ) . not . toHaveBeenCalled ( )
466+ } )
467+
468+ it ( "When the enable notifications parameter is false, the push to SQS is skipped" , async ( ) => {
469+ mockGetParameter . mockImplementation ( async ( ) => Promise . resolve ( "false" ) )
470+
471+ const bypass_event : APIGatewayProxyEvent = generateMockEvent ( requestDispatched )
472+ const bypass_response : APIGatewayProxyResult = await handler ( bypass_event , { } )
473+ expect ( bypass_response . statusCode ) . toBe ( 201 )
474+ expect ( mockPushPrescriptionToNotificationSQS ) . not . toHaveBeenCalled ( )
475+ } )
476+
477+ it ( "When the enable notifications parameter is true, the push to SQS is done" , async ( ) => {
478+ mockGetParameter . mockImplementation ( async ( ) => Promise . resolve ( "true" ) )
479+
480+ const successful_event : APIGatewayProxyEvent = generateMockEvent ( requestDispatched )
481+ const successful_response : APIGatewayProxyResult = await handler ( successful_event , { } )
482+ expect ( successful_response . statusCode ) . toBe ( 201 )
483+ expect ( mockPushPrescriptionToNotificationSQS ) . toHaveBeenCalled ( )
443484 } )
444485} )
0 commit comments