@@ -73,67 +73,37 @@ describe("Lambda Handler", () => {
7373
7474 describe ( "when content-change-approval-needed feature disabled" , ( ) => {
7575 beforeEach ( ( ) => {
76- ( configProvider as jest . Mock ) . mockImplementation ( ( ) => ( {
77- CONTENT_CACHE_IS_CHANGE_APPROVAL_ENABLED : false ,
78- } ) ) ;
76+ mockConfigProviderWithChangeApprovalSetTo ( false ) ;
7977 } ) ;
8078
8179 it ( "saves new vaccine content when cache was empty" , async ( ) => {
8280 const fetchedContentForVaccine = "some-different-content" ;
83- ( fetchContentForVaccine as jest . Mock ) . mockResolvedValue ( fetchedContentForVaccine ) ;
81+ mockFetchContentForVaccineWith ( fetchedContentForVaccine ) ;
8482
85- await expect ( handler ( { } , context ) ) . resolves . toBeUndefined ( ) ;
83+ await expectHandlerToResolveSuccessfullyWithUndefinedResponse ( { } , context ) ;
8684
87- Object . values ( VaccineTypes ) . forEach ( ( vaccineType ) => {
88- expect ( writeContentForVaccine ) . toHaveBeenCalledWith ( vaccineType , fetchedContentForVaccine ) ;
89- } ) ;
85+ expectWriteContentToHaveBeenCalledForAllVaccinesWithFetchedContent ( fetchedContentForVaccine ) ;
9086 } ) ;
9187
9288 it ( "saves new vaccine content when cache was previously invalidated" , async ( ) => {
93- ( readCachedContentForVaccine as jest . Mock ) . mockResolvedValue ( mockInvalidatedCacheReadResult ) ;
89+ mockReadCachedContentForVaccineWith ( mockInvalidatedCacheReadResult ) ;
9490 const fetchedContentForVaccine = "some-different-content" ;
95- ( fetchContentForVaccine as jest . Mock ) . mockResolvedValue ( fetchedContentForVaccine ) ;
96- ( vitaContentChangedSinceLastApproved as jest . Mock ) . mockReturnValue ( true ) ;
97-
98- await expect ( handler ( { } , context ) ) . resolves . toBeUndefined ( ) ;
99-
100- Object . values ( VaccineTypes ) . forEach ( ( vaccineType ) => {
101- expect ( writeContentForVaccine ) . toHaveBeenCalledWith ( vaccineType , fetchedContentForVaccine ) ;
102- expect ( invalidateCacheForVaccine ) . not . toHaveBeenCalledWith ( vaccineType ) ;
103- } ) ;
91+ await expectWhenContentFetchReturnsToSaveNewContentAndDoNotInvalidateCache ( fetchedContentForVaccine ) ;
10492 } ) ;
10593
10694 it ( "saves new vaccine content and does not invalidate cache if content has changed" , async ( ) => {
10795 const fetchedContentForVaccine = "some-content" ;
108-
109- ( fetchContentForVaccine as jest . Mock ) . mockResolvedValue ( fetchedContentForVaccine ) ;
110- ( vitaContentChangedSinceLastApproved as jest . Mock ) . mockReturnValue ( true ) ;
111-
112- await expect ( handler ( { } , context ) ) . resolves . toBeUndefined ( ) ;
113-
114- Object . values ( VaccineTypes ) . forEach ( ( vaccineType ) => {
115- expect ( writeContentForVaccine ) . toHaveBeenCalledWith ( vaccineType , fetchedContentForVaccine ) ;
116- expect ( invalidateCacheForVaccine ) . not . toHaveBeenCalledWith ( vaccineType ) ;
117- } ) ;
96+ await expectWhenContentFetchReturnsToSaveNewContentAndDoNotInvalidateCache ( fetchedContentForVaccine ) ;
11897 } ) ;
11998
12099 it ( "should only update one vaccine if vaccine name is set in inbound event" , async ( ) => {
121- const vaccineToUpdate = "rsv" ;
122- const fetchedContentForVaccine = "some-different-content" ;
123- ( fetchContentForVaccine as jest . Mock ) . mockResolvedValue ( fetchedContentForVaccine ) ;
124-
125- const event = { vaccineToUpdate : vaccineToUpdate } ;
126- await expect ( handler ( event , context ) ) . resolves . toBeUndefined ( ) ;
127-
128- expect ( writeContentForVaccine ) . toHaveBeenCalledTimes ( 1 ) ;
129- expect ( writeContentForVaccine ) . toHaveBeenCalledWith ( VaccineTypes . RSV , fetchedContentForVaccine ) ;
130- expect ( writeContentForVaccine ) . not . toHaveBeenCalledWith ( VaccineTypes . RSV_PREGNANCY , fetchedContentForVaccine ) ;
100+ await expectUpdateOnlyOneVaccineIfNameSetOnInboundEvent ( ) ;
131101 } ) ;
132102
133103 it ( "should throw if invalid vaccine name sent on inbound event" , async ( ) => {
134104 const vaccineToUpdate = "not-a-real-vaccine" ;
135105 const fetchedContentForVaccine = "some-different-content" ;
136- ( fetchContentForVaccine as jest . Mock ) . mockResolvedValue ( fetchedContentForVaccine ) ;
106+ mockFetchContentForVaccineWith ( fetchedContentForVaccine ) ;
137107
138108 const event = { vaccineToUpdate : vaccineToUpdate } ;
139109
@@ -147,69 +117,63 @@ describe("Lambda Handler", () => {
147117
148118 describe ( "when content-change-approval-needed feature enabled" , ( ) => {
149119 beforeEach ( ( ) => {
150- ( configProvider as jest . Mock ) . mockImplementation ( ( ) => ( {
151- CONTENT_CACHE_IS_CHANGE_APPROVAL_ENABLED : true ,
152- } ) ) ;
120+ mockConfigProviderWithChangeApprovalSetTo ( true ) ;
153121 } ) ;
154122
155123 it ( "overwrites invalidated cache with new updated content when forceUpdate is true in inbound event" , async ( ) => {
156- ( readCachedContentForVaccine as jest . Mock ) . mockResolvedValue ( mockInvalidatedCacheReadResult ) ;
124+ mockReadCachedContentForVaccineWith ( mockInvalidatedCacheReadResult ) ;
157125 const newContentFromContentAPI = "new-content" ;
158- ( fetchContentForVaccine as jest . Mock ) . mockResolvedValue ( newContentFromContentAPI ) ;
126+ mockFetchContentForVaccineWith ( newContentFromContentAPI ) ;
159127
160128 const event = { forceUpdate : true } ;
161- await expect ( handler ( event , context ) ) . resolves . toBeUndefined ( ) ;
129+ await expectHandlerToResolveSuccessfullyWithUndefinedResponse ( event , context ) ;
162130
163131 Object . values ( VaccineTypes ) . forEach ( ( vaccineType ) => {
164132 expect ( writeContentForVaccine ) . toHaveBeenCalledWith ( vaccineType , newContentFromContentAPI ) ;
165133 } ) ;
166134 } ) ;
167135
168136 it ( "does not overwrite invalidated cache with new content when forceUpdate is false in inbound event" , async ( ) => {
169- ( readCachedContentForVaccine as jest . Mock ) . mockResolvedValue ( mockInvalidatedCacheReadResult ) ;
137+ mockReadCachedContentForVaccineWith ( mockInvalidatedCacheReadResult ) ;
170138 const newContentFromContentAPI = "new-content" ;
171- ( fetchContentForVaccine as jest . Mock ) . mockResolvedValue ( newContentFromContentAPI ) ;
139+ mockFetchContentForVaccineWith ( newContentFromContentAPI ) ;
172140
173141 const event = { forceUpdate : false } ;
174- await expect ( handler ( event , context ) ) . resolves . toBeUndefined ( ) ;
142+ await expectHandlerToResolveSuccessfullyWithUndefinedResponse ( event , context ) ;
175143
176144 expect ( writeContentForVaccine ) . not . toHaveBeenCalled ( ) ;
177145 } ) ;
178146
179147 it ( "does not overwrite invalidated cache with new content when forceUpdate is not present in inbound event" , async ( ) => {
180- ( readCachedContentForVaccine as jest . Mock ) . mockResolvedValue ( mockInvalidatedCacheReadResult ) ;
148+ mockReadCachedContentForVaccineWith ( mockInvalidatedCacheReadResult ) ;
181149 const newContentFromContentAPI = "new-content" ;
182- ( fetchContentForVaccine as jest . Mock ) . mockResolvedValue ( newContentFromContentAPI ) ;
150+ mockFetchContentForVaccineWith ( newContentFromContentAPI ) ;
183151
184- const event = { } ;
185- await expect ( handler ( event , context ) ) . resolves . toBeUndefined ( ) ;
152+ await expectHandlerToResolveSuccessfullyWithUndefinedResponse ( { } , context ) ;
186153
187154 expect ( writeContentForVaccine ) . not . toHaveBeenCalled ( ) ;
188155 } ) ;
189156
190157 it ( "returns 200 when cache hydration is successful" , async ( ) => {
191158 ( vitaContentChangedSinceLastApproved as jest . Mock ) . mockReturnValue ( false ) ;
192159
193- await expect ( handler ( { } , context ) ) . resolves . toBeUndefined ( ) ;
160+ await expectHandlerToResolveSuccessfullyWithUndefinedResponse ( { } , context ) ;
194161 } ) ;
195162
196163 it ( "saves new vaccine content when cache was empty" , async ( ) => {
197- ( readCachedContentForVaccine as jest . Mock ) . mockResolvedValue ( mockEmptyCacheReadResult ) ;
198-
164+ mockReadCachedContentForVaccineWith ( mockEmptyCacheReadResult ) ;
199165 const fetchedContentForVaccine = "some-content" ;
200- ( fetchContentForVaccine as jest . Mock ) . mockResolvedValue ( fetchedContentForVaccine ) ;
166+ mockFetchContentForVaccineWith ( fetchedContentForVaccine ) ;
201167
202- await expect ( handler ( { } , context ) ) . resolves . toBeUndefined ( ) ;
168+ await expectHandlerToResolveSuccessfullyWithUndefinedResponse ( { } , context ) ;
203169
204- Object . values ( VaccineTypes ) . forEach ( ( vaccineType ) => {
205- expect ( writeContentForVaccine ) . toHaveBeenCalledWith ( vaccineType , fetchedContentForVaccine ) ;
206- } ) ;
170+ expectWriteContentToHaveBeenCalledForAllVaccinesWithFetchedContent ( fetchedContentForVaccine ) ;
207171 } ) ;
208172
209173 it ( "returns 200 and invalidates cache when content changes detected" , async ( ) => {
210174 ( vitaContentChangedSinceLastApproved as jest . Mock ) . mockReturnValue ( true ) ;
211175
212- await expect ( handler ( { } , context ) ) . resolves . toBeUndefined ( ) ;
176+ await expectHandlerToResolveSuccessfullyWithUndefinedResponse ( { } , context ) ;
213177
214178 Object . values ( VaccineTypes ) . forEach ( ( vaccineType ) => {
215179 expect ( invalidateCacheForVaccine ) . toHaveBeenCalledWith ( vaccineType ) ;
@@ -219,7 +183,7 @@ describe("Lambda Handler", () => {
219183 } ) ;
220184
221185 it ( "returns 200 when cached content had already been invalidated" , async ( ) => {
222- ( readCachedContentForVaccine as jest . Mock ) . mockResolvedValue ( mockInvalidatedCacheReadResult ) ;
186+ mockReadCachedContentForVaccineWith ( mockInvalidatedCacheReadResult ) ;
223187
224188 await expect ( handler ( { } , context ) ) . resolves . toBeUndefined ( ) ;
225189
@@ -229,27 +193,27 @@ describe("Lambda Handler", () => {
229193 it ( "returns 500 when cache hydration has failed due to fetching errors" , async ( ) => {
230194 ( fetchContentForVaccine as jest . Mock ) . mockRejectedValue ( new Error ( "test" ) ) ;
231195
232- await expect ( handler ( { } , context ) ) . rejects . toThrow ( ` ${ numberOfVaccines } failures` ) ;
196+ await expectHydrationToThrowFailureForAllVaccines ( ) ;
233197 } ) ;
234198
235199 it ( "returns 500 when cache hydration has failed due to filtering invalid content errors" , async ( ) => {
236200 ( getFilteredContentForVaccine as jest . Mock ) . mockImplementation ( ( ) => {
237201 throw new Error ( "test" ) ;
238202 } ) ;
239203
240- await expect ( handler ( { } , context ) ) . rejects . toThrow ( ` ${ numberOfVaccines } failures` ) ;
204+ await expectHydrationToThrowFailureForAllVaccines ( ) ;
241205 } ) ;
242206
243207 it ( "returns 500 when cache hydration has failed due to styling errors" , async ( ) => {
244208 ( getStyledContentForVaccine as jest . Mock ) . mockRejectedValue ( new Error ( "test" ) ) ;
245209
246- await expect ( handler ( { } , context ) ) . rejects . toThrow ( ` ${ numberOfVaccines } failures` ) ;
210+ await expectHydrationToThrowFailureForAllVaccines ( ) ;
247211 } ) ;
248212
249213 it ( "returns 500 when cache hydration has failed due to writing errors" , async ( ) => {
250214 ( writeContentForVaccine as jest . Mock ) . mockRejectedValue ( new Error ( "test" ) ) ;
251215
252- await expect ( handler ( { } , context ) ) . rejects . toThrow ( ` ${ numberOfVaccines } failures` ) ;
216+ await expectHydrationToThrowFailureForAllVaccines ( ) ;
253217 } ) ;
254218
255219 it ( "stores requestID as traceid in asyncLocalStorage context" , async ( ) => {
@@ -262,16 +226,65 @@ describe("Lambda Handler", () => {
262226 } ) ;
263227
264228 it ( "should only update one vaccine if vaccine name is set in inbound event" , async ( ) => {
265- const vaccineToUpdate = "rsv" ;
266- const fetchedContentForVaccine = "some-different-content" ;
267- ( fetchContentForVaccine as jest . Mock ) . mockResolvedValue ( fetchedContentForVaccine ) ;
229+ await expectUpdateOnlyOneVaccineIfNameSetOnInboundEvent ( ) ;
230+ } ) ;
231+ } ) ;
268232
269- const event = { vaccineToUpdate : vaccineToUpdate } ;
270- await expect ( handler ( event , context ) ) . resolves . toBeUndefined ( ) ;
233+ const mockConfigProviderWithChangeApprovalSetTo = ( changeApprovalEnabled : boolean ) => {
234+ ( configProvider as jest . Mock ) . mockImplementation ( ( ) => ( {
235+ CONTENT_CACHE_IS_CHANGE_APPROVAL_ENABLED : changeApprovalEnabled ,
236+ } ) ) ;
237+ } ;
271238
272- expect ( writeContentForVaccine ) . toHaveBeenCalledTimes ( 1 ) ;
273- expect ( writeContentForVaccine ) . toHaveBeenCalledWith ( VaccineTypes . RSV , fetchedContentForVaccine ) ;
274- expect ( writeContentForVaccine ) . not . toHaveBeenCalledWith ( VaccineTypes . RSV_PREGNANCY , fetchedContentForVaccine ) ;
239+ const mockReadCachedContentForVaccineWith = ( mockInvalidatedCacheReadResult : ReadCachedContentResult ) => {
240+ ( readCachedContentForVaccine as jest . Mock ) . mockResolvedValue ( mockInvalidatedCacheReadResult ) ;
241+ } ;
242+
243+ const mockFetchContentForVaccineWith = ( fetchContentResult : string ) => {
244+ ( fetchContentForVaccine as jest . Mock ) . mockResolvedValue ( fetchContentResult ) ;
245+ } ;
246+
247+ const expectUpdateOnlyOneVaccineIfNameSetOnInboundEvent = async ( ) => {
248+ const vaccineToUpdate = "rsv" ;
249+ const fetchedContentForVaccine = "some-different-content" ;
250+ mockFetchContentForVaccineWith ( fetchedContentForVaccine ) ;
251+
252+ const event = { vaccineToUpdate : vaccineToUpdate } ;
253+ await expect ( handler ( event , context ) ) . resolves . toBeUndefined ( ) ;
254+
255+ expect ( writeContentForVaccine ) . toHaveBeenCalledTimes ( 1 ) ;
256+ expect ( writeContentForVaccine ) . toHaveBeenCalledWith ( VaccineTypes . RSV , fetchedContentForVaccine ) ;
257+ expect ( writeContentForVaccine ) . not . toHaveBeenCalledWith ( VaccineTypes . RSV_PREGNANCY , fetchedContentForVaccine ) ;
258+ } ;
259+
260+ const expectHydrationToThrowFailureForAllVaccines = async ( ) => {
261+ await expect ( handler ( { } , context ) ) . rejects . toThrow ( `${ numberOfVaccines } failures` ) ;
262+ } ;
263+
264+ const expectWhenContentFetchReturnsToSaveNewContentAndDoNotInvalidateCache = async (
265+ fetchedContentForVaccine : string ,
266+ ) => {
267+ mockFetchContentForVaccineWith ( fetchedContentForVaccine ) ;
268+ ( vitaContentChangedSinceLastApproved as jest . Mock ) . mockReturnValue ( true ) ;
269+
270+ await expectHandlerToResolveSuccessfullyWithUndefinedResponse ( { } , context ) ;
271+
272+ Object . values ( VaccineTypes ) . forEach ( ( vaccineType ) => {
273+ expect ( writeContentForVaccine ) . toHaveBeenCalledWith ( vaccineType , fetchedContentForVaccine ) ;
274+ expect ( invalidateCacheForVaccine ) . not . toHaveBeenCalledWith ( vaccineType ) ;
275275 } ) ;
276- } ) ;
276+ } ;
277+
278+ const expectHandlerToResolveSuccessfullyWithUndefinedResponse = async (
279+ lambdaTriggerEvent : object ,
280+ context : Context ,
281+ ) => {
282+ await expect ( handler ( lambdaTriggerEvent , context ) ) . resolves . toBeUndefined ( ) ;
283+ } ;
284+
285+ const expectWriteContentToHaveBeenCalledForAllVaccinesWithFetchedContent = ( fetchedContentForVaccine : string ) => {
286+ Object . values ( VaccineTypes ) . forEach ( ( vaccineType ) => {
287+ expect ( writeContentForVaccine ) . toHaveBeenCalledWith ( vaccineType , fetchedContentForVaccine ) ;
288+ } ) ;
289+ } ;
277290} ) ;
0 commit comments