-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[lib][xl]: Add tests for computed fields and document types, Fix dupl…
…icate tags issue (#108) * Throw error when the schema is incorrect * Fix duplicate body tag, Fix linting error * Fix the library incorrectly stringifying strings * Update tags test for duplicated body tags * Add tests for document types * Update test * add test for computed fields * remove console.log * Add changeset * Remove file * Fix test error
- Loading branch information
1 parent
1ea02a1
commit ea9cc83
Showing
9 changed files
with
379 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--- | ||
"mddb": patch | ||
--- | ||
|
||
- Add tests for document types | ||
- Fix throwing an error when the document type is incorrect | ||
- Fix linting error | ||
- Fix a strange duplicated body tags issue | ||
- Fix the library incorrectly stringifying strings | ||
- Add tests for computed fields |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,286 @@ | ||
import { FileInfo, processFile } from "../lib/process"; | ||
import Path from "path"; | ||
import { Root, Content } from "mdast"; | ||
|
||
type MyContent = Content & { | ||
children?: { value: string }[]; | ||
}; | ||
|
||
describe("Can parse a file and get file info", () => { | ||
const pathToContentFixture = "__mocks__/content"; | ||
const filePath = "index.mdx"; | ||
const fullPath = Path.join(pathToContentFixture, filePath); | ||
test("Can get some values from AST and Add it to fileInfo", async () => { | ||
const fileInfo = processFile( | ||
pathToContentFixture, | ||
fullPath, | ||
(filePath) => filePath, | ||
[], | ||
[ | ||
(fileInfo: FileInfo, ast: Root) => { | ||
const headingNode = ast.children.filter((child) => { | ||
return child.type === "heading" && child.depth === 1; | ||
}) as MyContent[]; | ||
fileInfo.title = headingNode[0]?.children[0]?.value; | ||
}, | ||
] | ||
); | ||
|
||
expect(fileInfo.file_path).toBe(fullPath); | ||
expect(fileInfo.url_path).toBe("index.mdx"); | ||
expect(fileInfo.title).toBe("Welcome"); | ||
expect(fileInfo.extension).toBe("mdx"); | ||
|
||
expect(fileInfo.tags).toEqual([ | ||
"tag1", | ||
"tag2", | ||
"tag3", | ||
"日本語タグ", | ||
"标签名", | ||
"метка", | ||
"태그이름", | ||
"tag_فارسی", | ||
"Tag_avec_éèç-_öäüßñ", | ||
]); | ||
|
||
expect(fileInfo.metadata).toEqual({ | ||
title: "Homepage", | ||
tags: [ | ||
"tag1", | ||
"tag2", | ||
"tag3", | ||
"日本語タグ", | ||
"标签名", | ||
"метка", | ||
"태그이름", | ||
"tag_فارسی", | ||
"Tag_avec_éèç-_öäüßñ", | ||
], | ||
tasks: [ | ||
{ | ||
checked: false, | ||
description: "uncompleted task 2", | ||
}, | ||
{ | ||
checked: true, | ||
description: "completed task 1", | ||
}, | ||
{ | ||
checked: true, | ||
description: "completed task 2", | ||
}, | ||
], | ||
}); | ||
expect(fileInfo.links).toEqual([ | ||
{ | ||
embed: false, | ||
from: "index.mdx", | ||
internal: true, | ||
text: "link", | ||
to: "blog0.mdx", | ||
toRaw: "blog0.mdx", | ||
}, | ||
]); | ||
}); | ||
test("Can add array,object,null,undefined values throw the computed fields", () => { | ||
const fileInfo = processFile( | ||
pathToContentFixture, | ||
fullPath, | ||
(filePath) => filePath, | ||
[], | ||
[ | ||
// add homepage string to file info | ||
(fileInfo: FileInfo, ast: Root) => { | ||
fileInfo.public = { | ||
title: "Title", | ||
pageSize: 34, | ||
isLocked: false, | ||
}; | ||
}, | ||
// add pageNumber as number to fileInfo | ||
(fileInfo: FileInfo, ast: Root) => { | ||
fileInfo.Authors = ["Abdelrhiim", "Mohammed", "John"]; | ||
}, | ||
// add isLocked as boolean to fileInfo | ||
(fileInfo: FileInfo, ast: Root) => { | ||
fileInfo.matrix = null; | ||
}, | ||
// add isLocked as boolean to fileInfo | ||
(fileInfo: FileInfo, ast: Root) => { | ||
fileInfo.building = undefined; | ||
}, | ||
] | ||
); | ||
expect(fileInfo.file_path).toBe(fullPath); | ||
expect(fileInfo.url_path).toBe("index.mdx"); | ||
expect(fileInfo.public).toEqual({ | ||
title: "Title", | ||
pageSize: 34, | ||
isLocked: false, | ||
}); | ||
expect(fileInfo.Authors).toEqual(["Abdelrhiim", "Mohammed", "John"]); | ||
expect(fileInfo.matrix).toBeNull(); | ||
expect(fileInfo.building).toBeUndefined(); | ||
}); | ||
test("Can add string,number,and boolean values throw the computed fields", () => { | ||
const fileInfo = processFile( | ||
pathToContentFixture, | ||
fullPath, | ||
(filePath) => filePath, | ||
[], | ||
[ | ||
// add homepage string to file info | ||
(fileInfo: FileInfo, ast: Root) => { | ||
fileInfo.homePage = "indexFile"; | ||
}, | ||
// add pageNumber as number to fileInfo | ||
(fileInfo: FileInfo, ast: Root) => { | ||
fileInfo.pageNumber = 23; | ||
}, | ||
// add isLocked as boolean to fileInfo | ||
(fileInfo: FileInfo, ast: Root) => { | ||
fileInfo.isLocked = false; | ||
}, | ||
] | ||
); | ||
expect(fileInfo.file_path).toBe(fullPath); | ||
expect(fileInfo.url_path).toBe("index.mdx"); | ||
expect(fileInfo.homePage).toBe("indexFile"); | ||
expect(fileInfo.pageNumber).toBe(23); | ||
expect(fileInfo.isLocked).toBe(false); | ||
}); | ||
test("Can add metadata field threw the computed fields", async () => { | ||
const fileInfo = processFile( | ||
pathToContentFixture, | ||
fullPath, | ||
(filePath) => filePath, | ||
[], | ||
[ | ||
(fileInfo: FileInfo, ast: Root) => { | ||
fileInfo.metadata.AuthorName = "John Smith"; | ||
}, | ||
] | ||
); | ||
|
||
expect(fileInfo.file_path).toBe(fullPath); | ||
expect(fileInfo.url_path).toBe("index.mdx"); | ||
expect(fileInfo.extension).toBe("mdx"); | ||
|
||
expect(fileInfo.tags).toEqual([ | ||
"tag1", | ||
"tag2", | ||
"tag3", | ||
"日本語タグ", | ||
"标签名", | ||
"метка", | ||
"태그이름", | ||
"tag_فارسی", | ||
"Tag_avec_éèç-_öäüßñ", | ||
]); | ||
expect(fileInfo.metadata).toEqual({ | ||
AuthorName: "John Smith", | ||
title: "Homepage", | ||
tags: [ | ||
"tag1", | ||
"tag2", | ||
"tag3", | ||
"日本語タグ", | ||
"标签名", | ||
"метка", | ||
"태그이름", | ||
"tag_فارسی", | ||
"Tag_avec_éèç-_öäüßñ", | ||
], | ||
tasks: [ | ||
{ | ||
checked: false, | ||
description: "uncompleted task 2", | ||
}, | ||
{ | ||
checked: true, | ||
description: "completed task 1", | ||
}, | ||
{ | ||
checked: true, | ||
description: "completed task 2", | ||
}, | ||
], | ||
}); | ||
expect(fileInfo.links).toEqual([ | ||
{ | ||
embed: false, | ||
from: "index.mdx", | ||
internal: true, | ||
text: "link", | ||
to: "blog0.mdx", | ||
toRaw: "blog0.mdx", | ||
}, | ||
]); | ||
}); | ||
test("Can Edit and Delete Metadata Field threw the computed fields", () => { | ||
const fileInfo = processFile( | ||
pathToContentFixture, | ||
fullPath, | ||
(filePath) => filePath, | ||
[], | ||
[ | ||
(fileInfo: FileInfo, ast: Root) => { | ||
fileInfo.metadata.title = "Second Page"; | ||
delete fileInfo.metadata.AuthorName; | ||
}, | ||
] | ||
); | ||
expect(fileInfo.file_path).toBe(fullPath); | ||
expect(fileInfo.url_path).toBe("index.mdx"); | ||
expect(fileInfo.extension).toBe("mdx"); | ||
expect(fileInfo.tags).toEqual([ | ||
"tag1", | ||
"tag2", | ||
"tag3", | ||
"日本語タグ", | ||
"标签名", | ||
"метка", | ||
"태그이름", | ||
"tag_فارسی", | ||
"Tag_avec_éèç-_öäüßñ", | ||
]); | ||
expect(fileInfo.metadata).toEqual({ | ||
title: "Second Page", | ||
tags: [ | ||
"tag1", | ||
"tag2", | ||
"tag3", | ||
"日本語タグ", | ||
"标签名", | ||
"метка", | ||
"태그이름", | ||
"tag_فارسی", | ||
"Tag_avec_éèç-_öäüßñ", | ||
], | ||
tasks: [ | ||
{ | ||
checked: false, | ||
description: "uncompleted task 2", | ||
}, | ||
{ | ||
checked: true, | ||
description: "completed task 1", | ||
}, | ||
{ | ||
checked: true, | ||
description: "completed task 2", | ||
}, | ||
], | ||
}); | ||
expect(fileInfo.links).toEqual([ | ||
{ | ||
embed: false, | ||
from: "index.mdx", | ||
internal: true, | ||
text: "link", | ||
to: "blog0.mdx", | ||
toRaw: "blog0.mdx", | ||
}, | ||
]); | ||
}); | ||
}); |
Oops, something went wrong.