Skip to content

Commit

Permalink
Merge pull request #112 from nathonius/test/107/jest
Browse files Browse the repository at this point in the history
Unit tests
  • Loading branch information
nathonius authored Apr 23, 2024
2 parents 7aa5aee + 8ef8163 commit 4ca024e
Show file tree
Hide file tree
Showing 15 changed files with 5,877 additions and 1,444 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/code-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ on:
jobs:
build:
uses: "./.github/workflows/build.yml"
lint:
check:
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
- name: Lint
- name: Lint and Test
run: |
npm install
npm run lint
npm run test
46 changes: 24 additions & 22 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
# vscode
.vscode

# Intellij
*.iml
.idea

# npm
node_modules

# Don't include the compiled main.js file in the repo.
# They should be uploaded to GitHub releases instead.
main.js

# Exclude sourcemaps
*.map

# obsidian
data.json

# Exclude macOS Finder (System Explorer) View States
.DS_Store
# vscode
.vscode

# Intellij
*.iml
.idea

# npm
node_modules

# Don't include the compiled main.js file in the repo.
# They should be uploaded to GitHub releases instead.
main.js

# Exclude sourcemaps
*.map

# obsidian
data.json

# Exclude macOS Finder (System Explorer) View States
.DS_Store

coverage/
11 changes: 11 additions & 0 deletions __mocks__/obsidian.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { AppMock } from "./obsidian/App";
import { PluginMock } from "./obsidian/Plugin";
import { ModalMock } from "./obsidian/Modal";
import { PluginSettingTabMock } from "./obsidian/PluginSettingTab";

module.exports = {
App: AppMock,
Plugin: PluginMock,
Modal: ModalMock,
PluginSettingTab: PluginSettingTabMock,
};
25 changes: 25 additions & 0 deletions __mocks__/obsidian/App.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type { App, FileManager, Keymap, MetadataCache, Scope, UserEvent, Vault, Workspace } from "obsidian";

export class AppMock implements App {
get keymap(): Keymap {
throw new Error("Not implemented.");
}
get scope(): Scope {
throw new Error("Not implemented.");
}
get workspace(): Workspace {
throw new Error("Not implemented.");
}
get vault(): Vault {
throw new Error("Not implemented.");
}
get metadataCache(): MetadataCache {
throw new Error("Not implemented.");
}
get fileManager(): FileManager {
throw new Error("Not implemented.");
}
get lastEvent(): UserEvent | null {
throw new Error("Not implemented.");
}
}
34 changes: 34 additions & 0 deletions __mocks__/obsidian/Modal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import type { App, Scope } from "obsidian";
import type { Modal } from "obsidian";

export class ModalMock implements Modal {
constructor(public app: App) {}
get scope(): Scope {
throw new Error("Not implemented.");
}
get containerEl(): HTMLElement {
throw new Error("Not implemented.");
}
get modalEl(): HTMLElement {
throw new Error("Not implemented.");
}
get titleEl(): HTMLElement {
throw new Error("Not implemented.");
}
get contentEl(): HTMLElement {
throw new Error("Not implemented.");
}
shouldRestoreSelection: boolean = false;
open(): void {
throw new Error("Method not implemented.");
}
close(): void {
throw new Error("Method not implemented.");
}
onOpen(): void {
throw new Error("Method not implemented.");
}
onClose(): void {
throw new Error("Method not implemented.");
}
}
120 changes: 120 additions & 0 deletions __mocks__/obsidian/Plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/* eslint-disable unused-imports/no-unused-vars */
/* eslint-disable @typescript-eslint/no-explicit-any */
import { jest } from "@jest/globals";
import type { Editor } from "codemirror";
import type {
App,
Command,
Component,
EditorSuggest,
EventRef,
HoverLinkSource,
ObsidianProtocolHandler,
Plugin,
PluginManifest,
ViewCreator,
} from "obsidian";

export class PluginMock implements Plugin {
public data: any = undefined;
public intervals: number[] = [];
public addSettingTab = jest.fn();
public registerEditorExtension = jest.fn();
public registerInterval = jest.fn((interval: number) => {
this.intervals.push(interval);
return interval;
});
public registerMarkdownCodeBlockProcessor = jest.fn(() => {
return jest.fn(() => Promise.resolve());
});
public registerMarkdownPostProcessor = jest.fn(() => {
return jest.fn(() => Promise.resolve());
});

constructor(
public app: App,
public manifest: PluginManifest,
) {}

addRibbonIcon(icon: string, title: string, callback: (evt: MouseEvent) => any): HTMLElement {
throw new Error("Method not implemented.");
}
addStatusBarItem(): HTMLElement {
throw new Error("Method not implemented.");
}
addCommand(command: Command): Command {
throw new Error("Method not implemented.");
}
registerView(type: string, viewCreator: ViewCreator): void {
throw new Error("Method not implemented.");
}
registerHoverLinkSource(id: string, info: HoverLinkSource): void {
throw new Error("Method not implemented.");
}
registerExtensions(extensions: string[], viewType: string): void {
throw new Error("Method not implemented.");
}

registerCodeMirror(callback: (cm: Editor) => any): void {
throw new Error("Method not implemented.");
}

registerObsidianProtocolHandler(action: string, handler: ObsidianProtocolHandler): void {
throw new Error("Method not implemented.");
}
registerEditorSuggest(editorSuggest: EditorSuggest<any>): void {
throw new Error("Method not implemented.");
}
loadData(): Promise<any> {
return Promise.resolve(this.data);
}
saveData(data: any): Promise<void> {
this.data = data;
return Promise.resolve();
}
load(): void {
throw new Error("Method not implemented.");
}
onload(): void {
throw new Error("Method not implemented.");
}
unload(): void {
throw new Error("Method not implemented.");
}
onunload(): void {
throw new Error("Method not implemented.");
}
addChild<T extends Component>(component: T): T {
throw new Error("Method not implemented.");
}
removeChild<T extends Component>(component: T): T {
throw new Error("Method not implemented.");
}
register(cb: () => any): void {
throw new Error("Method not implemented.");
}
registerEvent(eventRef: EventRef): void {
throw new Error("Method not implemented.");
}
registerDomEvent<K extends keyof WindowEventMap>(
el: Window,
type: K,
callback: (this: HTMLElement, ev: WindowEventMap[K]) => any,
options?: boolean | AddEventListenerOptions | undefined,
): void;
registerDomEvent<K extends keyof DocumentEventMap>(
el: Document,
type: K,
callback: (this: HTMLElement, ev: DocumentEventMap[K]) => any,
options?: boolean | AddEventListenerOptions | undefined,
): void;
registerDomEvent<K extends keyof HTMLElementEventMap>(
el: HTMLElement,
type: K,
callback: (this: HTMLElement, ev: HTMLElementEventMap[K]) => any,
options?: boolean | AddEventListenerOptions | undefined,
): void;
registerDomEvent(el: unknown, type: unknown, callback: unknown, options?: unknown): void {
throw new Error("Method not implemented.");
}
}
17 changes: 17 additions & 0 deletions __mocks__/obsidian/PluginSettingTab.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { App, PluginSettingTab, Plugin } from "obsidian";

export class PluginSettingTabMock implements PluginSettingTab {
constructor(
public app: App,
public plugin: Plugin,
) {}
get containerEl(): HTMLElement {
throw new Error("Not implemented.");
}
display() {
throw new Error("Method not implemented.");
}
hide() {
throw new Error("Method not implemented.");
}
}
81 changes: 81 additions & 0 deletions __mocks__/queue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import type { EventListenerOrEventListenerObject, EventsMap, QueueEvent, QueueWorker } from "queue";
import type Queue from "queue";
import type { Options } from "queue";

export default class QueueMock implements Queue {
concurrency: number;
timeout: number;
autostart: boolean;
results: any[] | null;
length: number = 0;

constructor(options: Options) {
this.concurrency = options.concurrency ?? 1;
this.timeout = options.timeout ?? -1;
this.autostart = options.autostart ?? false;
this.results = options.results ?? [];
}

push(...workers: QueueWorker[]): number {
for (const worker of workers) {
void worker();
}
return 0;
}
unshift(...workers: QueueWorker[]): number {
throw new Error("Method not implemented.");
}
splice(start: number, deleteCount?: number | undefined): Queue;
splice(start: number, deleteCount: number, ...workers: QueueWorker[]): Queue;
splice(start: unknown, deleteCount?: unknown, ...rest: unknown[]): Queue {
throw new Error("Method not implemented.");
}
pop(): QueueWorker | undefined {
throw new Error("Method not implemented.");
}
shift(): QueueWorker | undefined {
throw new Error("Method not implemented.");
}
slice(start?: number | undefined, end?: number | undefined): Queue {
throw new Error("Method not implemented.");
}
reverse(): Queue {
throw new Error("Method not implemented.");
}
indexOf(searchElement: QueueWorker, fromIndex?: number | undefined): number {
throw new Error("Method not implemented.");
}
lastIndexOf(searchElement: QueueWorker, fromIndex?: number | undefined): number {
throw new Error("Method not implemented.");
}
start(callback: (error?: Error | undefined, results?: any[] | null | undefined) => void): void;
start(): Promise<{ error?: Error | undefined; results?: any[] | null | undefined }>;
start(): void;
start(callback?: unknown): void | Promise<{ error?: Error | undefined; results?: any[] | null | undefined }> {
throw new Error("Method not implemented.");
}
stop(): void {
throw new Error("Method not implemented.");
}
end(error?: Error | undefined): void {
throw new Error("Method not implemented.");
}
addEventListener<Event extends keyof EventsMap>(
name: Event,
callback: EventListenerOrEventListenerObject<QueueEvent<Event, EventsMap[Event]>>,
options?: boolean | AddEventListenerOptions | undefined,
): void {
throw new Error("Method not implemented.");
}
dispatchEvent<Event extends keyof EventsMap>(event: QueueEvent<Event, EventsMap[Event]>): boolean {
throw new Error("Method not implemented.");
}
removeEventListener<Event extends keyof EventsMap>(
name: Event,
callback: EventListenerOrEventListenerObject<QueueEvent<Event, EventsMap[Event]>>,
options?: boolean | EventListenerOptions | undefined,
): void {
throw new Error("Method not implemented.");
}
}
13 changes: 11 additions & 2 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import eslint from "@eslint/js";
import tseslint from "typescript-eslint";
import UnusedImportsPlugin from "eslint-plugin-unused-imports";
import PrettierConfig from "eslint-config-prettier";
import JestPlugin from "eslint-plugin-jest";

export default tseslint.config(
{
ignores: ["node_modules/", "main.js", "eslint.config.mjs"],
ignores: ["node_modules/", "main.js", "esbuild.config.mjs", "eslint.config.mjs", "jest.config.mjs"],
},
eslint.configs.recommended,
tseslint.configs.eslintRecommended,
Expand All @@ -21,14 +22,15 @@ export default tseslint.config(
},
plugins: {
"unused-imports": UnusedImportsPlugin,
jest: JestPlugin,
},
rules: {
"@typescript-eslint/restrict-template-expressions": "warn",
"@typescript-eslint/no-unsafe-assignment": "warn",
"@typescript-eslint/no-unsafe-argument": "warn",
"@typescript-eslint/no-unsafe-member-access": "warn",
"@typescript-eslint/no-redundant-type-constituents": "warn",
"@typescript-eslint/unbound-method": "warn",
"@typescript-eslint/unbound-method": "off",
"@typescript-eslint/no-this-alias": "warn",
"@typescript-eslint/no-empty-function": "warn",
"@typescript-eslint/consistent-type-imports": "error",
Expand All @@ -48,4 +50,11 @@ export default tseslint.config(
},
},
PrettierConfig,
{
files: ["**/__tests__/**/*.[jt]s?(x)", "**/?(*.)+(spec|test).[tj]s?(x)"],
...JestPlugin.configs["flat/recommended"],
rules: {
"@typescript-eslint/no-floating-promises": "off",
},
},
);
Loading

0 comments on commit 4ca024e

Please sign in to comment.