Skip to content

Connect, collaborate, and grow with a community of TypeScript developers

feat: implement authenticated comment tests (#713)

Bill Chengf0d22460412c
diff --git a/apps/web/.gitignore b/apps/web/.gitignore
index c7262c3f..3cf37bc9 100644
--- a/apps/web/.gitignore
+++ b/apps/web/.gitignore
@@ -5,3 +5,4 @@
/test-results/
/playwright-report/
/playwright/.cache/
+/playwright/.auth/
\ No newline at end of file
diff --git a/apps/web/playwright.config.ts b/apps/web/playwright.config.ts
index f7340be0..2976e639 100644
--- a/apps/web/playwright.config.ts
+++ b/apps/web/playwright.config.ts
@@ -6,24 +6,32 @@ export default defineConfig({
expect: {
timeout: 5 * 1000,
},
- fullyParallel: true,
+ fullyParallel: false,
forbidOnly: Boolean(process.env.CI),
retries: process.env.CI ? 2 : 0,
workers: process.env.CI ? 1 : undefined,
reporter: [['html', { open: 'never' }]],
use: {
- baseURL: 'http://127.0.0.1:3000',
- trace: 'on-first-retry',
+ baseURL: 'http://localhost:3000',
+ trace: process.env.CI ? 'on-first-retry' : 'retain-on-failure',
},
projects: [
+ { name: 'setup', testMatch: /.*\.setup\.ts/ },
{
name: 'chromium',
- use: { ...devices['Desktop Chrome'] },
+ use: {
+ ...devices['Desktop Chrome'],
+ storageState: 'playwright/.auth/user.json',
+ contextOptions: {
+ permissions: ['clipboard-read', 'clipboard-write'],
+ },
+ },
+ dependencies: ['setup'],
},
],
webServer: {
command: 'pnpm start',
- url: 'http://127.0.0.1:3000',
+ url: 'http://localhost:3000',
reuseExistingServer: !process.env.CI,
},
});
diff --git a/apps/web/tests/e2e/auth.setup.ts b/apps/web/tests/e2e/auth.setup.ts
new file mode 100644
index 00000000..371b852c
--- /dev/null
+++ b/apps/web/tests/e2e/auth.setup.ts
@@ -0,0 +1,70 @@
+import { test as setup } from '@playwright/test';
+import { prisma } from '@repo/db';
+import { addMonths } from 'date-fns';
+import { USER } from './constant';
+
+setup('unauthenticated', async ({ page }) => {
+ await page.context().storageState({ path: 'playwright/.auth/unauthenticated.json' });
+});
+
+setup('authenticate user', async ({ page }) => {
+ const expires = addMonths(new Date(), 1);
+
+ await prisma.user.upsert({
+ where: {
+ email: USER.email,
+ },
+ create: {
+ id: USER.id,
+ name: 'user',
+ email: USER.email,
+ sessions: {
+ create: {
+ expires,
+ sessionToken: USER.sessionToken,
+ },
+ },
+ roles: {
+ create: {
+ role: 'USER',
+ },
+ },
+ accounts: {
+ create: {
+ type: 'oauth',
+ provider: 'github',
+ providerAccountId: '12345678',
+ access_token: 'gho_abcd',
+ token_type: 'bearer',
+ scope: 'read:user,user:email',
+ },
+ },
+ },
+ update: {
+ sessions: {
+ update: {
+ where: {
+ sessionToken: USER.sessionToken,
+ },
+ data: {
+ expires,
+ },
+ },
+ },
+ },
+ });
+
+ await page.context().addCookies([
+ {
+ name: 'next-auth.session-token',
+ value: USER.sessionToken,
+ domain: 'localhost',
+ path: '/',
+ httpOnly: true,
+ sameSite: 'Lax',
+ expires: Math.floor(expires.getTime() / 1000),
+ },
+ ]);
+
+ await page.context().storageState({ path: 'playwright/.auth/user.json' });
+});
diff --git a/apps/web/tests/e2e/authenticated/comment.test.ts b/apps/web/tests/e2e/authenticated/comment.test.ts
new file mode 100644
index 00000000..36c6c859
--- /dev/null
+++ b/apps/web/tests/e2e/authenticated/comment.test.ts
@@ -0,0 +1,130 @@
+import { test, expect } from '@playwright/test';
+import { prisma } from '@repo/db';
+import { USER } from '../constant';
+import { ctrlV, wrapTypescriptCode } from '../helpers';
+
+test.describe('create, edit, and delete comments', () => {
+ test.beforeEach(async ({ page }) => {
+ await page.goto('/challenge/5');
+ await page.getByRole('button', { name: 'Comments' }).click();
+ });
+
+ const commentNumber = Math.floor(Math.random() * 1000);
+ const parentComment = `Here is my comment ${commentNumber}`;
+ const parentCodeText = `const comment${commentNumber} = true`;
+ const editedParentComment = `Here is my edited comment ${commentNumber}`;
+
+ const replyNumber = Math.floor(Math.random() * 1000);
+ const replyComment = `Here is my reply ${replyNumber} to comment ${commentNumber}`;
+ const replyCodeText = `const reply${replyNumber} = true`;
+ const editedReplyComment = `Here is my edited reply ${replyNumber} to comment ${commentNumber}`;
+
+ test('comment on challenge', async ({ page }) => {
+ await page
+ .getByPlaceholder('Enter your comment here.')
+ .fill(`${parentComment}${wrapTypescriptCode(parentCodeText)}`);
+ await page.getByRole('button', { name: 'Comment', exact: true }).click();
+
+ await expect(page.getByText(`${parentComment} ${parentCodeText}`)).toBeVisible();
+ });
+
+ test('reply to parent comment', async ({ page }) => {
+ const commentBlock = page.locator('div[id^=comment]', {
+ hasText: `${parentComment} ${parentCodeText}`,
+ });
+ await commentBlock.getByRole('button', { name: 'Reply' }).click();
+
+ await page.locator('*:focus').fill(`${replyComment}${wrapTypescriptCode(replyCodeText)}`);
+ await page.getByRole('button', { name: 'Comment', exact: true, disabled: false }).click();
+
+ await expect(page.getByText(`${replyComment} ${replyCodeText}`)).toBeVisible();
+ });
+
+ test('edit parent comment', async ({ page }) => {
+ const commentBlock = page.locator('div[id^=comment]', {
+ hasText: `${parentComment} ${parentCodeText}`,
+ });
+ await commentBlock.getByRole('button', { name: 'Edit' }).click();
+
+ await page
+ .locator('*:focus')
+ .fill(`${editedParentComment}${wrapTypescriptCode(parentCodeText)}`);
+ await page.getByRole('button', { name: 'Comment', exact: true, disabled: false }).click();
+
+ await expect(page.getByText(`${editedParentComment} ${parentCodeText}`)).toBeVisible();
+ });
+
+ test('edit reply', async ({ page }) => {
+ const commentBlock = page.locator('div[id^=comment]', {
+ hasText: `${editedParentComment} ${parentCodeText}`,
+ });
+ await commentBlock.getByRole('button', { name: '1 reply' }).click();
+
+ const replyBlock = page.locator('div[id^=comment]', {
+ hasText: `${replyComment} ${replyCodeText}`,
+ });
+ await replyBlock.getByRole('button', { name: 'Edit' }).click();
+
+ await page.locator('*:focus').fill(`${editedReplyComment}${wrapTypescriptCode(replyCodeText)}`);
+ await page.getByRole('button', { name: 'Comment', exact: true, disabled: false }).click();
+
+ await expect(page.getByText(`${editedReplyComment} ${replyCodeText}`)).toBeVisible();
+ });
+
+ test('delete reply', async ({ page }) => {
+ const commentBlock = page.locator('div[id^=comment]', {
+ hasText: `${editedParentComment} ${parentCodeText}`,
+ });
+ await commentBlock.getByRole('button', { name: '1 reply' }).click();
+
+ const replyBlock = page.locator('div[id^=comment]', {
+ hasText: `${editedReplyComment} ${replyCodeText}`,
+ });
+ await replyBlock.getByRole('button', { name: 'Delete' }).click();
+ await page.getByRole('button', { name: 'Delete' }).click();
+
+ await expect(
+ commentBlock.getByText(`${editedReplyComment} ${replyCodeText}`),
+ ).not.toBeVisible();
+ });
+
+ test('delete parent comment', async ({ page }) => {
+ const commentBlock = page.locator('div[id^=comment]', {
+ hasText: `${editedParentComment} ${parentCodeText}`,
+ });
+ await commentBlock.getByRole('button', { name: 'Delete' }).click();
+ await page.getByRole('button', { name: 'Delete' }).click();
+
+ await expect(
+ page.getByLabel('Description').getByText(`${editedParentComment} ${parentCodeText}`),
+ ).not.toBeVisible();
+ });
+});
+
+test.describe('share comment', () => {
+ const commentNumber = Math.floor(Math.random() * 1000);
+ const parentComment = `Here is my comment ${commentNumber}`;
+
+ test('share comment and navigate to comment link', async ({ page, context }) => {
+ const comment = await prisma.comment.create({
+ data: {
+ rootChallengeId: 6,
+ text: parentComment,
+ userId: USER.id,
+ },
+ });
+
+ await page.goto('/challenge/6');
+ await page.getByRole('button', { name: 'Comments' }).click();
+
+ const commentBlock = page.locator('div[id^=comment]', { hasText: parentComment });
+ await commentBlock.getByText('Share').click();
+
+ await page.getByPlaceholder('Enter your comment here.').focus();
+ await ctrlV(page);
+ await expect(page.getByText(`${page.url()}/comments/${comment.id}`)).toBeVisible();
+
+ await page.goto(`${page.url()}/comments/${comment.id}`);
+ await expect(page.locator('div[id^=comment]', { hasText: parentComment })).toBeVisible();
+ });
+});
diff --git a/apps/web/tests/e2e/constant.ts b/apps/web/tests/e2e/constant.ts
new file mode 100644
index 00000000..e83b8ba4
--- /dev/null
+++ b/apps/web/tests/e2e/constant.ts
@@ -0,0 +1,5 @@
+export const USER = {
+ id: 'e31340ff-f0cd-42c6-a2db-119352a55455',
+ email: '[email protected]',
+ sessionToken: 'd4d6a53d-b2e0-42c7-a4b3-2d246ce2f54a',
+};
diff --git a/apps/web/tests/e2e/helpers.ts b/apps/web/tests/e2e/helpers.ts
new file mode 100644
index 00000000..ea0b0a58
--- /dev/null
+++ b/apps/web/tests/e2e/helpers.ts
@@ -0,0 +1,18 @@
+import type { Page } from '@playwright/test';
+import os from 'node:os';
+
+export async function ctrlC(page: Page): Promise<void> {
+ const isMac = os.platform() === 'darwin';
+ const modifier = isMac ? 'Meta' : 'Control';
+ await page.keyboard.press(`${modifier}+KeyC`);
+}
+
+export async function ctrlV(page: Page): Promise<void> {
+ const isMac = os.platform() === 'darwin';
+ const modifier = isMac ? 'Meta' : 'Control';
+ await page.keyboard.press(`${modifier}+KeyV`);
+}
+
+export const wrapTypescriptCode = (text: string) => {
+ return `\n\`\`\`ts\n${text}\n\`\`\``;
+};
diff --git a/apps/web/tests/e2e/challenge.test.ts b/apps/web/tests/e2e/unauthenticated/challenge.test.ts
similarity index 96%
rename from apps/web/tests/e2e/challenge.test.ts
rename to apps/web/tests/e2e/unauthenticated/challenge.test.ts
index 23403f19..62ae9a1a 100644
--- a/apps/web/tests/e2e/challenge.test.ts
+++ b/apps/web/tests/e2e/unauthenticated/challenge.test.ts
@@ -1,5 +1,7 @@
import { test, expect } from '@playwright/test';
+test.use({ storageState: 'playwright/.auth/unauthenticated.json' });
+
test('challenge page', async ({ page }) => {
await page.goto('/challenge/4');
diff --git a/apps/web/tests/e2e/explore.test.ts b/apps/web/tests/e2e/unauthenticated/explore.test.ts
similarity index 74%
rename from apps/web/tests/e2e/explore.test.ts
rename to apps/web/tests/e2e/unauthenticated/explore.test.ts
index 9e43d729..c508ff77 100644
--- a/apps/web/tests/e2e/explore.test.ts
+++ b/apps/web/tests/e2e/unauthenticated/explore.test.ts
@@ -1,5 +1,7 @@
import { test, expect } from '@playwright/test';
+test.use({ storageState: 'playwright/.auth/unauthenticated.json' });
+
test('explore page', async ({ page }) => {
await page.goto('/explore');
diff --git a/apps/web/tests/e2e/home.test.ts b/apps/web/tests/e2e/unauthenticated/home.test.ts
similarity index 73%
rename from apps/web/tests/e2e/home.test.ts
rename to apps/web/tests/e2e/unauthenticated/home.test.ts
index 03d3205c..800c5444 100644
--- a/apps/web/tests/e2e/home.test.ts
+++ b/apps/web/tests/e2e/unauthenticated/home.test.ts
@@ -1,5 +1,7 @@
import { test, expect } from '@playwright/test';
+test.use({ storageState: 'playwright/.auth/unauthenticated.json' });
+
test('home page', async ({ page }) => {
await page.goto('/');