Skip to content

Commit c2a4968

Browse files
Feat/adding test for replying to a comment (#1183)
1 parent 3531696 commit c2a4968

File tree

3 files changed

+123
-0
lines changed

3 files changed

+123
-0
lines changed

e2e/articles.spec.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,21 @@ 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(page.getByLabel("like-trigger")).toBeVisible();
29+
await expect(page.getByLabel("bookmark-trigger")).toBeVisible();
30+
});
31+
1732
test("Should show bookmark article icon", async ({ page }) => {
1833
await page.goto("http://localhost:3000/articles");
1934

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

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

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)