Skip to content

Commit

Permalink
add tests/ and test for getting content from markdown files gh-15
Browse files Browse the repository at this point in the history
For unit testing I use node native test runner
  • Loading branch information
dromse committed Apr 22, 2024
1 parent 054993a commit 035808d
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 6 deletions.
3 changes: 1 addition & 2 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import tseslint from "typescript-eslint";
const __dirname = import.meta.dirname;

export default tseslint.config(
// eslint.configs.recommended,
{
plugins: {
"@typescript-eslint": tseslint.plugin,
Expand All @@ -14,7 +13,7 @@ export default tseslint.config(
tsconfigRootDir: __dirname,
},
},
files: ["src/**/*.{ts,tsx}"],
files: ["{src,tests}/**/*.{ts,tsx}"],
rules: {
"max-params": "error",
"@typescript-eslint/ban-ts-comment": [
Expand Down
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
"description": "Task management with rewards system.",
"main": "main.js",
"scripts": {
"dev": "node esbuild.config.mjs",
"dev:build": "rm -rf build && tsc -noEmit -skipLibCheck && node esbuild.config.mjs && cp manifest.json build && cat src/styles.css build/main.css > build/styles.css && rm build/main.css && touch build/.hotreload",
"build": "rm -rf build && tsc -noEmit -skipLibCheck && node esbuild.config.mjs production && cp manifest.json build && minify src/styles.css build/main.css > build/styles.css && rm build/main.css",
"esbuild": "esbuild main.js --bundle --outfile=build/main.js --format=cjs --external:obsidian",
"version": "node version-bump.mjs && git add manifest.json versions.json",
"watch": "nodemon --ignore build/ -e ts,tsx,css --exec \"pnpm build\" ",
"docs": "typedoc",
"eslint": "eslint"
"lint": "eslint",
"test": "node --import tsx --test tests/**/*.test.ts"
},
"keywords": [],
"author": "dromse",
Expand All @@ -31,6 +29,7 @@
"nodemon": "^3.1.0",
"obsidian": "latest",
"tslib": "2.4.0",
"tsx": "^4.7.2",
"typedoc": "^0.25.12",
"typedoc-plugin-markdown": "^3.17.1",
"typescript": "4.7.4",
Expand Down
77 changes: 77 additions & 0 deletions tests/file.test.ts
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);
});
});
15 changes: 15 additions & 0 deletions tests/vault/difficulty.md
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

0 comments on commit 035808d

Please sign in to comment.