Skip to content

Commit

Permalink
A few new e2e tests ✨
Browse files Browse the repository at this point in the history
  • Loading branch information
parkerdavis1 committed Jan 6, 2024
1 parent bf0cc98 commit 13c13cc
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 3 deletions.
62 changes: 62 additions & 0 deletions tests/e2e/admin.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { faker } from '@faker-js/faker'
import invariant from 'tiny-invariant'
import { expect, test, insertNewAdmin } from '../playwright-utils.ts'

test('login as admin', async ({ page }) => {
const password = faker.internet.password()
const adminUser = await insertNewAdmin({ password })
invariant(adminUser.name, 'User name not found')
await page.goto('/login')
await page
.getByRole('textbox', { name: /username/i })
.fill(adminUser.username)
await page.getByLabel(/^password$/i).fill(password)
await page.getByRole('button', { name: /log in/i }).click()
await expect(page).toHaveURL(`/`)

await expect(page.getByRole('link', { name: /admin/i })).toBeVisible()

// Navigate to users page
await page.getByRole('link', { name: /admin/i }).click()
await page.getByRole('menuitem', { name: 'Users' }).click()
await expect(page.getByRole('heading', { name: 'Users' })).toBeVisible()

// Open Edit user page
await page.getByText('open menu').first().click()
await page.getByRole('menuitem', { name: 'Edit' }).click()
await expect(page.getByRole('heading', { name: /edit user:/i })).toBeVisible()

// Test validation for feet
await page.getByLabel('feet').fill('-1')
await page.getByRole('button', { name: 'Save' }).click()
await expect(page.getByText('Feet must be between 0 and 8')).toBeVisible()

await page.getByLabel('feet').fill('text not allowed')
await page.getByRole('button', { name: 'Save' }).click()
await expect(page.getByText('Feet must be a number')).toBeVisible()

await page.getByLabel('feet').fill('')
await page.getByLabel('inches').fill('6')
await page.getByRole('button', { name: 'Save' }).click()
await expect(
page.getByText('You must enter both feet and inches for height'),
).toBeVisible()

// Heights are saved to database and displayed
await page.getByLabel('feet').fill('4')
await page.getByLabel('inches').fill('5')
await page.getByRole('button', { name: 'Save' }).click()
await page.getByText('open menu').first().click()
await page.getByRole('menuitem', { name: 'Edit' }).click()
await expect(page.getByLabel('feet')).toHaveValue('4')
await expect(page.getByLabel('inches')).toHaveValue('5')

// Blank values are saved as null
await page.getByLabel('feet').fill('')
await page.getByLabel('inches').fill('')
await page.getByRole('button', { name: 'Save' }).click()
await page.getByText('open menu').first().click()
await page.getByRole('menuitem', { name: 'Edit' }).click()
await expect(page.getByLabel('feet')).toHaveValue('')
await expect(page.getByLabel('inches')).toHaveValue('')
})
77 changes: 77 additions & 0 deletions tests/e2e/settings-profile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,80 @@ test('Users can update their profile photo', async ({ login, page }) => {

expect(beforeSrc).not.toEqual(afterSrc)
})

test('Users can edit and view height with proper validations', async ({
login,
page,
}) => {
await login()
await page.goto('/settings/profile')

// Validation: no negative numbers
await page.getByLabel('feet').fill('-1')
await page.getByRole('button', { name: 'Save changes' }).click()
await expect(page.getByText('Feet must be between 0 and 8')).toBeVisible()

// Validation: no text allowed
await page.getByLabel('feet').fill('text not allowed')
await page.getByRole('button', { name: 'Save changes' }).click()
await expect(page.getByText('Feet must be a number')).toBeVisible()

// Validation: must have both feet and inches
await page.getByLabel('feet').fill('')
await page.getByLabel('inches').fill('6')
await page.getByRole('button', { name: 'Save changes' }).click()
await expect(
page.getByText('You must enter both feet and inches for height'),
).toBeVisible()

// Heights are saved to database and displayed
await page.getByLabel('feet').fill('4')
await page.getByLabel('inches').fill('5')
await page.getByRole('button', { name: 'Save changes' }).click()
await page.goto('/settings/profile')

await expect(page.getByLabel('feet')).toHaveValue('4')
await expect(page.getByLabel('inches')).toHaveValue('5')

// Blank values are saved as null
await page.getByLabel('feet').fill('')
await page.getByLabel('inches').fill('')
await page.getByRole('button', { name: 'Save' }).click()

await page.goto('/settings/profile')

await expect(page.getByLabel('feet')).toHaveValue('')
await expect(page.getByLabel('inches')).toHaveValue('')
})

test('Users can edit their years of experience with proper validation', async ({
login,
page,
}) => {
await login()
await page.goto('/settings/profile')

// Validation: no negative numbers
await page.getByLabel(/years of experience/i).fill('-1')
await page.getByRole('button', { name: 'Save changes' }).click()
await expect(
page.getByText('Number must be greater than or equal to 0'),
).toBeVisible()

// Validation: Text not allowed
await page.getByLabel(/years of experience/i).fill('text not allowed')
await page.getByRole('button', { name: 'Save changes' }).click()
await expect(page.getByText('Must input valid number')).toBeVisible()

// Heights are saved to database and displayed
await page.getByLabel(/years of experience/i).fill('4')
await page.getByRole('button', { name: 'Save changes' }).click()
await page.goto('/settings/profile')
await expect(page.getByLabel(/years of experience/i)).toHaveValue('4')

// Blank values are saved as null
await page.getByLabel(/years of experience/i).fill('')
await page.getByRole('button', { name: 'Save' }).click()
await page.goto('/settings/profile')
await expect(page.getByLabel(/years of experience/i)).toHaveValue('')
})
35 changes: 32 additions & 3 deletions tests/playwright-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,35 @@ export async function insertNewUser({ password }: { password?: string } = {}) {
return user
}

export async function insertNewAdmin({ password }: { password?: string } = {}) {
const userData = createUser()

const adminRole = await prisma.role.findFirst({
where: {
name: 'admin',
},
})

if (!adminRole) throw new Error('No admin role defined')

const user = await prisma.user.create({
data: {
...userData,
password: {
create: {
hash: await getPasswordHash(password || userData.username),
},
},
roles: {
connect: { id: adminRole.id },
},
},
select: { id: true, name: true, username: true, email: true },
})
dataCleanup.users.add(user.id)
return user
}

export const test = base.extend<{
login: (user?: { id: string }) => ReturnType<typeof loginPage>
}>({
Expand Down Expand Up @@ -91,9 +120,9 @@ export async function loginPage({
export async function setSignupPassword() {
await prisma.signupPassword.deleteMany()
await prisma.signupPassword.create({
data: {
hash: await getPasswordHash('horses are cool'),
}
data: {
hash: await getPasswordHash('horses are cool'),
},
})
}

Expand Down

0 comments on commit 13c13cc

Please sign in to comment.