generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add
tests/
and test for getting content from markdown files gh-15
For unit testing I use node native test runner
- Loading branch information
Showing
4 changed files
with
96 additions
and
6 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
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,77 @@ | ||
import { RawFile } from "@hooks/types"; | ||
import { GrindPluginSettings } from "@types"; | ||
import { getRawFiles } from "@utils/file"; | ||
import assert from "node:assert"; | ||
import fs from "node:fs"; | ||
import path from "node:path"; | ||
import { describe, it } from "node:test"; | ||
import { TFile, Vault } from "obsidian"; | ||
// @ts-expect-error: This works. | ||
const __dirname = import.meta.dirname; | ||
|
||
const settings: GrindPluginSettings | undefined = undefined; | ||
|
||
const vaultPath = "vault/"; | ||
const vaultFullPath = path.join(__dirname, vaultPath); | ||
|
||
const getMarkdownFiles = (): Array<TFile> => { | ||
const files: Array<TFile> = []; | ||
const filePaths = fs.readdirSync(vaultFullPath); | ||
|
||
filePaths.forEach((filePath) => { | ||
const filePathInVault = path.join(vaultFullPath, filePath); | ||
|
||
if (path.extname(filePathInVault) === ".md") { | ||
files.push({ | ||
path: filePath, | ||
basename: path.basename(filePathInVault), | ||
extension: ".md", | ||
} as TFile); | ||
} | ||
}); | ||
|
||
return files; | ||
}; | ||
|
||
const cachedRead = async (file: TFile): Promise<string> => { | ||
const fileContent = fs.readFileSync( | ||
path.join(vaultFullPath, file.path), | ||
"utf8", | ||
); | ||
|
||
return fileContent; | ||
}; | ||
|
||
const vault: Vault = { getMarkdownFiles, cachedRead } as Vault; | ||
|
||
describe("Markdown content", () => { | ||
it("Get file content to parse tasks", async () => { | ||
const expected: Array<RawFile> = [ | ||
{ | ||
path: "difficulty.md", | ||
content: [ | ||
"# Tasks with difficulty tags", | ||
"", | ||
"## Todo", | ||
"", | ||
"- [ ] trivial task #diff/trivial", | ||
"- [ ] easy task #diff/easy", | ||
"- [ ] medium task #diff/medium", | ||
"- [ ] hard task #diff/hard", | ||
"", | ||
"## Done", | ||
"", | ||
"- [x] trivial task #diff/trivial", | ||
"- [x] easy task #diff/easy", | ||
"- [x] medium task #diff/medium", | ||
"- [x] hard task #diff/hard", | ||
"", | ||
], | ||
}, | ||
]; | ||
|
||
const actual = await getRawFiles(vault, settings); | ||
|
||
assert.deepStrictEqual(actual, expected); | ||
}); | ||
}); |
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,15 @@ | ||
# Tasks with difficulty tags | ||
|
||
## Todo | ||
|
||
- [ ] trivial task #diff/trivial | ||
- [ ] easy task #diff/easy | ||
- [ ] medium task #diff/medium | ||
- [ ] hard task #diff/hard | ||
|
||
## Done | ||
|
||
- [x] trivial task #diff/trivial | ||
- [x] easy task #diff/easy | ||
- [x] medium task #diff/medium | ||
- [x] hard task #diff/hard |