-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from rocky-linux/feature/fix-news
Fix Possible News Problems and Add Tests
- Loading branch information
Showing
6 changed files
with
230 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import Link from "next/link"; | ||
import { useTranslations } from "next-intl"; | ||
|
||
export default function SlugNotFound() { | ||
const t = useTranslations("news.notFound"); | ||
|
||
return ( | ||
<div className="pt-10 pb-24 sm:pt-12 sm:pb-32"> | ||
<div className="mx-auto max-w-3xl text-base leading-7"> | ||
<h1 className="mt-2 text-3xl font-bold tracking-tight sm:text-4xl mb-12 text-center font-display"> | ||
{t("title")} | ||
</h1> | ||
<div className="prose dark:prose-invert prose-headings:font-display prose-a:text-primary prose-pre:bg-muted prose-pre:py-3 prose-pre:px-4 prose-pre:rounded prose-img:rounded-md max-w-none"> | ||
{t.rich("description", { | ||
newsLink: (chunks) => <Link href="/news">{chunks}</Link>, | ||
})} | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import * as newsLib from "../news"; | ||
|
||
// Mocks | ||
import fs, { Dirent } from "fs"; | ||
import path from "path"; | ||
import "@/utils/remarkUtils"; | ||
|
||
jest.mock("@/utils/remarkUtils", () => ({ | ||
processMarkdownAsHTML: jest | ||
.fn() | ||
.mockResolvedValue("<p>Mocked HTML content</p>"), | ||
})); | ||
|
||
jest.mock("fs", () => ({ | ||
promises: { | ||
access: jest.fn(), | ||
readdir: jest.fn(), | ||
readFile: jest.fn(), | ||
}, | ||
})); | ||
|
||
// Tests | ||
|
||
describe("News Library", () => { | ||
beforeEach(() => { | ||
const mockFileNames = ["post1.md", "post2.md"]; | ||
const mockFileContents = [ | ||
'---\ntitle: Post 1\ndate: "2022-01-01"\n---\n\nPost 1 content', | ||
'---\ntitle: Post 2\ndate: "2022-01-02"\n---\n\nPost 2 content', | ||
]; | ||
|
||
jest.spyOn(fs.promises, "access").mockImplementation((filePath) => { | ||
const index = mockFileNames.indexOf(path.basename(filePath.toString())); | ||
return index >= 0 ? Promise.resolve() : Promise.reject(); | ||
}); | ||
|
||
jest | ||
.spyOn(fs.promises, "readdir") | ||
.mockResolvedValue(mockFileNames as unknown as Dirent[]); | ||
|
||
jest.spyOn(fs.promises, "readFile").mockImplementation((filePath) => { | ||
const index = mockFileNames.indexOf(path.basename(filePath.toString())); | ||
return Promise.resolve(mockFileContents[index]); | ||
}); | ||
}); | ||
|
||
describe("checkIfSlugIsValid", () => { | ||
it("should return true if the slug is valid", async () => { | ||
const slug = "post1"; | ||
const isValid = await newsLib.checkIfSlugIsValid(slug); | ||
expect(isValid).toBe(true); | ||
}); | ||
|
||
it("should return false if the slug is blank", async () => { | ||
const slug = ""; | ||
const isValid = await newsLib.checkIfSlugIsValid(slug); | ||
expect(isValid).toBe(false); | ||
}); | ||
|
||
it("should return false if the slug is invalid", async () => { | ||
const slug = "invalid-slug"; | ||
const isValid = await newsLib.checkIfSlugIsValid(slug); | ||
expect(isValid).toBe(false); | ||
}); | ||
|
||
it("should return false if the slug is in an invalid format", async () => { | ||
const slug = "invalid/slug"; | ||
const isValid = await newsLib.checkIfSlugIsValid(slug); | ||
expect(isValid).toBe(false); | ||
}); | ||
}); | ||
|
||
describe("getSortedPostsData", () => { | ||
it("should return an array of sorted post data", async () => { | ||
const sortedPosts = await newsLib.getSortedPostsData(); | ||
expect(sortedPosts).toStrictEqual([ | ||
{ slug: "post2", date: "2022-01-02", title: "Post 2" }, | ||
{ slug: "post1", date: "2022-01-01", title: "Post 1" }, | ||
]); | ||
}); | ||
}); | ||
|
||
describe("getAllPostSlugs", () => { | ||
it("should return an array of all post slugs", async () => { | ||
const postSlugs = await newsLib.getAllPostSlugs(); | ||
expect(postSlugs).toStrictEqual([ | ||
{ params: { slug: "post1" } }, | ||
{ params: { slug: "post2" } }, | ||
]); | ||
}); | ||
}); | ||
|
||
describe("getPostData", () => { | ||
it("should return the data of a specific post", async () => { | ||
const slug = "post1"; | ||
const postData = await newsLib.getPostData(slug); | ||
expect(postData).toBeDefined(); | ||
expect(postData.date.toString()).toBe("2022-01-01"); | ||
expect(postData.slug).toBe(slug); | ||
expect(postData.contentHtml).toBe("<p>Mocked HTML content</p>"); | ||
}); | ||
|
||
it("should throw an error if the post does not exist", async () => { | ||
const slug = "non-existent-post"; | ||
await expect(newsLib.getPostData(slug)).rejects.toThrow(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { unified } from "unified"; | ||
import remarkParse from "remark-parse"; | ||
import remarkRehype from "remark-rehype"; | ||
import rehypeStringify from "rehype-stringify"; | ||
import rehypeRaw from "rehype-raw"; | ||
import remarkGfm from "remark-gfm"; | ||
|
||
export const processMarkdown = (content: string) => { | ||
return unified() | ||
.use(remarkParse) | ||
.use(remarkGfm) | ||
.use(remarkRehype, { allowDangerousHtml: true }) | ||
.use(rehypeRaw) | ||
.use(rehypeStringify) | ||
.process(content); | ||
}; | ||
|
||
export const processMarkdownAsHTML = async (content: string) => { | ||
return (await processMarkdown(content)).toString(); | ||
}; |