Skip to content

Commit

Permalink
Add tests for processFile function
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamedsalem401 committed Nov 15, 2023
1 parent e2b9d19 commit e56be54
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 34 deletions.
4 changes: 3 additions & 1 deletion __mocks__/content/index.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
---
title: Homepage
tags: tag1, tag2, tag3
---

# Welcome
# Welcome
[link](blog0.mdx)
6 changes: 0 additions & 6 deletions __mocks__/content/news/index.md

This file was deleted.

4 changes: 2 additions & 2 deletions src/lib/indexFolderToObjects.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getUniqueValues, recursiveWalkDir } from "../utils/index.js";
import type { WikiLink } from "../utils/index.js";
import { extractFileSchemeFromObject } from "../utils/extractFileSchemeFromObject.js";
import { readLocalMarkdownFileToObject } from "./readLocalMarkdownFileToObject.js";
import { processFile } from "./processFile.js";
import type { File, FileTag, Link, Tag } from "./types/schemaTypes.js";

export function indexFolderToObjects(
Expand All @@ -19,7 +19,7 @@ export function indexFolderToObjects(
);

for (const filePath of filteredFilePathsToIndex) {
const fileObject = readLocalMarkdownFileToObject(
const fileObject = processFile(
folderPath,
filePath,
filePathsToIndex,
Expand Down
28 changes: 28 additions & 0 deletions src/lib/processFile.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import path from "path";
import { processFile } from "./processFile";

describe("Can parse a file and get file info", () => {
const pathToContentFixture = "__mocks__/content";

test("can parse a file", async () => {
const filePath = "index.mdx";
const fullPath = path.join(pathToContentFixture, filePath);
const fileInfo = processFile(
fullPath,
pathToContentFixture,
[],
(filePath: string) => filePath
);
console.log(fileInfo);
expect(fileInfo.file_path).toBe(fullPath);
expect(fileInfo.extension).toBe("mdx");
expect(fileInfo.tags).toEqual(["tag1", "tag2", "tag3"]);
expect(fileInfo.metadata).toEqual({
title: "Homepage",
tags: ["tag1", "tag2", "tag3"],
});
expect(fileInfo.links).toEqual([
{ linkSrc: "blog0.mdx", linkType: "normal" },
]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ import fs from "fs";
import path from "path";
import { parseMarkdownContent } from "../utils/index.js";
import type { FileObject } from "./types/FileObject.js";
import { MddbFile } from "./schema.js";
import { generateFileIdFromPath } from "../utils/index.js";
import { getFileExtensionFromPath } from "../utils/index.js";
import crypto from "crypto";

export function readLocalMarkdownFileToObject(
folderPath: string,
export function processFile(
filePath: string,
folderPath: string,
filePathsToIndex: string[],
pathToUrlResolver: (filePath: string) => string
): FileObject {
const id = generateFileIdFromPath(filePath);
const extension = getFileExtensionFromPath(filePath);
const encodedPath = Buffer.from(filePath, "utf-8").toString();
const id = crypto.createHash("sha1").update(encodedPath).digest("hex");
const extension = path.extname(filePath).slice(1);

const fileObject: FileObject = {
_id: id,
file_path: filePath,
Expand All @@ -26,9 +26,8 @@ export function readLocalMarkdownFileToObject(
};

// if not md or mdx return this
const isExtensionNotSupported =
!MddbFile.supportedExtensions.includes(extension);
if (isExtensionNotSupported) {
const isExtensionSupported = ["md", "mdx"].includes(extension);
if (!isExtensionSupported) {
return fileObject;
}

Expand Down
8 changes: 0 additions & 8 deletions src/utils/generateFileIdFromPath.ts

This file was deleted.

4 changes: 0 additions & 4 deletions src/utils/getFileExtensionFromPath.ts

This file was deleted.

2 changes: 0 additions & 2 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,5 @@ export { recursiveWalkDir } from "./recursiveWalkDir.js";
export { extractWikiLinks, WikiLink } from "./extractWikiLinks.js";
export { parseMarkdownContent } from "./parseMarkdownContent.js";
export { getUniqueValues } from "./getUniqueValues.js";
export { generateFileIdFromPath } from "./generateFileIdFromPath.js";
export { getFileExtensionFromPath } from "./getFileExtensionFromPath.js";
export { defaultFilePathToUrl } from "./defaultFilePathToUrl.js";
export { extractFileSchemeFromObject } from "./extractFileSchemeFromObject.js";
3 changes: 2 additions & 1 deletion tsconfig.spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"src/**/*.spec.js",
"src/**/*.test.jsx",
"src/**/*.spec.jsx",
"src/**/*.d.ts"
"src/**/*.d.ts",
"src/lib/processFile.spec.ts"
]
}

0 comments on commit e56be54

Please sign in to comment.