feat: add tests (#843)
This commit is contained in:
parent
95a2af2535
commit
9dd9a9898c
35 changed files with 1063 additions and 191 deletions
77
tests/auth.test.ts
Normal file
77
tests/auth.test.ts
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
import { generateId } from 'ai';
|
||||
import { getUnixTime } from 'date-fns';
|
||||
import { test, expect, Page } from '@playwright/test';
|
||||
|
||||
test.use({ storageState: { cookies: [], origins: [] } });
|
||||
|
||||
const testEmail = `test-${getUnixTime(new Date())}@playwright.com`;
|
||||
const testPassword = generateId(16);
|
||||
|
||||
class AuthPage {
|
||||
constructor(private page: Page) {}
|
||||
|
||||
async gotoLogin() {
|
||||
await this.page.goto('/login');
|
||||
await expect(this.page.getByRole('heading')).toContainText('Sign In');
|
||||
}
|
||||
|
||||
async gotoRegister() {
|
||||
await this.page.goto('/register');
|
||||
await expect(this.page.getByRole('heading')).toContainText('Sign Up');
|
||||
}
|
||||
|
||||
async register(email: string, password: string) {
|
||||
await this.gotoRegister();
|
||||
await this.page.getByPlaceholder('user@acme.com').click();
|
||||
await this.page.getByPlaceholder('user@acme.com').fill(email);
|
||||
await this.page.getByLabel('Password').click();
|
||||
await this.page.getByLabel('Password').fill(password);
|
||||
await this.page.getByRole('button', { name: 'Sign Up' }).click();
|
||||
}
|
||||
|
||||
async login(email: string, password: string) {
|
||||
await this.gotoLogin();
|
||||
await this.page.getByPlaceholder('user@acme.com').click();
|
||||
await this.page.getByPlaceholder('user@acme.com').fill(email);
|
||||
await this.page.getByLabel('Password').click();
|
||||
await this.page.getByLabel('Password').fill(password);
|
||||
await this.page.getByRole('button', { name: 'Sign In' }).click();
|
||||
}
|
||||
|
||||
async expectToastToContain(text: string) {
|
||||
await expect(this.page.getByTestId('toast')).toContainText(text);
|
||||
}
|
||||
}
|
||||
|
||||
test.describe
|
||||
.serial('authentication', () => {
|
||||
let authPage: AuthPage;
|
||||
|
||||
test.beforeEach(async ({ page }) => {
|
||||
authPage = new AuthPage(page);
|
||||
});
|
||||
|
||||
test('redirect to login page when unauthenticated', async ({ page }) => {
|
||||
await page.goto('/');
|
||||
await expect(page.getByRole('heading')).toContainText('Sign In');
|
||||
});
|
||||
|
||||
test('register a test account', async ({ page }) => {
|
||||
await authPage.register(testEmail, testPassword);
|
||||
await expect(page).toHaveURL('/');
|
||||
await authPage.expectToastToContain('Account created successfully!');
|
||||
});
|
||||
|
||||
test('register test account with existing email', async () => {
|
||||
await authPage.register(testEmail, testPassword);
|
||||
await authPage.expectToastToContain('Account already exists!');
|
||||
});
|
||||
|
||||
test('log into account', async ({ page }) => {
|
||||
await authPage.login(testEmail, testPassword);
|
||||
|
||||
await page.waitForURL('/');
|
||||
await expect(page).toHaveURL('/');
|
||||
await expect(page.getByPlaceholder('Send a message...')).toBeVisible();
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue