generated from nhcarrigan/template
b754503317
Node.js CI / Lint and Test (push) Successful in 1m43s
### Explanation _No response_ ### Issue _No response_ ### Attestations - [x] I have read and agree to the [Code of Conduct](https://docs.nhcarrigan.com/community/coc/) - [x] I have read and agree to the [Community Guidelines](https://docs.nhcarrigan.com/community/guide/). - [x] My contribution complies with the [Contributor Covenant](https://docs.nhcarrigan.com/dev/covenant/). ### Dependencies - [x] I have pinned the dependencies to a specific patch version. ### Style - [x] I have run the linter and resolved any errors. - [x] My pull request uses an appropriate title, matching the conventional commit standards. - [x] My scope of feat/fix/chore/etc. correctly matches the nature of changes in my pull request. ### Tests - [x] My contribution adds new code, and I have added tests to cover it. - [x] My contribution modifies existing code, and I have updated the tests to reflect these changes. - [x] All new and existing tests pass locally with my changes. - [x] Code coverage remains at or above the configured threshold. ### Documentation _No response_ ### Versioning _No response_ Reviewed-on: #12 Co-authored-by: Naomi Carrigan <commits@nhcarrigan.com> Co-committed-by: Naomi Carrigan <commits@nhcarrigan.com>
80 lines
3.1 KiB
TypeScript
80 lines
3.1 KiB
TypeScript
/**
|
|
* @copyright NHCarrigan
|
|
* @license Naomi's Public License
|
|
* @author Naomi Carrigan
|
|
*/
|
|
|
|
import { readFile } from "node:fs/promises";
|
|
import { readdir } from "node:fs/promises";
|
|
import { join } from "node:path";
|
|
import { describe, expect, it } from "vitest";
|
|
import { navigation } from "../src/components/navigation.ts";
|
|
import matter from "gray-matter";
|
|
|
|
type Navigation = typeof navigation;
|
|
type NavigationItem = Array<{
|
|
label: string
|
|
link: string
|
|
items?: Array<{
|
|
label: string
|
|
link: string
|
|
}>
|
|
}>
|
|
|
|
const excludedFiles = ["intro.mdx", "projects/_template.md"];
|
|
|
|
// this should recursively walk the specified directory and return a list of all files, prefixing them with nested paths.
|
|
// For example, calling walkDirectory() should return "/about/contact.md", "/about/mission.md", "/about/sustainability.md", etc.
|
|
const walkDirectory = async (directory = join(__dirname, "..", "src", "content", "docs"), prefix = ""): Promise<Array<string>> => {
|
|
const filesAndDirectories = await readdir(directory, { withFileTypes: true });
|
|
const pages = [];
|
|
for (const fileOrDirectory of filesAndDirectories) {
|
|
if (fileOrDirectory.isDirectory()) {
|
|
pages.push(...(await walkDirectory(join(directory, fileOrDirectory.name), join(prefix, fileOrDirectory.name))));
|
|
} else {
|
|
pages.push(join(prefix, fileOrDirectory.name));
|
|
}
|
|
}
|
|
return pages;
|
|
}
|
|
|
|
const flattenNavigation = (navigation: Navigation | NavigationItem): Array<{ label: string; link: string }> => {
|
|
const items: Array<{ label: string; link: string }> = [];
|
|
for (const item of navigation) {
|
|
if ("items" in item && item.items) {
|
|
items.push(...flattenNavigation(item.items as NavigationItem));
|
|
} else {
|
|
items.push({ label: item.label, link: item.link });
|
|
}
|
|
}
|
|
return items;
|
|
}
|
|
|
|
describe("navigation", () => {
|
|
it("should include all pages", async () => {
|
|
expect.hasAssertions();
|
|
let pages = await walkDirectory();
|
|
pages = pages.filter((page) => !excludedFiles.includes(page));
|
|
const flattenedNavigation = flattenNavigation(navigation);
|
|
for (const page of pages) {
|
|
const pageName = page.split(".")[0];
|
|
const navItem = flattenedNavigation.find((item) => item.link === `/${pageName}`);
|
|
expect(navItem, `Navigation item not found for page ${page}`).toBeDefined();
|
|
}
|
|
});
|
|
|
|
it("should use page titles as navigation item labels", async () => {
|
|
expect.hasAssertions();
|
|
let pages = await walkDirectory();
|
|
pages = pages.filter((page) => !excludedFiles.includes(page));
|
|
const flattenedNavigation = flattenNavigation(navigation);
|
|
for (const page of pages) {
|
|
const pageName = page.split(".")[0];
|
|
const navItem = flattenedNavigation.find((item) => item.link === `/${pageName}`);
|
|
const pageContent = await readFile(join(__dirname, "..", "src", "content", "docs", page), "utf-8");
|
|
const { data } = matter(pageContent);
|
|
expect(navItem?.label, `Navigation item label for ${pageName} is not correct`).toBe(data.title);
|
|
}
|
|
});
|
|
});
|