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

app.spec.ts

60 lines1.9 KB

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