Skip to content

Commit f023902

Browse files
committed
feat: added a test to check a user can reply to a comment
1 parent 168f007 commit f023902

File tree

3 files changed

+126
-0
lines changed

3 files changed

+126
-0
lines changed

e2e/articles.spec.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,24 @@ test.describe("Unauthenticated Articles Page", () => {
1414
);
1515
});
1616

17+
test("Should be able to navigate directly to an article", async ({
18+
page,
19+
}) => {
20+
await page.goto("http://localhost:3000/articles/e2e-test-slug-eqj0ozor");
21+
await expect(page.getByText("Lorem ipsum dolor sit amet,")).toBeVisible();
22+
await expect(
23+
page.getByRole("heading", { name: "Test Article" }),
24+
).toBeVisible();
25+
await expect(
26+
page.getByRole("heading", { name: "Written by E2E Test User One" }),
27+
).toBeVisible();
28+
await expect(
29+
page.getByRole("heading", { name: "Discussion (0)" }),
30+
).toBeVisible();
31+
await expect(page.getByLabel("like-trigger")).toBeVisible();
32+
await expect(page.getByLabel("bookmark-trigger")).toBeVisible();
33+
});
34+
1735
test("Should show bookmark article icon", async ({ page }) => {
1836
await page.goto("http://localhost:3000/articles");
1937

@@ -274,4 +292,46 @@ test.describe("Authenticated Articles Page", () => {
274292

275293
await expect(page.getByText(commentContent)).toBeVisible();
276294
});
295+
296+
test("Should be able reply to a comment", async ({ page }) => {
297+
await page.goto("http://localhost:3000/articles/e2e-test-slug-eqj0ozor");
298+
const numberOfCommentsIntially = await page
299+
.locator("div")
300+
.filter({ hasText: /^Thanks for the positive feedback!$/ })
301+
.count();
302+
await expect(page.getByText("Lorem ipsum dolor sit amet,")).toBeVisible();
303+
await expect(
304+
page.getByRole("heading", { name: "Test Article" }),
305+
).toBeVisible();
306+
await expect(
307+
page.getByRole("heading", { name: "Written by E2E Test User One" }),
308+
).toBeVisible();
309+
await expect(
310+
page.getByRole("heading", { name: "Discussion (0)" }),
311+
).toBeVisible();
312+
await expect(page.getByLabel("like-trigger")).toBeVisible();
313+
await expect(page.getByLabel("bookmark-trigger")).toBeVisible();
314+
315+
await page.getByRole("button", { name: "Reply" }).first().click();
316+
317+
await page.locator("#reply").fill("Thanks for the positive feedback!");
318+
await page.getByRole("button", { name: "Submit" }).nth(1).click();
319+
await page.waitForTimeout(250);
320+
321+
await expect(
322+
page.getByText("AUTHOR", { exact: true }).first(),
323+
).toBeVisible();
324+
const numberOfCommentsAfteringCommenting = await page
325+
.locator("div")
326+
.filter({ hasText: /^Thanks for the positive feedback!$/ })
327+
.count();
328+
expect(numberOfCommentsAfteringCommenting).toBeGreaterThan(
329+
numberOfCommentsIntially,
330+
);
331+
await expect(
332+
page
333+
.getByRole("link", { name: "E2E Test User One", exact: true })
334+
.nth(numberOfCommentsIntially + 1),
335+
).toBeVisible();
336+
});
277337
});

e2e/setup.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import dotenv from "dotenv";
2+
import postgres from "postgres";
3+
import { drizzle } from "drizzle-orm/postgres-js";
4+
import { post, comment } from "@/server/db/schema";
5+
6+
dotenv.config(); // Load .env file contents into process.env
7+
8+
export const setup = async () => {
9+
if (
10+
!process.env.DATABASE_URL ||
11+
!process.env.E2E_USER_ONE_ID ||
12+
!process.env.E2E_USER_TWO_ID
13+
) {
14+
throw new Error("Missing env variables for DB clean up script");
15+
}
16+
const db = drizzle(postgres(process.env.DATABASE_URL as string));
17+
18+
const addE2EArticleAndComment = async (
19+
authorId: string,
20+
commenterId: string,
21+
) => {
22+
const postId = "1nFnMmN5";
23+
const now = new Date().toISOString();
24+
await db
25+
.insert(post)
26+
.values({
27+
id: postId,
28+
published: now,
29+
excerpt: "Lorem ipsum dolor sit amet",
30+
updatedAt: now,
31+
slug: "e2e-test-slug-eqj0ozor",
32+
likes: 10,
33+
readTimeMins: 3,
34+
title: "Test Article",
35+
body: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas vitae ipsum id metus vestibulum rutrum eget a diam. Integer eget vulputate risus, ac convallis nulla. Mauris sed augue nunc. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Nam congue posuere tempor. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Ut ac augue non libero ullamcorper ornare. Ut commodo ligula vitae malesuada maximus. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Etiam sagittis justo non justo placerat, a dapibus sapien volutpat. Nullam ullamcorper sodales justo sed.",
36+
userId: authorId,
37+
})
38+
.onConflictDoNothing()
39+
.returning();
40+
41+
await db
42+
.insert(comment)
43+
.values({
44+
postId,
45+
body: "What a great article! Thanks for sharing",
46+
userId: commenterId,
47+
})
48+
.onConflictDoNothing()
49+
.returning();
50+
};
51+
52+
try {
53+
console.log("creating articles");
54+
55+
await addE2EArticleAndComment(
56+
process.env.E2E_USER_ONE_ID as string,
57+
process.env.E2E_USER_TWO_ID as string,
58+
);
59+
console.log("DB setup successful");
60+
} catch (err) {
61+
console.log("Error while setting up DB before E2E test run", err);
62+
}
63+
};
64+
65+
export default setup;

playwright.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { defineConfig, devices } from "@playwright/test";
1010
* See https://playwright.dev/docs/test-configuration.
1111
*/
1212
export default defineConfig({
13+
globalSetup: "./e2e/setup.ts",
1314
globalTeardown: "./e2e/teardown.ts",
1415
testDir: "e2e",
1516
/* Run tests in files in parallel */

0 commit comments

Comments
 (0)