more tests

This commit is contained in:
2024-08-21 15:04:28 -06:00
parent 0d79299072
commit 7bb15126b6
4 changed files with 62 additions and 0 deletions

View File

@@ -0,0 +1,4 @@
export interface IModuleItem {
name: string;
dueAt: string;
}

View File

@@ -0,0 +1,7 @@
import { IModuleItem } from "../IModuleItem";
export interface LocalCoursePage extends IModuleItem {
name: string;
text: string;
dueAt: string;
}

View File

@@ -0,0 +1,32 @@
import { extractLabelValue } from "../assignmnet/utils/markdownUtils";
import { LocalCoursePage } from "./localCoursePage";
export const pageMarkdown = {
toMarkdown: (page: LocalCoursePage) => {
const printableDueDate = new Date(page.dueAt)
.toISOString()
.replace("\u202F", " ");
const settingsMarkdown = `Name: ${page.name}\nDueDateForOrdering: ${printableDueDate}\n---\n`;
return settingsMarkdown + page.text;
},
parseMarkdown: (pageMarkdown: string) => {
const rawSettings = pageMarkdown.split("---")[0];
const name = extractLabelValue(rawSettings, "Name");
const rawDate = extractLabelValue(rawSettings, "DueDateForOrdering");
const parsedDate = new Date(rawDate);
if (isNaN(parsedDate.getTime())) {
throw new Error(`could not parse due date: ${rawDate}`);
}
const text = pageMarkdown.split("---\n")[1];
const page: LocalCoursePage = {
name,
dueAt: parsedDate.toISOString(),
text,
};
return page;
},
};

View File

@@ -0,0 +1,19 @@
import { describe, it, expect } from "vitest";
import { LocalCoursePage } from "../../page/localCoursePage";
import { pageMarkdown } from "../../page/pageMarkdown";
describe("PageMarkdownTests", () => {
it("can parse page", () => {
const page: LocalCoursePage = {
name: "test title",
text: "test text content",
dueAt: new Date().toISOString(),
};
const pageMarkdownString = pageMarkdown.toMarkdown(page);
const parsedPage = pageMarkdown.parseMarkdown(pageMarkdownString);
expect(parsedPage).toEqual(page);
});
});