Skip to content

Commit e4102dd

Browse files
Feat/e2e testing for article sorting (#1159)
1 parent e586a94 commit e4102dd

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

app/(app)/articles/_client.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ const ArticlesPage = () => {
124124
readTimeMins,
125125
id,
126126
currentUserBookmarkedPost,
127+
likes,
127128
}) => {
128129
// TODO: Bump posts that were recently updated to the top and show that they were updated recently
129130
if (!published) return null;
@@ -140,6 +141,7 @@ const ArticlesPage = () => {
140141
date={published}
141142
readTime={readTimeMins}
142143
bookmarkedInitialState={currentUserBookmarkedPost}
144+
likes={likes}
143145
/>
144146
);
145147
},

components/ArticlePreview/ArticlePreview.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Temporal } from "@js-temporal/polyfill";
77
import {
88
BookmarkIcon,
99
EllipsisHorizontalIcon,
10+
HeartIcon,
1011
} from "@heroicons/react/20/solid";
1112
import {
1213
Menu,
@@ -39,6 +40,7 @@ type Props = {
3940
menuOptions?: Array<ButtonOptions | LinkOptions>;
4041
showBookmark?: boolean;
4142
bookmarkedInitialState?: boolean;
43+
likes: number;
4244
};
4345

4446
const ArticlePreview: NextPage<Props> = ({
@@ -54,6 +56,7 @@ const ArticlePreview: NextPage<Props> = ({
5456
menuOptions,
5557
showBookmark = true,
5658
bookmarkedInitialState = false,
59+
likes,
5760
}) => {
5861
const { data: session } = useSession();
5962
const [bookmarked, setIsBookmarked] = useState(bookmarkedInitialState);
@@ -125,6 +128,15 @@ const ArticlePreview: NextPage<Props> = ({
125128
<>
126129
<span aria-hidden="true">&middot;</span>
127130
<span>{readTime} min read</span>
131+
{likes && (
132+
<>
133+
<span aria-hidden="true">&middot;</span>
134+
<span data-likes={likes}>{likes}</span>
135+
<HeartIcon
136+
className={`relative top-[1px] h-3.5 w-3.5 fill-red-400`}
137+
/>
138+
</>
139+
)}
128140
</>
129141
)}
130142
<div className="flex items-center justify-start"></div>

e2e/articles.spec.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,60 @@ test.describe("Unauthenticated Articles Page", () => {
7676
page.getByText("Sign in or sign up to leave a comment"),
7777
).toBeVisible();
7878
});
79+
80+
test("Should sort articles by Newest", async ({ page }) => {
81+
await page.goto("http://localhost:3000/articles");
82+
await page.waitForSelector("article");
83+
84+
const articles = await page.$$eval("article", (articles) => {
85+
return articles.map((article) => ({
86+
date: article.querySelector("time")?.dateTime,
87+
}));
88+
});
89+
const isSortedNewest = articles.every((article, index, arr) => {
90+
if (index === arr.length - 1) return true;
91+
if (!article.date || !arr[index + 1].date) return false;
92+
return new Date(article.date) >= new Date(arr[index + 1].date!);
93+
});
94+
expect(isSortedNewest).toBeTruthy();
95+
});
96+
97+
test("Should sort articles by Oldest", async ({ page }) => {
98+
await page.goto("http://localhost:3000/articles?filter=oldest");
99+
await page.waitForSelector("article");
100+
const articles = await page.$$eval("article", (articles) => {
101+
return articles.map((article) => ({
102+
date: article.querySelector("time")?.dateTime,
103+
}));
104+
});
105+
const isSortedOldest = articles.every((article, index, arr) => {
106+
if (index === arr.length - 1) return true;
107+
if (!article.date || !arr[index + 1].date) return false;
108+
return new Date(article.date) <= new Date(arr[index + 1].date!);
109+
});
110+
expect(isSortedOldest).toBeTruthy();
111+
});
112+
113+
test("Should sort articles by Top - likes", async ({ page }) => {
114+
await page.goto("http://localhost:3000/articles?filter=top");
115+
await page.waitForSelector("article");
116+
117+
const articles = await page.$$eval("article", (articles) => {
118+
return articles.map((article) => ({
119+
likes: parseInt(
120+
article.querySelector("[data-likes]")?.getAttribute("data-likes") ||
121+
"0",
122+
10,
123+
),
124+
}));
125+
});
126+
127+
const isSortedTop = articles.every((article, index, arr) => {
128+
if (index === arr.length - 1) return true;
129+
return article.likes >= arr[index + 1].likes;
130+
});
131+
expect(isSortedTop).toBeTruthy();
132+
});
79133
});
80134

81135
test.describe("Authenticated Articles Page", () => {

0 commit comments

Comments
 (0)