11import { patchLetters } from '../../index' ;
2- import type { Context } from 'aws-lambda' ;
2+ import type { APIGatewayProxyResult , Context } from 'aws-lambda' ;
33import { mockDeep } from 'jest-mock-extended' ;
44import { makeApiGwEvent } from './utils/test-utils' ;
55import * as letterService from '../../services/letter-operations' ;
66import { NotFoundError , ValidationError } from '../../errors' ;
77import { LetterApiDocument , LetterApiStatus } from '../../contracts/letter-api' ;
8+ import { ErrorResponse , mapErrorToResponse } from '../../mappers/error-mapper' ;
89
910jest . mock ( '../../services/letter-operations' ) ;
11+ jest . mock ( '../../mappers/error-mapper' ) ;
12+
13+ const mockedMapErrorToResponse = jest . mocked ( mapErrorToResponse ) ;
14+ const expectedErrorResponse : APIGatewayProxyResult = {
15+ statusCode : 400 ,
16+ body : "Error"
17+ } ;
18+ mockedMapErrorToResponse . mockReturnValue ( expectedErrorResponse ) ;
19+
20+ const mockedPatchLetterStatus = jest . mocked ( letterService . patchLetterStatus ) ;
21+
22+ const letterApiDocument = makeLetterApiDocument ( "id1" , "REJECTED" ) ;
23+ const requestBody = JSON . stringify ( letterApiDocument , null , 2 ) ;
1024
1125function makeLetterApiDocument ( id : string , status : LetterApiStatus ) : LetterApiDocument {
1226 return {
@@ -23,13 +37,13 @@ function makeLetterApiDocument(id: string, status: LetterApiStatus) : LetterApiD
2337 } ;
2438}
2539
26- const letterApiDocument = makeLetterApiDocument ( "id1" , "REJECTED" ) ;
27-
28- const requestBody = JSON . stringify ( letterApiDocument , null , 2 ) ;
40+ beforeEach ( ( ) => {
41+ jest . clearAllMocks ( ) ;
42+ } ) ;
2943
3044describe ( 'patchLetters API Handler' , ( ) => {
31- it ( 'returns 200 OK with updated resource' , async ( ) => {
3245
46+ it ( 'returns 200 OK with updated resource' , async ( ) => {
3347 const event = makeApiGwEvent ( {
3448 path : '/letters/id1' ,
3549 body : requestBody ,
@@ -38,7 +52,6 @@ describe('patchLetters API Handler', () => {
3852 const context = mockDeep < Context > ( ) ;
3953 const callback = jest . fn ( ) ;
4054
41- const mockedPatchLetterStatus = letterService . patchLetterStatus as jest . Mock ;
4255 mockedPatchLetterStatus . mockResolvedValue ( letterApiDocument ) ;
4356
4457 const result = await patchLetters ( event , context , callback ) ;
@@ -49,22 +62,20 @@ describe('patchLetters API Handler', () => {
4962 } ) ;
5063 } ) ;
5164
52- it ( 'returns 400 Bad Request as there is no body' , async ( ) => {
65+ it ( 'returns error response when there is no body' , async ( ) => {
5366 const event = makeApiGwEvent ( {
5467 path : '/letters/id1' ,
5568 pathParameters : { id : "id1" } ,
5669 headers : { 'app-supplier-id' : 'supplier1' } } ) ;
5770 const context = mockDeep < Context > ( ) ;
5871 const callback = jest . fn ( ) ;
72+
5973 const result = await patchLetters ( event , context , callback ) ;
6074
61- expect ( result ) . toEqual ( {
62- statusCode : 400 ,
63- body : 'Bad Request: Missing request body' ,
64- } ) ;
75+ expect ( result ) . toEqual ( expectedErrorResponse ) ;
6576 } ) ;
6677
67- it ( 'returns 404 Not Found as path parameter is not found' , async ( ) => {
78+ it ( 'returns error response when path parameter letterId is not found' , async ( ) => {
6879 const event = makeApiGwEvent ( {
6980 path : '/letters/' ,
7081 body : requestBody ,
@@ -73,15 +84,11 @@ describe('patchLetters API Handler', () => {
7384 const callback = jest . fn ( ) ;
7485 const result = await patchLetters ( event , context , callback ) ;
7586
76- expect ( result ) . toEqual ( {
77- statusCode : 404 ,
78- body : 'Not Found: The requested resource does not exist' ,
79- } ) ;
87+ expect ( result ) . toEqual ( expectedErrorResponse ) ;
8088 } ) ;
8189
82- it ( 'returns 400 Bad Request when ValidationError is thrown by service' , async ( ) => {
83- const mockedPatchLetterStatus = letterService . patchLetterStatus as jest . Mock ;
84- mockedPatchLetterStatus . mockRejectedValue ( new ValidationError ( 'Validation failed' ) ) ;
90+ it ( 'returns error response when error is thrown by service' , async ( ) => {
91+ mockedPatchLetterStatus . mockRejectedValue ( new Error ( 'Service error' ) ) ;
8592
8693 const event = makeApiGwEvent ( {
8794 path : '/letters/id1' ,
@@ -94,61 +101,19 @@ describe('patchLetters API Handler', () => {
94101
95102 const result = await patchLetters ( event , context , callback ) ;
96103
97- expect ( result ) . toEqual ( {
98- statusCode : 400 ,
99- body : 'Validation failed'
100- } ) ;
104+ expect ( result ) . toEqual ( expectedErrorResponse ) ;
101105 } ) ;
102106
103- it ( 'returns 404 Not Found when NotFoundError is thrown by service' , async ( ) => {
104- const mockedPatchLetterStatus = letterService . patchLetterStatus as jest . Mock ;
105- mockedPatchLetterStatus . mockRejectedValue ( new NotFoundError ( 'Letter not found' ) ) ;
106-
107- const event = makeApiGwEvent ( {
108- path : '/letters/id1' ,
109- body : requestBody ,
110- pathParameters : { id : "id1" } ,
111- headers : { 'app-supplier-id' : 'supplier1' }
112- } ) ;
113- const context = mockDeep < Context > ( ) ;
114- const callback = jest . fn ( ) ;
115-
116- const result = await patchLetters ( event , context , callback ) ;
117-
118- expect ( result ) . toEqual ( {
119- statusCode : 404 ,
120- body : 'Letter not found'
121- } ) ;
122- } ) ;
123-
124- it ( 'throws unexpected errors from service' , async ( ) => {
125- const mockedPatchLetterStatus = letterService . patchLetterStatus as jest . Mock ;
126- mockedPatchLetterStatus . mockRejectedValue ( new Error ( 'Unexpected error' ) ) ;
127-
128- const event = makeApiGwEvent ( {
129- path : '/letters/id1' ,
130- body : requestBody ,
131- pathParameters : { id : "id1" } ,
132- headers : { 'app-supplier-id' : 'supplier1' }
133- } ) ;
134- const context = mockDeep < Context > ( ) ;
135- const callback = jest . fn ( ) ;
136-
137- await expect ( patchLetters ( event , context , callback ) ) . rejects . toThrow ( 'Unexpected error' ) ;
138- } ) ;
139-
140- it ( 'returns 400 Bad Request: Missing supplier ID if header app-supplier-id ' , async ( ) => {
107+ it ( 'returns error when app-supplier-id is missing' , async ( ) => {
141108 const event = makeApiGwEvent ( {
142109 path : '/letters/id1' ,
143110 body : requestBody ,
144111 pathParameters : { id : "id1" } } ) ;
145112 const context = mockDeep < Context > ( ) ;
146113 const callback = jest . fn ( ) ;
114+
147115 const result = await patchLetters ( event , context , callback ) ;
148116
149- expect ( result ) . toEqual ( {
150- statusCode : 400 ,
151- body : 'Bad Request: Missing supplier ID' ,
152- } ) ;
117+ expect ( result ) . toEqual ( expectedErrorResponse ) ;
153118 } ) ;
154119} ) ;
0 commit comments