Skip to content

Commit 2ea735b

Browse files
committed
Added some basic login tests that actually works
1 parent 3d1e8c9 commit 2ea735b

5 files changed

Lines changed: 35 additions & 7 deletions

File tree

.github/workflows/playwright.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
name: Playwright Tests
22
on:
33
push:
4-
branches: [ main, master ]
4+
branches: [ main ]
55
pull_request:
6-
branches: [ main, master ]
6+
branches: [ main ]
77
jobs:
88
test:
99
timeout-minutes: 60

app/components/LoginForm.vue

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<template>
2-
<UCard variant="subtle" data-testid="login_card">
2+
<UCard variant="subtle">
33
<UForm :schema="schema" :state="state" class="space-y-4" @submit="onSubmit">
44
<UFormField name="username" v-if="!isAnyUserSelected">
55
<UInput
6+
data-testid="username"
67
placeholder="Who?"
78
type="text"
89
v-model="state.username"
@@ -32,6 +33,7 @@
3233

3334
<UFormField name="password">
3435
<UInput
36+
data-testid="password"
3537
placeholder="Password"
3638
v-model="state.password"
3739
type="password"
@@ -40,6 +42,7 @@
4042
</UFormField>
4143
<div>
4244
<UButton
45+
data-testid="login_button"
4346
type="submit"
4447
icon="i-lucide-log-in"
4548
size="md"

stores/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export const useStore = defineStore('main', () => {
4040
await clearSession()
4141
}
4242

43-
const useRememberedUsers = () =>
43+
const useRememberedUsers = (): Ref<RememberedUser[]> =>
4444
useLocalStorage<RememberedUser[]>('rememberedUsers', [])
4545

4646
return {

test/playwright/login.test.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,38 @@
11
import { expect, test } from '@nuxt/test-utils/playwright'
2-
import { resetDb, createAuthTestData } from '../utils/index'
2+
import { resetDb, createAuthTestData, TEST_PARENT_USER } from '../utils/index'
3+
4+
const TIMEOUT = 10000 // 10 seconds
35

46
test.describe('Login page', () => {
57
test.beforeEach(async ({ page, goto }) => {
8+
page.setDefaultNavigationTimeout(TIMEOUT)
9+
page.setDefaultTimeout(TIMEOUT)
610
await resetDb()
711
await createAuthTestData()
812
})
913

10-
test('displays the login card', async ({ page, goto }) => {
14+
test('login form is shown', async ({ page, goto }) => {
15+
await goto('/', { waitUntil: 'hydration' })
16+
await expect(page.getByTestId('username')).toBeVisible()
17+
await expect(page.getByTestId('password')).toBeVisible()
18+
await expect(page.getByTestId('login_button')).toBeVisible()
19+
})
20+
21+
test('login with valid user credentials', async ({ page, goto }) => {
22+
await goto('/', { waitUntil: 'hydration' })
23+
await page.getByTestId('username').fill(TEST_PARENT_USER.username)
24+
await page.getByTestId('password').fill(TEST_PARENT_USER.password)
25+
await page.getByTestId('login_button').click()
26+
await page.waitForURL('**/dashboard')
27+
expect(page.url()).toMatch(/\/dashboard$/)
28+
})
29+
30+
test('login with invalid user credentials', async ({ page, goto }) => {
1131
await goto('/', { waitUntil: 'hydration' })
12-
await expect(page.getByTestId('login_card')).toBeVisible()
32+
await page.getByTestId('username').fill(TEST_PARENT_USER.username)
33+
await page.getByTestId('password').fill('wrongpassword')
34+
await page.getByTestId('login_button').click()
35+
await page.waitForSelector('li[role="alert"]') // wait for toast to pop up
36+
expect(page.url()).toMatch(/\/login$/)
1337
})
1438
})

test/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ export async function setTestUserPoints(username: string, points: number) {
9090
username,
9191
])
9292
}
93+
9394
export async function getSessionCookie(username: string, password: string) {
9495
const response = await fetch('/api/auth/login', {
9596
method: 'POST',

0 commit comments

Comments
 (0)