Skip to content

Commit b86f8a3

Browse files
committed
test: fix expect assertPage util
1 parent 6a26458 commit b86f8a3

File tree

1 file changed

+40
-31
lines changed

1 file changed

+40
-31
lines changed

starters/e2e/qwikrouter/util.ts

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,26 @@ export async function assertPage(ctx: TestContext, test: AssertPage) {
66
const page = getPage(ctx);
77
const pageUrl = new URL(page.url());
88
const html = page.locator("html");
9-
10-
expect(await html.getAttribute("q:version")).toBeDefined();
11-
expect(await html.getAttribute("q:base")).toBeDefined();
12-
expect(await html.getAttribute("q:id")).toBeDefined();
13-
149
const head = html.locator("head");
15-
expect(await head.getAttribute("q:id")).toBeDefined();
1610

1711
if (test.pathname) {
18-
expect(pageUrl.pathname).toBe(test.pathname);
12+
await expect(html).toHaveAttribute("q:version");
13+
await expect(html).toHaveAttribute("q:base");
14+
15+
// Build expected URL with or without search params
16+
let expectedHref = pageUrl.origin + test.pathname;
17+
if (test.searchParams && test.searchParams !== "empty") {
18+
const searchParams = new URLSearchParams();
19+
for (const [key, value] of Object.entries(test.searchParams)) {
20+
searchParams.append(key, value);
21+
}
22+
expectedHref += "?" + searchParams.toString();
23+
}
24+
25+
await expect(page).toHaveURL(expectedHref);
1926

2027
const canonical = head.locator('link[rel="canonical"]');
21-
const href = await canonical.getAttribute("href");
22-
const canonicalUrl = new URL(href!);
23-
expect(canonicalUrl.pathname).toBe(test.pathname);
28+
await expect(canonical).toHaveAttribute("href", expectedHref);
2429
}
2530

2631
if (test.searchParams) {
@@ -35,52 +40,58 @@ export async function assertPage(ctx: TestContext, test: AssertPage) {
3540

3641
if (test.title) {
3742
const title = head.locator("title");
38-
expect(await title.innerText()).toBe(test.title);
43+
await expect(title).toHaveText(test.title, {
44+
useInnerText: true,
45+
});
3946
}
4047

4148
let parentLocator = page.locator("body");
4249
if (test.layoutHierarchy) {
4350
for (const layoutName of test.layoutHierarchy) {
4451
const selector = `[data-test-layout="${layoutName}"]`;
4552
parentLocator = parentLocator.locator(selector);
46-
expect(
47-
await parentLocator.isVisible(),
53+
await expect(
54+
parentLocator,
4855
`Incorrect layout hierarchy, did not find "${selector}", pathname: ${pageUrl.pathname}`,
49-
).toBe(true);
56+
).toBeVisible();
5057
}
5158

5259
const noFindChildLayout = parentLocator.locator(`[data-test-layout]`);
5360
if (await noFindChildLayout.isVisible()) {
5461
const layoutName =
5562
await noFindChildLayout.getAttribute("data-test-layout")!;
56-
expect(
57-
layoutName,
63+
64+
await expect(
65+
noFindChildLayout,
5866
`Should not be another nested layout, but found [data-test-layout="${layoutName}"], pathname: ${pageUrl.pathname}`,
59-
).toBe(null);
67+
).not.toHaveAttribute("data-test-layout");
6068
}
6169
}
6270

6371
if (test.h1) {
6472
const h1 = parentLocator.locator("h1");
65-
expect(await h1.innerText()).toBe(test.h1);
73+
await expect(h1).toHaveText(test.h1, {
74+
useInnerText: true,
75+
});
6676
}
6777

6878
const activeLink = locator(
6979
ctx,
7080
`header [data-test-header-links] a[class="active"]`,
7181
);
7282
if (typeof test.activeHeaderLink === "string") {
73-
if (await activeLink.isVisible()) {
74-
expect(await activeLink.innerText()).toBe(test.activeHeaderLink);
75-
} else {
76-
expect(true, `Header link "${test.activeHeaderLink}" not active`).toBe(
77-
false,
78-
);
79-
}
83+
await expect(
84+
activeLink,
85+
`Header link "${test.activeHeaderLink}" not active`,
86+
).toBeVisible();
87+
await expect(activeLink).toHaveText(test.activeHeaderLink, {
88+
useInnerText: true,
89+
});
8090
} else if (test.activeHeaderLink === false) {
81-
if (await activeLink.isVisible()) {
82-
expect(true, `There should not be an active header link`).toBe(false);
83-
}
91+
await expect(
92+
activeLink,
93+
`There should not be an active header link`,
94+
).not.toBeVisible();
8495
}
8596
}
8697

@@ -101,9 +112,7 @@ export async function linkNavigate(
101112
const page = getPage(ctx);
102113
const link = page.locator(linkSelector);
103114

104-
if (!(await link.isVisible())) {
105-
expect(true, `Link selector ${linkSelector} not found`).toBe(false);
106-
}
115+
await expect(link, `Link selector ${linkSelector} not found`).toBeVisible();
107116

108117
const href = (await link.getAttribute("href"))!;
109118
console.log(` nav> ${href}`);

0 commit comments

Comments
 (0)