@@ -11,6 +11,7 @@ import {
1111 pollUpsertLetterLogForError ,
1212} from "tests/helpers/aws-cloudwatch-helper" ;
1313import { supplierDataSetup } from "tests/helpers/suppliers-setup-helper" ;
14+ import { pollForLettersInDb } from "tests/helpers/poll-for-letters-helper" ;
1415
1516let baseUrl : string ;
1617
@@ -27,7 +28,6 @@ test.describe("Event Subscription SNS Tests", () => {
2728 logger . info ( `Testing event subscription with domainId: ${ domainId } ` ) ;
2829 const preparedEvent = createPreparedV1Event ( { domainId } ) ;
2930 const response = await sendSnsEvent ( preparedEvent ) ;
30- const RETRY_DELAY_MS = 30_000 ;
3131
3232 expect ( response . MessageId ) . toBeTruthy ( ) ;
3333
@@ -48,40 +48,14 @@ test.describe("Event Subscription SNS Tests", () => {
4848 // check if supplier exists in suppliers table
4949 await supplierDataSetup ( supplierId ) ;
5050
51- const headers = createValidRequestHeaders ( supplierId ) ;
52- let statusCode = 0 ;
53- let letterStatus : string | undefined ;
54-
55- for ( let attempt = 1 ; attempt <= 3 ; attempt ++ ) {
56- const getLetterResponse = await request . get (
57- `${ baseUrl } /${ SUPPLIER_LETTERS } /${ domainId } ` ,
58- {
59- headers,
60- } ,
61- ) ;
62-
63- statusCode = getLetterResponse . status ( ) ;
64- const responseBody = ( await getLetterResponse . json ( ) ) as {
65- data ?: { attributes ?: { status ?: string } } ;
66- } ;
67- letterStatus = responseBody . data ?. attributes ?. status ;
68-
69- if ( statusCode === 200 && letterStatus === "PENDING" ) {
70- logger . info (
71- `Attempt ${ attempt } : Received status code ${ statusCode } for domainId: ${ domainId } ` ,
72- ) ;
73- break ;
74- }
75-
76- if ( attempt < 3 ) {
77- logger . info (
78- `Attempt ${ attempt } : Received status code ${ statusCode } for domainId: ${ domainId } . Retrying after ${ RETRY_DELAY_MS / 1000 } seconds...` ,
79- ) ;
80- await new Promise ( ( resolve ) => {
81- setTimeout ( resolve , RETRY_DELAY_MS ) ; // Wait for 30 seconds before the next attempt
82- } ) ;
83- }
84- }
51+ // poll for letter to be inserted in db with status PENDING
52+ const { letterStatus, statusCode } = await pollForLettersInDb (
53+ request ,
54+ supplierId ,
55+ domainId ,
56+ baseUrl ,
57+ ) ;
58+
8559 expect ( statusCode ) . toBe ( 200 ) ;
8660 expect ( letterStatus ) . toBe ( "PENDING" ) ;
8761 } ) ;
0 commit comments