Skip to content

Commit e7e6ecd

Browse files
committed
refactor: replace URL expectation with waitForURL in authTest function
1 parent 1c751b2 commit e7e6ecd

2 files changed

Lines changed: 18 additions & 13 deletions

File tree

client/tests/helpers/mocks.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,30 @@ export default async function mockRequests(
44
page: Page,
55
overrides?: Record<string, any>
66
) {
7-
const mocks = {
7+
const mocks: Record<string, any> = {
88
...MOCKS,
99
...overrides,
1010
};
1111

12-
Object.entries(mocks).forEach(([path, response]) => {
13-
const handler = typeof response === "function" ? response : () => response;
12+
await page.route("**/api/**", (route) => {
13+
const url = route.request().url();
14+
const rawPath = url.split("/api/")[1] || "";
15+
const normalized = rawPath.split("?")[0].replace(/\/$/, "").toLowerCase();
1416

15-
page.route(`**/api/${path}**`, (route) => {
16-
const mockResponse = handler();
17-
18-
route.fulfill({
17+
const key = Object.keys(mocks).find((k) => k.toLowerCase() === normalized);
18+
if (key) {
19+
const response = mocks[key];
20+
const handler =
21+
typeof response === "function" ? response : () => response;
22+
return route.fulfill({
1923
status: 200,
2024
contentType: "application/json",
21-
body: JSON.stringify(mockResponse),
25+
body: JSON.stringify(handler()),
2226
});
23-
});
27+
}
28+
29+
console.warn("[mocks] no mock for /api/" + normalized);
30+
return route.continue();
2431
});
2532
}
2633

@@ -43,6 +50,7 @@ const MOCKS = {
4350
emailConfirmed: true,
4451
},
4552
},
53+
"accounts/login": makeLoginResponse(),
4654
"accounts/getProfile": {
4755
isSuccess: true,
4856
user: {
@@ -62,7 +70,6 @@ const MOCKS = {
6270
features: [],
6371
},
6472
},
65-
"accounts/login": makeLoginResponse(),
6673
"accounts/resendConfirmationEmail": {
6774
isSuccess: true,
6875
code: "REG_SUCCESS",

client/tests/helpers/test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ function authTest(page: Page) {
3838
await page.locator('input[type="password"]').fill("Brave1234!");
3939
await page.getByText("Sign In").click();
4040

41-
await expect(page).toHaveURL(
42-
"http://localhost:3000/admin/verificationAdmin"
43-
);
41+
await page.waitForURL(/\/admin\/verificationAdmin$/, { timeout: 10000 });
4442
await expect(
4543
page.getByRole("heading", { name: "Verification Administration" })
4644
).toBeVisible();

0 commit comments

Comments
 (0)