first unit test

This commit is contained in:
samuele-cozzi 2023-03-08 08:55:37 +01:00
parent 81274b9d91
commit 886d72f0ff
8 changed files with 2961 additions and 68 deletions

2
.gitignore vendored
View file

@ -20,3 +20,5 @@ data.json
# Exclude macOS Finder (System Explorer) View States
.DS_Store
tests/coverage

14
jest.config.js Normal file
View file

@ -0,0 +1,14 @@
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
//modulePathIgnorePatterns: ["<rootDir>/docs/","<rootDir>/vault/"],
coverageDirectory: "tests/coverage",
coverageReporters: ["lcov"],
modulePaths: ['node_modules','<rootDir>'],
moduleDirectories: ['node_modules'],
moduleFileExtensions: ['js', 'mjs', 'cjs', 'jsx', 'ts', 'd.ts', 'tsx', 'json', 'node']
};

2830
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -6,20 +6,26 @@
"scripts": {
"dev": "node esbuild.config.mjs",
"build": "tsc -noEmit -skipLibCheck && node esbuild.config.mjs production",
"test": "jest --coverage --silent=false",
"test:watch": "jest --watch",
"version": "node version-bump.mjs && git add manifest.json versions.json"
},
"keywords": [],
"author": "",
"license": "MIT",
"devDependencies": {
"@types/jest": "^29.4.0",
"@types/node": "^16.11.6",
"@typescript-eslint/eslint-plugin": "5.29.0",
"@typescript-eslint/parser": "5.29.0",
"builtin-modules": "3.3.0",
"esbuild": "0.17.3",
"jest": "^29.5.0",
"normalize-path": "^3.0.0",
"obsidian": "latest",
"ts-jest": "^29.0.5",
"tslib": "2.4.0",
"typescript": "4.7.4"
"typescript": "^4.7.4"
},
"dependencies": {
"@marp-team/marp-cli": "^2.4.0",

View file

@ -1,9 +1,9 @@
import { MarkdownView, TAbstractFile, Plugin, addIcon } from 'obsidian';
import { MarkdownView, TAbstractFile, Plugin, addIcon, App, PluginSettingTab, Setting } from 'obsidian';
import { MARP_PREVIEW_VIEW, MarpPreviewView } from './views/marpPreviewView';
import { MarpExport } from './utilities/marpExport';
import { ICON_SLIDE_PREVIEW } from './utilities/icons';
import { MarpSlidesSettings, MarpSlidesSettingTab, DEFAULT_SETTINGS } from 'utilities/settings';
import { MarpSlidesSettings, DEFAULT_SETTINGS } from 'utilities/settings';
export default class MarpSlides extends Plugin {
@ -148,3 +148,45 @@ export default class MarpSlides extends Plugin {
}
}
export class MarpSlidesSettingTab extends PluginSettingTab {
private plugin: MarpSlides;
constructor(app: App, plugin: MarpSlides) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const {containerEl} = this;
containerEl.empty();
containerEl.createEl('h2', {text: 'MARP Slide Plugin - Settings'});
new Setting(containerEl)
.setName('Chrome Path')
.setDesc('Sets the custom path for Chrome or Chromium-based browser to export PDF, PPTX, and image. If it\'s empty, Marp will find out the installed Google Chrome / Chromium / Microsoft Edge.')
.addText(text => text
.setPlaceholder('Enter CHROME_PATH')
.setValue(this.plugin.settings.CHROME_PATH)
.onChange(async (value) => {
console.log('Chrome Path: ' + value);
this.plugin.settings.CHROME_PATH = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('Theme Path')
.setDesc('Local paths to additional <a href="https://marpit.marp.app/theme-css">theme CSS</a> for Marp core and Marpit framework. The rule for paths is following Markdown: Styles.')
.addText(text => text
.setPlaceholder('template\\marp\\themes')
.setValue(this.plugin.settings.ThemePath)
.onChange(async (value) => {
console.log('Theme Path: ' + value);
this.plugin.settings.ThemePath = value;
await this.plugin.saveSettings();
}));
}
}

View file

@ -1,6 +1,3 @@
import { App, PluginSettingTab, Setting } from 'obsidian';
import MarpSlides from '../main';
export interface MarpSlidesSettings {
CHROME_PATH: string;
ThemePath: string;
@ -9,46 +6,4 @@ export interface MarpSlidesSettings {
export const DEFAULT_SETTINGS: MarpSlidesSettings = {
CHROME_PATH: '',
ThemePath: ''
}
export class MarpSlidesSettingTab extends PluginSettingTab {
private plugin: MarpSlides;
constructor(app: App, plugin: MarpSlides) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const {containerEl} = this;
containerEl.empty();
containerEl.createEl('h2', {text: 'MARP Slide Plugin - Settings'});
new Setting(containerEl)
.setName('Chrome Path')
.setDesc('Sets the custom path for Chrome or Chromium-based browser to export PDF, PPTX, and image. If it\'s empty, Marp will find out the installed Google Chrome / Chromium / Microsoft Edge.')
.addText(text => text
.setPlaceholder('Enter CHROME_PATH')
.setValue(this.plugin.settings.CHROME_PATH)
.onChange(async (value) => {
console.log('Chrome Path: ' + value);
this.plugin.settings.CHROME_PATH = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('Theme Path')
.setDesc('Local paths to additional <a href="https://marpit.marp.app/theme-css">theme CSS</a> for Marp core and Marpit framework. The rule for paths is following Markdown: Styles.')
.addText(text => text
.setPlaceholder('template\\marp\\themes')
.setValue(this.plugin.settings.ThemePath)
.onChange(async (value) => {
console.log('Theme Path: ' + value);
this.plugin.settings.ThemePath = value;
await this.plugin.saveSettings();
}));
}
}

View file

@ -0,0 +1,32 @@
const normalize = require('normalize-path');
// Import this named export into your test file:
export const TFile = jest.fn().mockImplementation(() => {
return {
constructor: () => {},
path: String,
parent: () => new TFile,
vault: new Vault()
};
});
export const Vault = jest.fn().mockImplementation(() => {
return {
constructor: () => {},
adapter: new FileSystemAdapter
}
});
export const FileSystemAdapter = jest.fn().mockImplementation(() => {
let _path : string = ""
return {
constructor: () => {},
write: (path: string, data: string) => { _path = path},
getBasePath: () => { return _path; }
}
});
export const normalizePath = jest.fn().mockImplementation((str: string) => {
return normalize(str)
})

52
tests/filePath.test.ts Normal file
View file

@ -0,0 +1,52 @@
import { TFile } from 'obsidian';
import {describe, expect, test} from '@jest/globals';
import { FilePath } from "../src/utilities/filePath";
import { DEFAULT_SETTINGS } from "../src/utilities/settings";
class pathsUtility {
base: string;
relative: string;
expected:string;
}
test('file base path', () => {
const filePath = new FilePath(DEFAULT_SETTINGS);
const tests : pathsUtility[] = [
{ base: "aaa", relative: "bbb", expected: "aaa/bbb/"}//,
//{ base: "C:\\user\\foo\\vault", relative: "\\folder\\file", expected: "C:\\user\\foo\\vault\\folder\\file\\"},
];
tests.forEach(element => {
const file = new TFile;
file.parent.path = element.relative;
file.vault.adapter.write(element.base, '');
const result = filePath.getCompleteFileBasePath(file);
expect(result).toBe(element.expected);
});
});
// test('file path', () => {
// const filePath = new FilePath(DEFAULT_SETTINGS);
// const tests : pathsUtility[] = [
// { base: "aaa", relative: "bbb.md", expected: "aaa/bbb.md"},
// { base: "C:\\user\\foo\\vault", relative: "\\folder\\file.md", expected: "C:\\user\\foo\\vault\\folder\\file.md"},
// ];
// tests.forEach(element => {
// const file = new TFile;
// file.parent.path = element.relative;
// file.vault.adapter.write(element.base, '');
// const result = filePath.getCompleteFilePath(file);
// expect(result).toBe(element.expected);
// });
// });