Skip to content

Commit

Permalink
Merge branch 'codu-code:develop' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfofdalalst authored Oct 23, 2024
2 parents 0b92fde + e4102dd commit 57b1265
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/(app)/articles/_client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ const ArticlesPage = () => {
readTimeMins,
id,
currentUserBookmarkedPost,
likes,
}) => {
// TODO: Bump posts that were recently updated to the top and show that they were updated recently
if (!published) return null;
Expand All @@ -140,6 +141,7 @@ const ArticlesPage = () => {
date={published}
readTime={readTimeMins}
bookmarkedInitialState={currentUserBookmarkedPost}
likes={likes}
/>
);
},
Expand Down
12 changes: 12 additions & 0 deletions components/ArticlePreview/ArticlePreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Temporal } from "@js-temporal/polyfill";
import {
BookmarkIcon,
EllipsisHorizontalIcon,
HeartIcon,
} from "@heroicons/react/20/solid";
import {
Menu,
Expand Down Expand Up @@ -39,6 +40,7 @@ type Props = {
menuOptions?: Array<ButtonOptions | LinkOptions>;
showBookmark?: boolean;
bookmarkedInitialState?: boolean;
likes: number;
};

const ArticlePreview: NextPage<Props> = ({
Expand All @@ -54,6 +56,7 @@ const ArticlePreview: NextPage<Props> = ({
menuOptions,
showBookmark = true,
bookmarkedInitialState = false,
likes,
}) => {
const { data: session } = useSession();
const [bookmarked, setIsBookmarked] = useState(bookmarkedInitialState);
Expand Down Expand Up @@ -125,6 +128,15 @@ const ArticlePreview: NextPage<Props> = ({
<>
<span aria-hidden="true">&middot;</span>
<span>{readTime} min read</span>
{likes && (
<>
<span aria-hidden="true">&middot;</span>
<span data-likes={likes}>{likes}</span>
<HeartIcon
className={`relative top-[1px] h-3.5 w-3.5 fill-red-400`}
/>
</>
)}
</>
)}
<div className="flex items-center justify-start"></div>
Expand Down
54 changes: 54 additions & 0 deletions e2e/articles.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,60 @@ test.describe("Unauthenticated Articles Page", () => {
page.getByText("Sign in or sign up to leave a comment"),
).toBeVisible();
});

test("Should sort articles by Newest", async ({ page }) => {
await page.goto("http://localhost:3000/articles");
await page.waitForSelector("article");

const articles = await page.$$eval("article", (articles) => {
return articles.map((article) => ({
date: article.querySelector("time")?.dateTime,
}));
});
const isSortedNewest = articles.every((article, index, arr) => {
if (index === arr.length - 1) return true;
if (!article.date || !arr[index + 1].date) return false;
return new Date(article.date) >= new Date(arr[index + 1].date!);
});
expect(isSortedNewest).toBeTruthy();
});

test("Should sort articles by Oldest", async ({ page }) => {
await page.goto("http://localhost:3000/articles?filter=oldest");
await page.waitForSelector("article");
const articles = await page.$$eval("article", (articles) => {
return articles.map((article) => ({
date: article.querySelector("time")?.dateTime,
}));
});
const isSortedOldest = articles.every((article, index, arr) => {
if (index === arr.length - 1) return true;
if (!article.date || !arr[index + 1].date) return false;
return new Date(article.date) <= new Date(arr[index + 1].date!);
});
expect(isSortedOldest).toBeTruthy();
});

test("Should sort articles by Top - likes", async ({ page }) => {
await page.goto("http://localhost:3000/articles?filter=top");
await page.waitForSelector("article");

const articles = await page.$$eval("article", (articles) => {
return articles.map((article) => ({
likes: parseInt(
article.querySelector("[data-likes]")?.getAttribute("data-likes") ||
"0",
10,
),
}));
});

const isSortedTop = articles.every((article, index, arr) => {
if (index === arr.length - 1) return true;
return article.likes >= arr[index + 1].likes;
});
expect(isSortedTop).toBeTruthy();
});
});

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

0 comments on commit 57b1265

Please sign in to comment.