@@ -3,18 +3,15 @@ import { mockDeep } from "jest-mock-extended";
33import { makeApiGwEvent } from "./utils/test-utils" ;
44import { PostMIRequest , PostMIResponse } from "../../contracts/mi" ;
55import * as miService from '../../services/mi-operations' ;
6- import { postMi } from '../../index' ;
6+ import pino from 'pino' ;
7+ import { LetterRepository , MIRepository } from "../../../../../internal/datastore/src" ;
8+ import { Deps } from "../../config/deps" ;
9+ import { EnvVars } from "../../config/env" ;
10+ import { createPostMIHandler } from "../post-mi" ;
711
812jest . mock ( '../../services/mi-operations' ) ;
913
10- jest . mock ( '../../config/lambda-config' , ( ) => ( {
11- lambdaConfig : {
12- SUPPLIER_ID_HEADER : 'nhsd-supplier-id' ,
13- APIM_CORRELATION_HEADER : 'nhsd-correlation-id'
14- }
15- } ) ) ;
16-
17- const postMiRequest : PostMIRequest = {
14+ const postMIRequest : PostMIRequest = {
1815 data : {
1916 type : 'ManagementInformation' ,
2017 attributes : {
@@ -27,37 +24,50 @@ const postMiRequest : PostMIRequest = {
2724 }
2825 }
2926} ;
30- const requestBody = JSON . stringify ( postMiRequest , null , 2 ) ;
27+ const requestBody = JSON . stringify ( postMIRequest , null , 2 ) ;
3128
32- const postMiResponse : PostMIResponse = {
29+ const postMIResponse : PostMIResponse = {
3330 data : {
3431 id : 'id1' ,
35- ...postMiRequest . data
32+ ...postMIRequest . data
3633 }
3734 } ;
3835
39- const mockedPostMiOperation = jest . mocked ( miService . postMI ) ;
36+ const mockedPostMIOperation = jest . mocked ( miService . postMI ) ;
4037
4138beforeEach ( ( ) => {
4239 jest . clearAllMocks ( ) ;
4340} ) ;
4441
4542
4643describe ( 'postMI API Handler' , ( ) => {
44+
45+ const mockedDeps : jest . Mocked < Deps > = {
46+ miRepo : { } as unknown as MIRepository ,
47+ logger : { info : jest . fn ( ) , error : jest . fn ( ) } as unknown as pino . Logger ,
48+ env : {
49+ SUPPLIER_ID_HEADER : 'nhsd-supplier-id' ,
50+ APIM_CORRELATION_HEADER : 'nhsd-correlation-id' ,
51+ DOWNLOAD_URL_TTL_SECONDS : 1
52+ } as unknown as EnvVars
53+ } as Deps ;
54+
55+
4756 it ( 'returns 200 OK with updated resource' , async ( ) => {
4857 const event = makeApiGwEvent ( {
4958 path : '/mi' ,
5059 body : requestBody ,
5160 headers : { 'nhsd-supplier-id' : 'supplier1' , 'nhsd-correlation-id' : 'correlationId' }
5261 } ) ;
5362
54- mockedPostMiOperation . mockResolvedValue ( postMiResponse ) ;
63+ mockedPostMIOperation . mockResolvedValue ( postMIResponse ) ;
5564
56- const result = await postMi ( event , mockDeep < Context > ( ) , jest . fn ( ) ) ;
65+ const postMI = createPostMIHandler ( mockedDeps ) ;
66+ const result = await postMI ( event , mockDeep < Context > ( ) , jest . fn ( ) ) ;
5767
5868 expect ( result ) . toEqual ( {
5969 statusCode : 201 ,
60- body : JSON . stringify ( postMiResponse , null , 2 )
70+ body : JSON . stringify ( postMIResponse , null , 2 )
6171 } ) ;
6272 } ) ;
6373
@@ -72,7 +82,8 @@ describe('postMI API Handler', () => {
7282 headers : { 'nhsd-supplier-id' : 'supplier1' , 'nhsd-correlation-id' : 'correlationId' }
7383 } ) ;
7484
75- const result = await postMi ( event , mockDeep < Context > ( ) , jest . fn ( ) ) ;
85+ const postMI = createPostMIHandler ( mockedDeps ) ;
86+ const result = await postMI ( event , mockDeep < Context > ( ) , jest . fn ( ) ) ;
7687
7788 expect ( result ) . toEqual ( expect . objectContaining ( {
7889 statusCode : 400
@@ -85,7 +96,8 @@ describe('postMI API Handler', () => {
8596 headers : { 'nhsd-supplier-id' : 'supplier1' , 'nhsd-correlation-id' : 'correlationId' }
8697 } ) ;
8798
88- const result = await postMi ( event , mockDeep < Context > ( ) , jest . fn ( ) ) ;
99+ const postMI = createPostMIHandler ( mockedDeps ) ;
100+ const result = await postMI ( event , mockDeep < Context > ( ) , jest . fn ( ) ) ;
89101
90102 expect ( result ) . toEqual ( expect . objectContaining ( {
91103 statusCode : 400
@@ -100,9 +112,10 @@ describe('postMI API Handler', () => {
100112 pathParameters : { id : 'id1' } ,
101113 headers : { 'nhsd-supplier-id' : 'supplier1' , 'nhsd-correlation-id' : 'correlationId' }
102114 } ) ;
103- mockedPostMiOperation . mockRejectedValue ( new Error ( ) ) ;
115+ mockedPostMIOperation . mockRejectedValue ( new Error ( ) ) ;
104116
105- const result = await postMi ( event , mockDeep < Context > ( ) , jest . fn ( ) ) ;
117+ const postMI = createPostMIHandler ( mockedDeps ) ;
118+ const result = await postMI ( event , mockDeep < Context > ( ) , jest . fn ( ) ) ;
106119
107120 expect ( result ) . toEqual ( expect . objectContaining ( {
108121 statusCode : 500
@@ -116,7 +129,8 @@ describe('postMI API Handler', () => {
116129 headers : { 'nhsd-correlation-id' : 'correlationId' }
117130 } ) ;
118131
119- const result = await postMi ( event , mockDeep < Context > ( ) , jest . fn ( ) ) ;
132+ const postMI = createPostMIHandler ( mockedDeps ) ;
133+ const result = await postMI ( event , mockDeep < Context > ( ) , jest . fn ( ) ) ;
120134
121135 expect ( result ) . toEqual ( expect . objectContaining ( {
122136 statusCode : 400
@@ -130,7 +144,8 @@ describe('postMI API Handler', () => {
130144 headers : { 'nhsd-supplier-id' : 'supplier1' }
131145 } ) ;
132146
133- const result = await postMi ( event , mockDeep < Context > ( ) , jest . fn ( ) ) ;
147+ const postMI = createPostMIHandler ( mockedDeps ) ;
148+ const result = await postMI ( event , mockDeep < Context > ( ) , jest . fn ( ) ) ;
134149
135150 expect ( result ) . toEqual ( expect . objectContaining ( {
136151 statusCode : 500
@@ -144,7 +159,8 @@ describe('postMI API Handler', () => {
144159 headers : { 'nhsd-supplier-id' : 'supplier1' , 'nhsd-correlation-id' : 'correlationId' }
145160 } ) ;
146161
147- const result = await postMi ( event , mockDeep < Context > ( ) , jest . fn ( ) ) ;
162+ const postMI = createPostMIHandler ( mockedDeps ) ;
163+ const result = await postMI ( event , mockDeep < Context > ( ) , jest . fn ( ) ) ;
148164
149165 expect ( result ) . toEqual ( expect . objectContaining ( {
150166 statusCode : 400
@@ -158,7 +174,8 @@ describe('postMI API Handler', () => {
158174 headers : { 'nhsd-supplier-id' : 'supplier1' , 'nhsd-correlation-id' : 'correlationId' }
159175 } ) ;
160176
161- const result = await postMi ( event , mockDeep < Context > ( ) , jest . fn ( ) ) ;
177+ const postMI = createPostMIHandler ( mockedDeps ) ;
178+ const result = await postMI ( event , mockDeep < Context > ( ) , jest . fn ( ) ) ;
162179
163180 expect ( result ) . toEqual ( expect . objectContaining ( {
164181 statusCode : 400
@@ -175,7 +192,8 @@ describe('postMI API Handler', () => {
175192 throw 'Unexpected error' ;
176193 } )
177194
178- const result = await postMi ( event , mockDeep < Context > ( ) , jest . fn ( ) ) ;
195+ const postMI = createPostMIHandler ( mockedDeps ) ;
196+ const result = await postMI ( event , mockDeep < Context > ( ) , jest . fn ( ) ) ;
179197
180198 expect ( result ) . toEqual ( expect . objectContaining ( {
181199 statusCode : 500
0 commit comments