-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rss: add tests to check against generated XML
- Loading branch information
Showing
5 changed files
with
74 additions
and
0 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
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 rss from "@astrojs/rss"; | ||
import { getCollection } from "astro:content"; | ||
import type { APIContext } from "astro"; | ||
|
||
export async function GET(context: APIContext) { | ||
const posts = await getCollection("posts"); | ||
return rss({ | ||
title: "Ladybird Browser Posts", | ||
description: "Ladybird is a brand-new browser & web engine", | ||
site: context.site!, | ||
items: posts.map((post) => ({ | ||
title: post.data.title, | ||
description: post.data.description, | ||
author: post.data.author, | ||
pubDate: post.data.date, | ||
link: `/posts/${post.slug}`, | ||
})), | ||
trailingSlash: false, | ||
}); | ||
} |
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,45 @@ | ||
import { test, expect, describe } from "bun:test"; | ||
import { XMLParser } from "fast-xml-parser"; | ||
import fs from "fs"; | ||
import path from "path"; | ||
|
||
const rootDir: string = path.join(__dirname, "../"); | ||
|
||
describe("RSS Feeds", () => { | ||
const srcDir = path.join(rootDir, "/src/content/posts"); | ||
const distDir = path.join(rootDir, "/dist/"); | ||
|
||
const mdFiles = fs | ||
.readdirSync(srcDir) | ||
.filter((file) => file.endsWith(".mdx")); | ||
const xmlFile = fs.readFileSync(path.join(distDir, "posts.xml")); | ||
|
||
const parser = new XMLParser(); | ||
const parsedXML = parser.parse(xmlFile); | ||
|
||
test("RSS Channel includes overall metadata", () => { | ||
expect(parsedXML.rss.channel).toEqual( | ||
expect.objectContaining({ | ||
title: "Ladybird Browser Posts", | ||
description: "Ladybird is a brand-new browser & web engine", | ||
link: "https://ladybird.org", | ||
}) | ||
); | ||
}); | ||
|
||
test("XML should contain entries for every post", async () => { | ||
expect(parsedXML.rss.channel.item).toBeArrayOfSize(mdFiles.length); | ||
parsedXML.rss.channel.item.forEach((item: any) => { | ||
const itemAttributes = Object.keys(item); | ||
expect(itemAttributes).toEqual( | ||
expect.arrayContaining([ | ||
"title", | ||
"link", | ||
"description", | ||
"pubDate", | ||
"author", | ||
]) | ||
); | ||
}); | ||
}); | ||
}); |