@@ -12,37 +12,33 @@ mockedMapErrorToResponse.mockReturnValue(expectedErrorResponse);
1212jest . mock ( '../../services/letter-operations' ) ;
1313import * as letterService from '../../services/letter-operations' ;
1414
15- // mock dependencies
16- jest . mock ( "../../config/deps" , ( ) => ( { getDeps : jest . fn ( ) } ) ) ;
17- import { Deps , getDeps } from "../../config/deps" ;
18- const mockedGetDeps = getDeps as jest . Mock < Deps > ;
19- const fakeDeps : jest . Mocked < Deps > = {
20- s3Client : { } as unknown as S3Client ,
21- letterRepo : { } as unknown as LetterRepository ,
22- logger : { info : jest . fn ( ) , error : jest . fn ( ) } as unknown as pino . Logger ,
23- env : {
24- SUPPLIER_ID_HEADER : 'nhsd-supplier-id' ,
25- APIM_CORRELATION_HEADER : 'nhsd-correlation-id' ,
26- LETTERS_TABLE_NAME : 'LETTERS_TABLE_NAME' ,
27- LETTER_TTL_HOURS : 'LETTER_TTL_HOURS' ,
28- DOWNLOAD_URL_TTL_SECONDS : 'DOWNLOAD_URL_TTL_SECONDS'
29- } as unknown as LambdaEnv
30- }
31- mockedGetDeps . mockReturnValue ( fakeDeps ) ;
32-
3315import type { APIGatewayProxyResult , Context } from 'aws-lambda' ;
3416import { mockDeep } from 'jest-mock-extended' ;
3517import { makeApiGwEvent } from './utils/test-utils' ;
3618import { ValidationError } from '../../errors' ;
3719import * as errors from '../../contracts/errors' ;
38- import { getLetterData } from '../get-letter-data' ;
20+ import { createGetLetterDataHandler } from '../get-letter-data' ;
3921import { S3Client } from '@aws-sdk/client-s3' ;
4022import pino from 'pino' ;
4123import { LetterRepository } from '../../../../../internal/datastore/src' ;
42- import { LambdaEnv } from '../../config/env' ;
24+ import { EnvVars } from '../../config/env' ;
25+ import { Deps } from "../../config/deps" ;
4326
4427describe ( 'API Lambda handler' , ( ) => {
4528
29+ const mockedDeps : jest . Mocked < Deps > = {
30+ s3Client : { } as unknown as S3Client ,
31+ letterRepo : { } as unknown as LetterRepository ,
32+ logger : { info : jest . fn ( ) , error : jest . fn ( ) } as unknown as pino . Logger ,
33+ env : {
34+ SUPPLIER_ID_HEADER : 'nhsd-supplier-id' ,
35+ APIM_CORRELATION_HEADER : 'nhsd-correlation-id' ,
36+ LETTERS_TABLE_NAME : 'LETTERS_TABLE_NAME' ,
37+ LETTER_TTL_HOURS : 1 ,
38+ DOWNLOAD_URL_TTL_SECONDS : 1
39+ } as unknown as EnvVars
40+ }
41+
4642 beforeEach ( ( ) => {
4743 jest . clearAllMocks ( ) ;
4844 } ) ;
@@ -59,7 +55,8 @@ describe('API Lambda handler', () => {
5955 const context = mockDeep < Context > ( ) ;
6056 const callback = jest . fn ( ) ;
6157
62- const result = await getLetterData ( event , context , callback ) ;
58+ const getLetterDataHandler = createGetLetterDataHandler ( mockedDeps ) ;
59+ const result = await getLetterDataHandler ( event , context , callback ) ;
6360
6461 expect ( result ) . toEqual ( {
6562 statusCode : 303 ,
@@ -77,9 +74,10 @@ describe('API Lambda handler', () => {
7774 const context = mockDeep < Context > ( ) ;
7875 const callback = jest . fn ( ) ;
7976
80- const result = await getLetterData ( event , context , callback ) ;
77+ const getLetterDataHandler = createGetLetterDataHandler ( mockedDeps ) ;
78+ const result = await getLetterDataHandler ( event , context , callback ) ;
8179
82- expect ( mockedMapErrorToResponse ) . toHaveBeenCalledWith ( new Error ( 'The request headers are empty' ) , undefined , mockedGetDeps ( ) . logger ) ;
80+ expect ( mockedMapErrorToResponse ) . toHaveBeenCalledWith ( new Error ( 'The request headers are empty' ) , undefined , mockedDeps . logger ) ;
8381 expect ( result ) . toEqual ( expectedErrorResponse ) ;
8482 } ) ;
8583
@@ -93,9 +91,10 @@ describe('API Lambda handler', () => {
9391 const context = mockDeep < Context > ( ) ;
9492 const callback = jest . fn ( ) ;
9593
96- const result = await getLetterData ( event , context , callback ) ;
94+ const getLetterDataHandler = createGetLetterDataHandler ( mockedDeps ) ;
95+ const result = await getLetterDataHandler ( event , context , callback ) ;
9796
98- expect ( mockedMapErrorToResponse ) . toHaveBeenCalledWith ( new Error ( "The request headers don't contain the APIM correlation id" ) , undefined , mockedGetDeps ( ) . logger ) ;
97+ expect ( mockedMapErrorToResponse ) . toHaveBeenCalledWith ( new Error ( "The request headers don't contain the APIM correlation id" ) , undefined , mockedDeps . logger ) ;
9998 expect ( result ) . toEqual ( expectedErrorResponse ) ;
10099 } ) ;
101100
@@ -107,9 +106,10 @@ describe('API Lambda handler', () => {
107106 const context = mockDeep < Context > ( ) ;
108107 const callback = jest . fn ( ) ;
109108
110- const result = await getLetterData ( event , context , callback ) ;
109+ const getLetterDataHandler = createGetLetterDataHandler ( mockedDeps ) ;
110+ const result = await getLetterDataHandler ( event , context , callback ) ;
111111
112- expect ( mockedMapErrorToResponse ) . toHaveBeenCalledWith ( new ValidationError ( errors . ApiErrorDetail . InvalidRequestMissingLetterIdPathParameter ) , 'correlationId' , mockedGetDeps ( ) . logger ) ;
112+ expect ( mockedMapErrorToResponse ) . toHaveBeenCalledWith ( new ValidationError ( errors . ApiErrorDetail . InvalidRequestMissingLetterIdPathParameter ) , 'correlationId' , mockedDeps . logger ) ;
113113 expect ( result ) . toEqual ( expectedErrorResponse ) ;
114114 } ) ;
115115} ) ;
0 commit comments