Skip to content

Commit

Permalink
Better mock and fix tests (blacksmithgu#553)
Browse files Browse the repository at this point in the history
* Added 'bdd' runner

also sorted runners and added npx where was missing

* Better Vault mock + fixing tests

- moved mocks to automock location
- added event support to vault mock
- fixed the test that was broken after latest index changes
  • Loading branch information
vitaly authored Oct 17, 2021
1 parent 01d028f commit e59e1c3
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 17 deletions.
File renamed without changes.
13 changes: 8 additions & 5 deletions src/test/mocks/obsidian.ts → __mocks__/obsidian.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import EventEmitter from "events";

/** Basic obsidian abstraction for any file or folder in a vault. */
export abstract class TAbstractFile {
/**
Expand Down Expand Up @@ -44,10 +46,11 @@ export class TFolder extends TAbstractFile {
}
}

export class Vault {
/** Add an event listener to this vault. */
public on(event: string, handler: Function): any {
// TODO: Implement actual handlers; does nothing for now.
return "<mocked>";
export class Vault extends EventEmitter {
getFiles() {
return [];
}
trigger(name: string, ...data: any[]): void {
this.emit(name, ...data);
}
}
8 changes: 2 additions & 6 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
module.exports = {
preset: "ts-jest",
testEnvironment: 'jsdom',
moduleDirectories: ['node_modules', 'src'],
moduleNameMapper: {
"data/import/import-manager": "test/mocks/import-manager.ts",
"obsidian": "test/mocks/obsidian.ts"
}
testEnvironment: "jsdom",
moduleDirectories: ["node_modules", "src"],
};
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
"lib/**/*"
],
"scripts": {
"dev": "rollup --config rollup.config.js -w",
"lib": "rollup --config rollup.config.js --environment BUILD:lib",
"build": "rollup --config rollup.config.js --environment BUILD:production",
"format": "npx prettier --write src",
"bdd": "npx jest -i --watch --no-cache",
"build": "npx rollup --config rollup.config.js --environment BUILD:production",
"check-format": "npx prettier --check src",
"test": "jest"
"dev": "npx rollup --config rollup.config.js -w",
"format": "npx prettier --write src",
"lib": "npx rollup --config rollup.config.js --environment BUILD:lib",
"test": "npx jest"
},
"keywords": [],
"author": "",
Expand Down
14 changes: 13 additions & 1 deletion src/test/data/prefix-index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { PrefixIndexNode } from "data/index";
import { PrefixIndexNode, PrefixIndex } from "data/index";
import { Vault } from "obsidian";

test("Emoji Folder (Node)", () => {
let pi = new PrefixIndexNode("<root>");
Expand All @@ -8,3 +9,14 @@ test("Emoji Folder (Node)", () => {
expect(child).toBeTruthy();
expect(child?.element).toEqual("⚗️ KNOWLEDGE");
});

test("Emoji Folder (Index)", () => {
const vault = new Vault();
let index = new PrefixIndex(vault, () => {});

index.initialize();

vault.trigger("create", { path: "dataview/⚗️ KNOWLEDGE/Test.md" });

expect(index.get("dataview/⚗️ KNOWLEDGE")).toEqual(new Set(["dataview/⚗️ KNOWLEDGE/Test.md"]));
});

0 comments on commit e59e1c3

Please sign in to comment.