@@ -17,6 +17,7 @@ import {
1717 generateExpectedItems ,
1818 generateMockEvent ,
1919 mockDynamoDBClient ,
20+ mockSQSClient ,
2021 TASK_VALUES
2122} from "./utils/testUtils"
2223
@@ -36,7 +37,8 @@ import {
3637} from "../src/utils/responses"
3738import { QueryCommand , TransactionCanceledException , TransactWriteItemsCommand } from "@aws-sdk/client-dynamodb"
3839
39- const { mockSend} = mockDynamoDBClient ( )
40+ const { mockSend : dynamoDBMockSend } = mockDynamoDBClient ( )
41+ const { mockSend : sqsMockSend } = mockSQSClient ( )
4042const { handler, logger} = await import ( "../src/updatePrescriptionStatus" )
4143const LAMBDA_TIMEOUT_MS = 9500 // 9.5 sec
4244
@@ -75,7 +77,7 @@ describe("Integration tests for updatePrescriptionStatus handler", () => {
7577 expect ( response . statusCode ) . toEqual ( 201 )
7678 expect ( JSON . parse ( response . body ) ) . toEqual ( responseSingleItem )
7779
78- expect ( mockSend ) . toHaveBeenCalledWith (
80+ expect ( dynamoDBMockSend ) . toHaveBeenCalledWith (
7981 expect . objectContaining ( expectedItems )
8082 )
8183 } )
@@ -101,7 +103,7 @@ describe("Integration tests for updatePrescriptionStatus handler", () => {
101103 expect ( JSON . parse ( response . body ) ) . toEqual ( responseSingleItem )
102104
103105 expect ( expectedItems . input . TransactItems [ 0 ] . Put . Item . RepeatNo ) . toEqual ( undefined )
104- expect ( mockSend ) . toHaveBeenCalledWith (
106+ expect ( dynamoDBMockSend ) . toHaveBeenCalledWith (
105107 expect . objectContaining ( expectedItems )
106108 )
107109 } )
@@ -126,7 +128,7 @@ describe("Integration tests for updatePrescriptionStatus handler", () => {
126128 expect ( JSON . parse ( response . body ) ) . toEqual ( responseSingleItem )
127129
128130 expect ( expectedItems . input . TransactItems [ 0 ] . Put . Item . RepeatNo ) . toEqual ( 1 )
129- expect ( mockSend ) . toHaveBeenCalledWith (
131+ expect ( dynamoDBMockSend ) . toHaveBeenCalledWith (
130132 expect . objectContaining ( expectedItems )
131133 )
132134 } )
@@ -141,7 +143,7 @@ describe("Integration tests for updatePrescriptionStatus handler", () => {
141143 expect ( response . statusCode ) . toEqual ( 201 )
142144 expect ( JSON . parse ( response . body ) ) . toEqual ( responseMultipleItems )
143145
144- expect ( mockSend ) . toHaveBeenCalledWith (
146+ expect ( dynamoDBMockSend ) . toHaveBeenCalledWith (
145147 expect . objectContaining ( expectedItems )
146148 )
147149 } )
@@ -194,7 +196,7 @@ describe("Integration tests for updatePrescriptionStatus handler", () => {
194196
195197 it ( "when dynamo call fails, expect 500 status code and internal server error message" , async ( ) => {
196198 const event = generateMockEvent ( requestDispatched )
197- mockSend . mockRejectedValue ( new Error ( ) as never )
199+ dynamoDBMockSend . mockRejectedValue ( new Error ( ) as never )
198200
199201 const response : APIGatewayProxyResult = await handler ( event , { } )
200202
@@ -203,7 +205,7 @@ describe("Integration tests for updatePrescriptionStatus handler", () => {
203205 } )
204206
205207 it ( "when data store update times out, expect 504 status code and relevant error message" , async ( ) => {
206- mockSend . mockImplementation ( ( command ) => new Promise ( ( resolve ) => {
208+ dynamoDBMockSend . mockImplementation ( ( command ) => new Promise ( ( resolve ) => {
207209 if ( ! ( command instanceof TransactWriteItemsCommand ) ) {
208210 resolve ( false )
209211 }
@@ -279,7 +281,7 @@ describe("Integration tests for updatePrescriptionStatus handler", () => {
279281 const body = generateBody ( )
280282 const mockEvent : APIGatewayProxyEvent = generateMockEvent ( body )
281283
282- mockSend . mockRejectedValue (
284+ dynamoDBMockSend . mockRejectedValue (
283285 new TransactionCanceledException ( {
284286 message :
285287 "DynamoDB transaction cancelled due to conditional check failure." ,
@@ -324,7 +326,7 @@ describe("Integration tests for updatePrescriptionStatus handler", () => {
324326 requestDuplicateItems
325327 )
326328
327- mockSend . mockRejectedValue (
329+ dynamoDBMockSend . mockRejectedValue (
328330 new TransactionCanceledException ( {
329331 message :
330332 "DynamoDB transaction cancelled due to conditional check failure." ,
@@ -375,7 +377,7 @@ describe("Integration tests for updatePrescriptionStatus handler", () => {
375377 const mockEvent : APIGatewayProxyEvent = generateMockEvent ( body )
376378 const loggerSpy = jest . spyOn ( logger , "info" )
377379
378- mockSend . mockImplementation (
380+ dynamoDBMockSend . mockImplementation (
379381 async ( command ) => {
380382 if ( command instanceof QueryCommand ) {
381383 return new Object ( { Items : [
@@ -407,4 +409,17 @@ describe("Integration tests for updatePrescriptionStatus handler", () => {
407409 }
408410 )
409411 } )
412+
413+ it ( "when the notification SQS push fails, the response still succeeds" , async ( ) => {
414+ // TODO: I'm not convinced this is working...
415+ sqsMockSend . mockImplementation (
416+ async ( ) => {
417+ throw new Error ( "Test error" )
418+ }
419+ )
420+
421+ const event : APIGatewayProxyEvent = generateMockEvent ( requestDispatched )
422+ const response : APIGatewayProxyResult = await handler ( event , { } )
423+ expect ( response . statusCode ) . toBe ( 201 )
424+ } )
410425} )
0 commit comments