Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

app.spec.ts

67 lines2.3 KB

1import { expect, test } from '@playwright/test'
2import type { Page } from '@playwright/test'
3​
4async function waitForHydration(page: Page) {
5 await page.waitForFunction(() => typeof (window as any).$_TSR === 'undefined')
6}
7​
8async function signup(page: Page, email: string, password: string) {
9 await page.goto('/signup')
10 await waitForHydration(page)
11 await page.fill('input[name="email"]', email)
12 await page.fill('input[name="password"]', password)
13 await page.click('button[type="submit"]')
14 await page.waitForSelector('text=Logout')
16​
17async function login(
18 page: Page,
19 email: string,
20 password: string,
21 signupOnFail = false,
22) {
23 await page.goto('/login')
24 await waitForHydration(page)
25 await page.fill('input[name="email"]', email)
26 await page.fill('input[name="password"]', password)
27 await page.click('button[type="submit"]')
28​
29 if (signupOnFail) {
30 await page.waitForSelector('text=User not found')
31 await page.click('button:has-text("Sign up instead?")')
32 await page.waitForSelector('text=Logout')
33 }
35​
36test('Posts redirects to login when not authenticated', async ({ page }) => {
37 await page.goto('/posts')
38 await expect(page.locator('h1')).toContainText('Login')
39})
40​
41test('Login fails with user not found', async ({ page }) => {
42 const email = `missing-${Date.now()}@gmail.com`
43 await login(page, email, 'badpassword')
44 await expect(page.getByText('User not found')).toBeVisible()
45})
46​
47test('Login fails with incorrect password', async ({ page }) => {
48 const email = `incorrect-password-${Date.now()}@gmail.com`
49 await signup(page, email, 'test')
50 await page.goto('/logout')
51 await login(page, email, 'badpassword')
52 await expect(page.getByText('Incorrect password')).toBeVisible()
53})
54​
55test('Can sign up from a not found user', async ({ page }) => {
56 const email = `new-${Date.now()}@gmail.com`
57 await login(page, email, 'badpassword', true)
58 await expect(page.getByText(email)).toBeVisible()
59})
60​
61test('Navigating to post after logging in', async ({ page }) => {
62 const email = `posts-${Date.now()}@gmail.com`
63 await signup(page, email, 'test')
64 await page.getByRole('link', { name: 'Posts' }).click()
65 await page.getByRole('link', { name: 'sunt aut facere repe' }).click()
66 await expect(page.getByRole('heading')).toContainText('sunt aut facere')
67})