Skip to content

Commit

Permalink
Merge pull request #62 from datopian/update-tests
Browse files Browse the repository at this point in the history
Update tests
  • Loading branch information
mohamedsalem401 authored Nov 22, 2023
2 parents 826ed81 + c313c36 commit 7d73442
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 15 deletions.
9 changes: 8 additions & 1 deletion src/lib/process.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,14 @@ describe("Can parse a file and get file info", () => {
tags: ["tag1", "tag2", "tag3"],
});
expect(fileInfo.links).toEqual([
{ linkSrc: "blog0.mdx", linkType: "normal" },
{
embed: true,
from: "index.mdx",
internal: true,
text: "link",
to: "blog0.mdx",
toRaw: "blog0.mdx",
},
]);
});
});
2 changes: 1 addition & 1 deletion src/lib/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export function processFile(

fileInfo.url_path = pathToUrlResolver(relativePath);
fileInfo.metadata = metadata;
fileInfo.links = links.map((link) => ({ ...link, from: relativePath }));
fileInfo.links = links;

const filetype = metadata?.type || null;
fileInfo.filetype = filetype;
Expand Down
11 changes: 7 additions & 4 deletions src/utils/databaseUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,14 @@ export function mapLinksToInsert(filesToInsert: File[], file: any) {
}

function findFileToInsert(filesToInsert: File[], filePath: string) {
const normalizedFilePath = path.normalize(filePath);
const filePathWithoutExt = path.join(
path.dirname(filePath),
path.basename(filePath, path.extname(filePath))
);

return filesToInsert.find(({ file_path }) => {
const normalizedFile = path.normalize(file_path);
return normalizedFile === normalizedFilePath;
return filesToInsert.find(({ url_path }) => {
const normalizedFile = path.normalize(url_path || "");
return normalizedFile === filePathWithoutExt;
});
}

Expand Down
5 changes: 4 additions & 1 deletion src/utils/extractWikiLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,13 @@ const extractWikiLinks = (
linkSrc = node.data.permalink;
text = node.children?.[0]?.value || "";
}
const to = !linkSrc.startsWith("http")
? path.posix.join(directory, linkSrc)
: linkSrc;

return {
from: from,
to: linkSrc,
to: to,
toRaw: linkSrc,
text,
embed: linkType === "embed",
Expand Down
72 changes: 64 additions & 8 deletions src/utils/parseFile.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,38 @@ describe("parseFile", () => {
tags: ["a", "b", "c"],
};
const expectedLinks = [
{ linkType: "normal", linkSrc: "Some Link" },
{ linkType: "normal", linkSrc: "blog/Some Other Link" },
{ linkType: "normal", linkSrc: "blog/Some Other Link" },
{ linkType: "embed", linkSrc: "Some Image.png" },
{
embed: false,
from: "",
internal: true,
text: "",
to: "Some Link",
toRaw: "Some Link",
},
{
embed: false,
from: "",
internal: true,
text: "",
to: "blog/Some Other Link",
toRaw: "blog/Some Other Link",
},
{
embed: false,
from: "",
internal: true,
text: "",
to: "blog/Some Other Link",
toRaw: "blog/Some Other Link",
},
{
embed: true,
from: "",
internal: true,
text: "",
to: "Some Image.png",
toRaw: "Some Image.png",
},
];
const { metadata, links } = parseFile(source);
expect(metadata).toEqual(expectedMetadata);
Expand All @@ -37,10 +65,38 @@ describe("parseFile", () => {
tags: ["a", "b", "c"],
};
const expectedLinks = [
{ linkType: "normal", linkSrc: "/some/folder/Some Link" },
{ linkType: "normal", linkSrc: "/some/folder/blog/Some Other Link" },
{ linkType: "normal", linkSrc: "/some/folder/blog/Some Other Link" },
{ linkType: "embed", linkSrc: "/some/folder/Some Image.png" },
{
embed: false,
from: "",
internal: true,
text: "",
to: "some/folder/Some Link",
toRaw: "/some/folder/Some Link",
},
{
embed: false,
from: "",
internal: true,
text: "",
to: "some/folder/blog/Some Other Link",
toRaw: "/some/folder/blog/Some Other Link",
},
{
embed: false,
from: "",
internal: true,
text: "",
to: "some/folder/blog/Some Other Link",
toRaw: "/some/folder/blog/Some Other Link",
},
{
embed: true,
from: "",
internal: true,
text: "",
to: "some/folder/Some Image.png",
toRaw: "/some/folder/Some Image.png",
},
];
const permalinks = [
"/some/folder/Some Link",
Expand Down

0 comments on commit 7d73442

Please sign in to comment.