feat: Tests for channel creation dialog

This commit is contained in:
April Hall 2025-02-26 23:05:35 -05:00
parent 0e99daa477
commit a23ec93319
2 changed files with 72 additions and 1 deletions

View File

@ -1,7 +1,11 @@
import { z } from 'zod';
export const newChannelSchema = z.object({
channelName: z.string().min(1, 'Channel name is required').max(24, 'Channel name cannot be longer than 24 characters.'),
channelName: z
.string()
.min(1, 'Channel name is required')
.max(24, 'Channel name cannot be longer than 24 characters.')
.refine((value) => !/^\d/.test(value), 'Channel name cannot start with a number.'),
});
export type NewChannelSchema = typeof newChannelSchema;

View File

@ -0,0 +1,67 @@
import { test, expect, type Page } from '@playwright/test';
import { login } from './utils';
import { v4 as uuidv4 } from 'uuid';
function generate15CharUUID() {
// Second regex prevents UUID from starting with a number
return uuidv4().replace(/-/g, '').replace(/^\d*/g, '').substring(0, 15);
}
async function tryCreateChannel(page: Page, channelName: string) {
await page.waitForTimeout(1500);
await page.getByRole('button', { name: 'Create Channel' }).click();
await page.getByRole('textbox', { name: 'Channel Name' }).fill(channelName);
await page.getByRole('button', { name: 'Create', exact: true }).click();
}
test.describe('Create Channel', () => {
let page: Page;
test.beforeEach(async ({ browser }) => {
page = await browser.newPage();
// Login and navigate
await login(page);
await page.goto('/channel/general', { timeout: 30000, waitUntil: 'domcontentloaded' });
});
test('successfully create new channel', async ({ request }) => {
const uuid: string = generate15CharUUID();
console.log(uuid);
// Try to create new channel
await tryCreateChannel(page, uuid);
// Check if channel exists
const res = await request.get(`/channel/${uuid}`);
expect(res.status()).toEqual(200);
});
test('should not allow channel names > 25 characters', async () => {
// Try to create new channel
await tryCreateChannel(page, 'thisisatwentyfivelongstring;');
const error = page.getByText('Channel name cannot be longer than 24 characters.');
await expect(error).toBeVisible();
});
test('should not allow channel names to start with a number', async () => {
// Try to create new channel
await tryCreateChannel(page, '00-test');
const error = page.getByText('Channel name cannot start with a number.');
await expect(error).toBeVisible();
});
test('should not duplicate channel names', async () => {
const uuid: string = generate15CharUUID();
console.log(uuid);
// Try to create new channel
await tryCreateChannel(page, uuid);
await page.reload();
await tryCreateChannel(page, uuid);
const error = page.getByText('Channel already exists.');
await expect(error).toBeVisible();
});
});