Skip to content

Commit

Permalink
test(auth): fix e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pedraal committed Feb 13, 2024
1 parent 9fecd15 commit dc303f9
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 13 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"lint:ci": "eslint --output-file eslint_report.json --format json ."
},
"dependencies": {
"@playwright/test": "^1.41.2",
"@vueuse/nuxt": "^10.7.2",
"bcryptjs": "^2.4.3",
"jsonwebtoken": "^9.0.2",
Expand Down
3 changes: 2 additions & 1 deletion pages/#.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<script lang="ts" setup>
import { z } from 'zod'
import { userDTO } from '~/db/dto/user.dto'
definePageMeta({
Expand All @@ -21,7 +22,7 @@ const formErrors = reactive({
function validate() {
const userDTOWithPasswordConfirmation = userDTO.extend({
passwordConfirmation: userDTO.shape.password.refine((confirmation) => {
passwordConfirmation: z.string().refine((confirmation) => {
return confirmation === formData.password
}, { message: 'Passwords don\'t match' }),
})
Expand Down
30 changes: 29 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 20 additions & 10 deletions tests/e2e/auth.e2e.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { afterAll, afterEach, beforeEach, describe, expect, it } from 'vitest'
import { expect as expectE2E } from '@playwright/test'
import { createPage, setup } from '@nuxt/test-utils/e2e'
import { consola } from 'consola'
import { testNuxtConfig } from '../utils/nuxt_config'
import { MemoryDb } from '../utils/db'
import { UserRepository } from '~/db/repositories/user.repo'
import { DbClient } from '~/db/client'

const db = new MemoryDb()

Expand All @@ -12,7 +14,7 @@ beforeEach(() => {
})

afterEach(async () => {
await db.clear()
await DbClient.db.dropDatabase()
})

afterAll(async () => {
Expand All @@ -23,7 +25,7 @@ await setup({
nuxtConfig: await testNuxtConfig({ db }),
})

describe('auth', () => {
describe('auth', async () => {
describe('login', () => {
it('should show error if invalid', async () => {
const page = await createPage('/')
Expand All @@ -33,10 +35,10 @@ describe('auth', () => {
await page.getByLabel('Username').type('test')
await page.getByLabel('Password').type('test')
await page.getByTestId('submit').click()
expect(page.getByText('Invalid username or password')).toBeDefined()
await expectE2E(page.getByText('Invalid username or password')).toBeVisible()
})

it('should redirect if valid', async () => {
it('should auth and navigate to chats if valid', async () => {
const repo = new UserRepository()
await repo.create({ username: 'test', password: 'test' })
const page = await createPage('/')
Expand All @@ -47,27 +49,35 @@ describe('auth', () => {
await page.getByLabel('Password').type('test')
await page.getByTestId('submit').click()
await page.waitForURL('**/chats')
await expect(page.locator('h1').textContent()).resolves.toContain('Its time to join or create a chat room !')
await expectE2E(page.getByText('Its time to join or create a chat room !')).toBeVisible()
})
})

describe('#', () => {
it('should show error if invalid', async () => {
const repo = new UserRepository()
await repo.create({ username: 'test', password: 'test' })
const page = await createPage('/')
await page.getByTestId('login-link').click()
await page.getByTestId('#-link').click()
await page.waitForURL('**/#')
await expect(page.locator('h1').textContent()).resolves.toContain('#')
await page.getByLabel('Username').type('t')
await page.getByLabel('Password', { exact: true }).type('test')
await page.getByLabel('Password', { exact: true }).type('t')
await page.getByTestId('submit').click()
expect(page.getByText('Passwords don\'t match')).toBeDefined()
await expectE2E(page.getByText('Passwords don\'t match')).toBeVisible()
await expect(page.getByText('Must be longer than 3 characters').count()).resolves.toBe(2)
await page.getByLabel('Username').clear()
await page.getByLabel('Username').type('test')
await page.getByLabel('Password', { exact: true }).clear()
await page.getByLabel('Password', { exact: true }).type('test')
await page.getByLabel('Password confirmation').clear()
await page.getByLabel('Password confirmation').type('test')
await page.getByTestId('submit').click()
expect(page.getByText('Must be longer than 3 characters')).toBeDefined()
await expectE2E(page.getByText('Username already taken')).toBeVisible()
})

it('should redirect if valid', async () => {
it('should auth and navigate to chats if valid', async () => {
const repo = new UserRepository()
const page = await createPage('/')
await page.getByTestId('login-link').click()
Expand All @@ -79,7 +89,7 @@ describe('auth', () => {
await page.getByLabel('Password confirmation').type('test')
await page.getByTestId('submit').click()
await page.waitForURL('**/chats')
await expect(page.locator('h1').textContent()).resolves.toContain('Its time to join or create a chat room !')
await expectE2E(page.getByText('Its time to join or create a chat room !')).toBeVisible()
await expect(repo.collection.findOne({ username: 'test' })).resolves.toMatchObject({ username: 'test' })
})
})
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/db/repositories/user.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ describe('user repository', () => {
it('should reject if password does not match', async () => {
const repo = new UserRepository()
await repo.create({ username: 'foobar', password: 'test' })
await expect(repo.authenticate({ username: 'foobar', password: 'testy' })).rejects.toThrowError('Invalid username or password')
await expect(repo.authenticate({ username: 'foobar', password: 'testy' })).resolves.toBe(false)
})
})

Expand Down

0 comments on commit dc303f9

Please # to comment.