Skip to content

Commit

Permalink
refactor: split into monorepo
Browse files Browse the repository at this point in the history
  • Loading branch information
waynevanson committed Aug 20, 2023
1 parent f53d72e commit 3c7ce0f
Show file tree
Hide file tree
Showing 19 changed files with 184 additions and 132,604 deletions.
132,509 changes: 0 additions & 132,509 deletions .vault/.obsidian/plugins/data-entry/main.js

This file was deleted.

1 change: 1 addition & 0 deletions .vault/.obsidian/plugins/data-entry/main.js
13 changes: 0 additions & 13 deletions .vault/.obsidian/plugins/data-entry/manifest.json

This file was deleted.

1 change: 1 addition & 0 deletions .vault/.obsidian/plugins/data-entry/manifest.json
14 changes: 11 additions & 3 deletions .vault/.obsidian/workspace.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@
"id": "31fac1ab6a60f1aa",
"type": "leaf",
"state": {
"type": "empty",
"state": {}
"type": "markdown",
"state": {
"file": "Yello world.md",
"mode": "preview",
"source": true
}
}
}
]
Expand Down Expand Up @@ -81,6 +85,7 @@
"state": {
"type": "backlink",
"state": {
"file": "Yello world.md",
"collapseAll": false,
"extraContext": false,
"sortOrder": "alphabetical",
Expand All @@ -97,6 +102,7 @@
"state": {
"type": "outgoing-link",
"state": {
"file": "Yello world.md",
"linksCollapsed": false,
"unlinkedCollapsed": true
}
Expand All @@ -118,7 +124,9 @@
"type": "leaf",
"state": {
"type": "outline",
"state": {}
"state": {
"file": "Yello world.md"
}
}
}
],
Expand Down
1 change: 1 addition & 0 deletions packages/data-entry/__mocks__/obsidian.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from '@waynevanson/obsidian-mocks';
2 changes: 0 additions & 2 deletions packages/data-entry/__mocks__/obsidian/index.ts

This file was deleted.

8 changes: 0 additions & 8 deletions packages/data-entry/__mocks__/obsidian/override.d.ts

This file was deleted.

53 changes: 0 additions & 53 deletions packages/data-entry/__mocks__/obsidian/vault/index.ts

This file was deleted.

1 change: 1 addition & 0 deletions packages/data-entry/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"jest-environment-jsdom": "^29.6.2",
"jest-image-snapshot": "^6.2.0",
"obsidian": "latest",
"@waynevanson/obsidian-mocks": "workspace:../obsidian-mocks",
"semantic-release": "^21.0.7",
"ts-jest": "^29.1.1",
"ts-node": "^10.9.1",
Expand Down
4 changes: 3 additions & 1 deletion packages/data-entry/src/hooks/datasource/file.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ describe(useFile, () => {
};
const mocks = createMocks({
vault: {
[file.path]: file.contents,
root: {
[file.path]: file.contents,
},
},
});
const hooked = rtl.renderHook(() => useFile(mocks.vault, file.path));
Expand Down
11 changes: 4 additions & 7 deletions packages/data-entry/src/test-utils/mocks.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
import { Theme } from '@mui/material';
import { MainProps as ApplicationContext } from '../components';
import { Vault } from 'obsidian';
import { Directory } from '../../__mocks__/obsidian/vault/data-adapter';
import { MockVaultParams, Vault, createVaultMock } from 'obsidian';
import { Directory } from '@waynevanson/obsidian-mocks/dist/vault/data-adapter';

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface MockOptions {
application?: Partial<Omit<ApplicationContext, 'vault'>> & {
vault: Directory;
};
}

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface MockResult {
vault: Vault;
}

export interface MockContextOptions {
vault: Directory;
vault?: MockVaultParams;
}

export interface MockContextResult {
Expand All @@ -25,6 +23,5 @@ export interface MockContextResult {
}

export const createMocks = (options?: MockContextOptions): MockResult => ({
//@ts-expect-error
vault: new Vault(options?.vault),
vault: createVaultMock(options?.vault ?? { root: {} }),
});
3 changes: 2 additions & 1 deletion packages/data-entry/src/test-utils/rtl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { MockOptions, MockResult, createMocks } from './mocks';
import { ThemeProvider, createTheme } from '@mui/material';
import deepmerge from 'deepmerge';

//todo-add types here
export interface RenderOptions<
Q extends rtl.Queries = typeof rtl.queries,
Container extends Element | DocumentFragment = HTMLElement,
Expand Down Expand Up @@ -46,7 +47,7 @@ export function render<
{} as never,
options?.application ?? {},
);
const mocks = createMocks({ vault: options?.application?.vault ?? {} });
const mocks = createMocks({ vault: { root: vaultOptions } });
const application = { ...most, vault: mocks.vault };

const wrapper: React.JSXElementConstructor<{
Expand Down
1 change: 1 addition & 0 deletions packages/obsidian-mocks/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/dist
17 changes: 16 additions & 1 deletion packages/obsidian-mocks/package.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
{
"name": "@waynevanson/obsidian-mocks"
"name": "@waynevanson/obsidian-mocks",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"type": "commonjs",
"scripts": {
"build": "tsc"
},
"devDependencies": {
"@types/jest": "^29.5.3",
"@types/node": "^18.17.3",
"jest": "^29.6.2",
"obsidian": "^1.4.0",
"ts-jest": "^29.1.1",
"ts-node": "^10.9.1",
"typescript": "4.7.4"
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DataAdapter } from 'obsidian';
import type { DataAdapter } from 'obsidian';

export type FileConstructorParams = string;

Expand All @@ -10,6 +10,8 @@ export interface Directory extends Record<string, Item> {}
export function createDataAdapter(
system: Directory,
): DataAdapter & { existsSync: (path: string) => boolean } {
// todo - add time
const now = Date.now();
const cache: Record<string, string> = {};
const split: Array<string> = [];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class Events {
callback: (...data: Array<unknown>) => void,
ctx?: unknown,
): EventRef {
const ref: EventRef = { name, callback };
const ref: EventRef = { mockName: name, mockCallback: callback };

const handlers = this.handlersByName.has(name)
? this.handlersByName.get(name)!
Expand All @@ -34,7 +34,7 @@ export class Events {
}

offref(ref: EventRef): void {
this.off(ref.name, ref.callback);
this.off(ref.mockName, ref.mockCallback);
}

trigger(name: string, ...data: Array<unknown>): void {
Expand Down
18 changes: 18 additions & 0 deletions packages/obsidian-mocks/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export * from './events';
export * from './vault';

import 'obsidian';
import { Directory } from './data-adapter';

declare module 'obsidian' {
export interface EventRef {
mockName: string;
mockCallback: (...data: Array<unknown>) => void;
}

export interface MockVaultParams {
root: Directory;
}

export function createVaultMock(params: MockVaultParams): Vault;
}
78 changes: 78 additions & 0 deletions packages/obsidian-mocks/src/vault.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { Events } from './events';
import { createDataAdapter } from './data-adapter';
import type { DataAdapter, MockVaultParams } from 'obsidian';

export function createVaultMock(params: MockVaultParams): Vault {
return new Vault(params);
}

export class Vault extends Events {
public adapter: ReturnType<typeof createDataAdapter> & DataAdapter;

constructor(params: MockVaultParams) {
super();
this.adapter = createDataAdapter(params.root);
}

getAbstractFileByPath(this: this, path: string): TAbstractFile | null {
const exists = this.adapter.existsSync(path);
if (!exists) return null;
else {
return new TFile({
name: path,
path,
vault: this,
parent: null,
});
}
}

async read(this: this, file: TFile) {
return this.adapter.read(file.path);
}
}

type Properties<T> = { [P in keyof T]: T[P] };

export abstract class TAbstractFile {
vault!: Vault;
path!: string;
name!: string;
parent!: TFolder | null;

constructor(properties: Properties<TAbstractFile>) {
Object.assign(this, properties);
}
}

export class TFolder extends TAbstractFile {
children!: TAbstractFile[];

constructor(properties: Properties<TFolder>) {
super(properties);
Object.assign(this, properties);
}

isRoot(): boolean {
return this.parent == null;
}
}

export class TFile extends TAbstractFile {
constructor(properties: Properties<TFile>) {
super(properties);
Object.assign(this, properties);
}

// get basename(): string {
// return '';
// }

// get stat(): FileStats {
// return;
// }

// get extension(): string {
// return;
// }
}
13 changes: 13 additions & 0 deletions packages/obsidian-mocks/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"strict": true,
"module": "commonjs",
"moduleResolution": "node",
"target": "es6",
"strictNullChecks": true,
"outDir": "dist",
"declaration": true,
"esModuleInterop": true
},
"include": ["src"]
}
Loading

0 comments on commit 3c7ce0f

Please sign in to comment.