From a23ec9331951527d6610efd090859249be8119e0 Mon Sep 17 00:00:00 2001 From: April Hall Date: Wed, 26 Feb 2025 23:05:35 -0500 Subject: [PATCH] feat: Tests for channel creation dialog --- src/lib/types/misc.ts | 6 +++- tests/createChannel.spec.ts | 67 +++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 tests/createChannel.spec.ts diff --git a/src/lib/types/misc.ts b/src/lib/types/misc.ts index e8bc2c8..a7b4f3d 100644 --- a/src/lib/types/misc.ts +++ b/src/lib/types/misc.ts @@ -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; diff --git a/tests/createChannel.spec.ts b/tests/createChannel.spec.ts new file mode 100644 index 0000000..1fb3105 --- /dev/null +++ b/tests/createChannel.spec.ts @@ -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(); + }); +});