Skip to content

Commit bfb41a4

Browse files
Add wait to patch tests
1 parent f7b9ab9 commit bfb41a4

2 files changed

Lines changed: 60 additions & 1 deletion

File tree

tests/component-tests/apiGateway-tests/update-letter-status.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
import {
1212
createTestData,
1313
getLettersBySupplier,
14+
waitForLetterStatus,
1415
} from "../../helpers/generate-fetch-test-data";
1516
import { createInvalidRequestHeaders } from "../../constants/request-headers";
1617
import {
@@ -53,6 +54,13 @@ test.describe("API Gateway Tests to Verify Patch Status Endpoint", () => {
5354
);
5455

5556
expect(response.status()).toBe(202);
57+
58+
const updated = await waitForLetterStatus(
59+
SUPPLIERID,
60+
letter.id,
61+
"ACCEPTED",
62+
);
63+
expect(updated.status).toBe("ACCEPTED");
5664
});
5765

5866
test(`Patch /letters returns 202 and status is updated to REJECTED`, async ({
@@ -71,6 +79,13 @@ test.describe("API Gateway Tests to Verify Patch Status Endpoint", () => {
7179
);
7280

7381
expect(response.status()).toBe(202);
82+
83+
const updated = await waitForLetterStatus(
84+
SUPPLIERID,
85+
letter.id,
86+
"REJECTED",
87+
);
88+
expect(updated.status).toBe("REJECTED");
7489
});
7590

7691
test(`Patch /letters returns 400 if request Body is invalid`, async ({

tests/helpers/generate-fetch-test-data.ts

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
22
import {
33
DeleteCommand,
44
DynamoDBDocumentClient,
5+
GetCommand,
56
QueryCommand,
67
} from "@aws-sdk/lib-dynamodb";
78
import {
89
LETTERSTABLENAME,
9-
SUPPLIERID,
1010
SUPPLIERTABLENAME,
1111
envName,
1212
} from "../constants/api-constants";
@@ -74,6 +74,50 @@ export const getLettersBySupplier = async (
7474
return Items as SupplierApiLetters[];
7575
};
7676

77+
const delay = (ms: number) =>
78+
new Promise((resolve) => {
79+
setTimeout(resolve, ms);
80+
});
81+
82+
export async function waitForLetterStatus(
83+
supplierId: string,
84+
id: string,
85+
status: string,
86+
options?: {
87+
timeoutMs?: number;
88+
intervalMs?: number;
89+
},
90+
): Promise<SupplierApiLetters> {
91+
const timeoutMs = options?.timeoutMs ?? 60_000;
92+
const intervalMs = options?.intervalMs ?? 5000;
93+
const startedAt = Date.now();
94+
95+
while (Date.now() - startedAt < timeoutMs) {
96+
const { Item } = await docClient.send(
97+
new GetCommand({
98+
TableName: LETTERSTABLENAME,
99+
Key: { id, supplierId },
100+
ProjectionExpression: "id, #status, supplierId",
101+
ExpressionAttributeNames: {
102+
"#status": "status",
103+
},
104+
}),
105+
);
106+
107+
const letter = Item as SupplierApiLetters | undefined;
108+
109+
if (letter && letter.status === status) {
110+
return letter;
111+
}
112+
113+
await delay(intervalMs);
114+
}
115+
116+
throw new Error(
117+
`Timed out waiting for letter ${id} to reach status ${status} for supplier ${supplierId}.`,
118+
);
119+
}
120+
77121
export const deleteLettersBySupplier = async (
78122
supplierId: string,
79123
id: string,

0 commit comments

Comments
 (0)