Skip to content

Commit 9c88dd4

Browse files
committed
test(playwright): refactor page.evaluate into page.request.<METHOD>
1 parent 27e6de3 commit 9c88dd4

File tree

1 file changed

+39
-36
lines changed

1 file changed

+39
-36
lines changed

tests/project-bootstrap.browser.ts

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,43 +17,46 @@ test("Project bootstrap works", async ({ page, baseURL }) => {
1717
// This needs to come before the unit fetch request below so there isn't a cors issue
1818
await page.goto(baseURL ?? "/");
1919

20+
expect(baseURL).toBeDefined();
21+
22+
const url = new URL(baseURL as string);
23+
url.pathname = "/api/as-api/organisation/default";
24+
const defaultOrg: OrganisationGetDefaultResponse = await (
25+
await page.request.get(url.href)
26+
).json();
27+
2028
// Ensure default unit and associated projects and products doesn't exist
21-
await page.evaluate(
22-
async ({ baseURL, username }) => {
23-
// This need to be a pure function
24-
const defaultOrgRes = await fetch(baseURL + "/api/as-api/organisation/default");
25-
const defaultOrg: OrganisationGetDefaultResponse = await defaultOrgRes.json();
26-
const orgRes = await fetch(baseURL + `/api/as-api/organisation/${defaultOrg.id}/unit`);
27-
const units = ((await orgRes.json()) as OrganisationUnitsGetResponse).units;
28-
29-
const personalUnit = units.find((unit) => unit.name === username);
30-
31-
if (personalUnit) {
32-
const productRes = await fetch(baseURL + "/api/as-api/product");
33-
const products = ((await productRes.json()) as ProductsGetResponse).products;
34-
35-
const productsToDelete = products
36-
.filter((product) => product.unit.id === personalUnit.id)
37-
.filter(
38-
(product): product is ProductDmProjectTier =>
39-
product.product.type === "DATA_MANAGER_PROJECT_TIER_SUBSCRIPTION",
40-
);
41-
const productPromises = productsToDelete.map(async (product) => {
42-
await fetch(baseURL + `/api/dm-api/project/${product.claim?.id}`, { method: "DELETE" });
43-
await fetch(baseURL + `/api/as-api/product/${product.product.id}`, { method: "DELETE" });
44-
});
45-
await Promise.allSettled(productPromises);
46-
47-
const res = await fetch(baseURL + "/api/as-api/unit", { method: "DELETE" });
48-
const response = await res.json();
49-
if (!res.ok && response.error !== "The Unit does not exist") {
50-
console.log(response.error);
51-
throw Error("An existing unit could not be cleaned up before running the test");
52-
}
53-
}
54-
},
55-
{ baseURL, username: process.env.PW_USERNAME },
56-
);
29+
url.pathname = `/api/as-api/organisation/${defaultOrg.id}/unit`;
30+
const units = ((await (await page.request.get(url.href)).json()) as OrganisationUnitsGetResponse)
31+
.units;
32+
33+
const personalUnit = units.find((unit) => unit.name === process.env.PW_USERNAME);
34+
35+
if (personalUnit) {
36+
url.pathname = "/api/as-api/product";
37+
const products = ((await (await page.request.get(url.href)).json()) as ProductsGetResponse)
38+
.products;
39+
40+
const productsToDelete = products
41+
.filter((product) => product.unit.id === personalUnit.id)
42+
.filter(
43+
(product): product is ProductDmProjectTier =>
44+
product.product.type === "DATA_MANAGER_PROJECT_TIER_SUBSCRIPTION",
45+
);
46+
const productPromises = productsToDelete.map(async (product) => {
47+
url.pathname = `/api/dm-api/project/${product.claim?.id}`;
48+
page.request.delete(url.href);
49+
url.pathname = `/api/as-api/product/${product.product.id}`;
50+
page.request.delete(url.href);
51+
});
52+
await Promise.allSettled(productPromises);
53+
54+
url.pathname = "/api/as-api/unit";
55+
const res = await page.request.delete(url.href);
56+
57+
const response = await res.json();
58+
expect(!res.ok() && response.error !== "The Unit does not exist").toBeFalsy();
59+
}
5760

5861
//
5962
// The Test

0 commit comments

Comments
 (0)