Skip to content

Commit ac34024

Browse files
Adding E2E tests for my-posts page Part 1 (#1187)
1 parent 2df1fd9 commit ac34024

File tree

6 files changed

+125
-37
lines changed

6 files changed

+125
-37
lines changed

E2E Overview.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
To run the end-to-end tests using Playwright, you need to configure your environment and follow these steps:
44

5-
6-
75
### Session and User Setup
86

97
First, you need to add your E2E test user to your locally running database. Do this by running the following script if you haven't already:

e2e/articles.spec.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { test, expect } from "playwright/test";
22
import { randomUUID } from "crypto";
3-
import { loggedInAsUserOne } from "./utils";
3+
import { articleContent, articleExcerpt, loggedInAsUserOne } from "./utils";
44

55
test.describe("Unauthenticated Articles Page", () => {
66
test("Should show popular tags", async ({ page, isMobile }) => {
@@ -17,10 +17,10 @@ test.describe("Unauthenticated Articles Page", () => {
1717
test("Should be able to navigate directly to an article", async ({
1818
page,
1919
}) => {
20-
await page.goto("http://localhost:3000/articles/e2e-test-slug-eqj0ozor");
21-
await expect(page.getByText("Lorem ipsum dolor sit amet,")).toBeVisible();
20+
await page.goto("http://localhost:3000/articles/e2e-test-slug-published");
21+
await expect(page.getByText(articleExcerpt)).toBeVisible();
2222
await expect(
23-
page.getByRole("heading", { name: "Test Article" }),
23+
page.getByRole("heading", { name: "Published Article" }),
2424
).toBeVisible();
2525
await expect(
2626
page.getByRole("heading", { name: "Written by E2E Test User One" }),
@@ -223,8 +223,6 @@ test.describe("Authenticated Articles Page", () => {
223223
});
224224

225225
test("Should write and publish an article", async ({ page, isMobile }) => {
226-
const articleContent =
227-
"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.";
228226
const articleTitle = "Lorem Ipsum";
229227
await page.goto("http://localhost:3000");
230228
// Waits for articles to be loaded
@@ -261,7 +259,7 @@ test.describe("Authenticated Articles Page", () => {
261259
/^http:\/\/localhost:3000\/articles\/lorem-ipsum-.*$/,
262260
);
263261

264-
await expect(page.getByText("Lorem ipsum dolor sit amet,")).toBeVisible();
262+
await expect(page.getByText(articleExcerpt)).toBeVisible();
265263
await expect(
266264
page.getByRole("heading", { name: "Lorem Ipsum" }),
267265
).toBeVisible();
@@ -291,14 +289,14 @@ test.describe("Authenticated Articles Page", () => {
291289
});
292290

293291
test("Should be able reply to a comment", async ({ page }) => {
294-
await page.goto("http://localhost:3000/articles/e2e-test-slug-eqj0ozor");
292+
await page.goto("http://localhost:3000/articles/e2e-test-slug-published");
295293
const numberOfCommentsIntially = await page
296294
.locator("div")
297295
.filter({ hasText: /^Thanks for the positive feedback!$/ })
298296
.count();
299-
await expect(page.getByText("Lorem ipsum dolor sit amet,")).toBeVisible();
297+
await expect(page.getByText(articleExcerpt)).toBeVisible();
300298
await expect(
301-
page.getByRole("heading", { name: "Test Article" }),
299+
page.getByRole("heading", { name: "Published Article" }),
302300
).toBeVisible();
303301
await expect(
304302
page.getByRole("heading", { name: "Written by E2E Test User One" }),

e2e/my-posts.spec.ts

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,56 @@
1-
import test from "@playwright/test";
2-
import { loggedInAsUserOne } from "./utils";
1+
import test, { expect } from "@playwright/test";
2+
import { articleExcerpt, loggedInAsUserOne } from "./utils";
33

44
test.describe("Unauthenticated my-posts Page", () => {
5-
//
6-
// Replace with tests for unauthenticated users
5+
test("Unauthenticated users should be redirected to get-started page if they access my-posts directly", async ({
6+
page,
7+
}) => {
8+
await page.goto("http://localhost:3000/my-posts");
9+
await page.waitForURL("http://localhost:3000/get-started");
10+
expect(page.url()).toEqual("http://localhost:3000/get-started");
11+
});
712
});
813

914
test.describe("Authenticated my-posts Page", () => {
1015
test.beforeEach(async ({ page }) => {
1116
await loggedInAsUserOne(page);
1217
});
13-
//
14-
// Replace with tests for authenticated users
18+
19+
test("Tabs for different type of posts should be visible", async ({
20+
page,
21+
}) => {
22+
await page.goto("http://localhost:3000/my-posts");
23+
24+
await expect(page.getByRole("link", { name: "Drafts" })).toBeVisible();
25+
await expect(page.getByRole("link", { name: "Scheduled" })).toBeVisible();
26+
await expect(page.getByRole("link", { name: "Published" })).toBeVisible();
27+
});
28+
29+
test("Different article tabs should correctly display articles matching that type", async ({
30+
page,
31+
}) => {
32+
await page.goto("http://localhost:3000/my-posts");
33+
34+
await expect(page.getByRole("link", { name: "Drafts" })).toBeVisible();
35+
await expect(page.getByRole("link", { name: "Scheduled" })).toBeVisible();
36+
await expect(page.getByRole("link", { name: "Published" })).toBeVisible();
37+
38+
await page.getByRole("link", { name: "Drafts" }).click();
39+
await expect(
40+
page.getByRole("heading", { name: "Draft Article" }),
41+
).toBeVisible();
42+
await expect(page.getByText(articleExcerpt)).toBeVisible();
43+
44+
await page.getByRole("link", { name: "Scheduled" }).click();
45+
await expect(
46+
page.getByRole("heading", { name: "Scheduled Article" }),
47+
).toBeVisible();
48+
await expect(page.getByText(articleExcerpt)).toBeVisible();
49+
50+
await page.getByRole("link", { name: "Published" }).click();
51+
await expect(
52+
page.getByRole("heading", { name: "Published Article" }),
53+
).toBeVisible();
54+
await expect(page.getByText(articleExcerpt, { exact: true })).toBeVisible();
55+
});
1556
});

e2e/setup.ts

Lines changed: 65 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import postgres from "postgres";
22
import { drizzle } from "drizzle-orm/postgres-js";
33
import { post, comment, session, user } from "@/server/db/schema";
44
import {
5+
articleContent,
6+
articleExcerpt,
57
E2E_USER_ONE_EMAIL,
68
E2E_USER_ONE_ID,
79
E2E_USER_ONE_SESSION_ID,
@@ -12,37 +14,81 @@ import {
1214
import { eq } from "drizzle-orm";
1315

1416
export const setup = async () => {
17+
// Dynamically import nanoid
18+
const { nanoid } = await import("nanoid");
19+
1520
const db = drizzle(
1621
postgres("postgresql://postgres:[email protected]:5432/postgres"),
1722
);
18-
1923
const addE2EArticleAndComment = async (
2024
authorId: string,
2125
commenterId: string,
2226
) => {
23-
const postId = "1nFnMmN5";
27+
const publishedPostId = nanoid(8);
28+
const scheduledPostId = nanoid(8);
29+
const draftPostId = nanoid(8);
2430
const now = new Date().toISOString();
25-
await db
26-
.insert(post)
27-
.values({
28-
id: postId,
29-
published: now,
30-
excerpt: "Lorem ipsum dolor sit amet",
31-
updatedAt: now,
32-
slug: "e2e-test-slug-eqj0ozor",
33-
likes: 10,
34-
readTimeMins: 3,
35-
title: "Test Article",
36-
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.",
37-
userId: authorId,
38-
})
39-
.onConflictDoNothing()
40-
.returning();
31+
32+
const oneYearFromToday = new Date(now);
33+
oneYearFromToday.setFullYear(oneYearFromToday.getFullYear() + 1);
34+
35+
await Promise.all([
36+
db
37+
.insert(post)
38+
.values({
39+
id: publishedPostId,
40+
published: now,
41+
excerpt: articleExcerpt,
42+
updatedAt: now,
43+
slug: "e2e-test-slug-published",
44+
likes: 10,
45+
readTimeMins: 3,
46+
title: "Published Article",
47+
body: articleContent,
48+
userId: authorId,
49+
})
50+
.onConflictDoNothing()
51+
.returning(),
52+
53+
db
54+
.insert(post)
55+
.values({
56+
id: draftPostId,
57+
published: null,
58+
excerpt: articleExcerpt,
59+
updatedAt: now,
60+
slug: "e2e-test-slug-draft",
61+
likes: 10,
62+
readTimeMins: 3,
63+
title: "Draft Article",
64+
body: articleContent,
65+
userId: authorId,
66+
})
67+
.onConflictDoNothing()
68+
.returning(),
69+
70+
db
71+
.insert(post)
72+
.values({
73+
id: scheduledPostId,
74+
published: oneYearFromToday.toISOString(),
75+
excerpt: articleExcerpt,
76+
updatedAt: now,
77+
slug: "e2e-test-slug-scheduled",
78+
likes: 10,
79+
readTimeMins: 3,
80+
title: "Scheduled Article",
81+
body: articleContent,
82+
userId: authorId,
83+
})
84+
.onConflictDoNothing()
85+
.returning(),
86+
]);
4187

4288
await db
4389
.insert(comment)
4490
.values({
45-
postId,
91+
postId: publishedPostId,
4692
body: "What a great article! Thanks for sharing",
4793
userId: commenterId,
4894
})

e2e/utils/constants.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export const articleContent =
2+
"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.";
3+
4+
export const articleExcerpt = "Lorem ipsum dolor sit amet";

e2e/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from "./utils";
2+
export * from "./constants";

0 commit comments

Comments
 (0)