Skip to content

Commit 13137d5

Browse files
author
Bob Fanger
committed
test: Added E2E test for hooks
1 parent ede3680 commit 13137d5

File tree

4 files changed

+306
-267
lines changed

4 files changed

+306
-267
lines changed

playwright/tests/hooks.spec.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { test, expect } from "@playwright/test";
2+
3+
test.use({ viewport: { width: 480, height: 360 } });
4+
test.describe("hooks", () => {
5+
test("counter", async ({ page }) => {
6+
await page.goto("/hooks");
7+
await expect(page.locator('[data-testid="count"]')).toHaveText("0");
8+
await page.locator('[data-testid="add"]').click();
9+
await expect(page.locator('[data-testid="count"]')).toHaveText("1");
10+
await page.locator('[data-testid="add"]').click();
11+
await expect(page.locator('[data-testid="count"]')).toHaveText("2");
12+
});
13+
14+
test("authentication", async ({ page }) => {
15+
await page.goto("/hooks", { waitUntil: "networkidle" });
16+
await expect(
17+
page.locator('[data-testid="not-authenticated"]')
18+
).toBeVisible();
19+
await expect(
20+
page.locator('[data-testid="authenticated"]')
21+
).not.toBeVisible();
22+
await page.locator('[data-testid="login"]').click();
23+
await expect(page.locator('[data-testid="authenticated"]')).toBeVisible();
24+
await page.locator('[data-testid="logout"]').click();
25+
await expect(
26+
page.locator('[data-testid="not-authenticated"]')
27+
).toBeVisible();
28+
});
29+
});

src/routes/hooks/+page.svelte

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@
2222
{#if $countHook}
2323
{@const [count, setCount] = $countHook}
2424

25-
<div>Count: {count}</div>
26-
<button on:click={() => setCount(count + 1)}>+</button>
25+
<div>Count: <span data-testid="count">{count}</span></div>
26+
<button data-testid="add" on:click={() => setCount(count + 1)}>+</button>
2727
<hr />
2828
{/if}
2929
<react:AuthProvider value={auth}>
3030
<Nested />
3131
</react:AuthProvider>
3232

3333
{#if auth.authenticated}
34-
<button on:click={onLogout}>Logout</button>
34+
<button on:click={onLogout} data-testid="logout">Logout</button>
3535
{:else}
36-
<button on:click={onLogin}>Login</button>
36+
<button on:click={onLogin} data-testid="login">Login</button>
3737
{/if}

src/routes/hooks/HookWithContext.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</script>
77

88
{#if $auth?.authenticated}
9-
<div>Authenticated</div>
9+
<div data-testid="authenticated">Authenticated</div>
1010
{:else}
11-
<div>Not authenticated</div>
11+
<div data-testid="not-authenticated">Not authenticated</div>
1212
{/if}

0 commit comments

Comments
 (0)