Skip to content

Commit

Permalink
Added jest with simple tests
Browse files Browse the repository at this point in the history
  • Loading branch information
denisoed committed Mar 24, 2022
1 parent 0fa2e61 commit 5c5db92
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 17 deletions.
16 changes: 16 additions & 0 deletions __tests__/orthography/orthographyEditor.test.ts
@@ -0,0 +1,16 @@
import { OrthographyEditor } from '../../src/orthography/orthographyEditor';

const _OrthographyEditor = new OrthographyEditor(null, null);

describe('OrthographyEditor', () => {
it('should be defined', () => {
expect(OrthographyEditor).toBeDefined();
});

describe('getColRow', () => {
it('if not provide editor and originalWord args will return undefined', () => {
const result = _OrthographyEditor.getColRow(undefined, undefined);
expect(result).toBeUndefined();
});
});
});
6 changes: 6 additions & 0 deletions babel.config.js
@@ -0,0 +1,6 @@
module.exports = {
presets: [
['@babel/preset-env', { targets: { node: 'current' } }],
'@babel/preset-typescript'
]
};
11 changes: 11 additions & 0 deletions jest.config.ts
@@ -0,0 +1,11 @@
/*
* For a detailed explanation regarding each configuration property, visit:
* https://jestjs.io/docs/configuration
*/

module.exports = {
// A map from regular expressions to paths to transformers
transform: {
'^.+\\.[t|j]sx?$': 'babel-jest'
}
};
7 changes: 7 additions & 0 deletions package.json
Expand Up @@ -11,6 +11,7 @@
"dev": "rollup --config rollup.config.js -w",
"build": "npm run lint && rollup --config rollup.config.js",
"lint": "eslint '*/**/*.{js,ts}'",
"test": "jest",
"format": "prettier --write \"src/**/*.ts\""
},
"keywords": [
Expand All @@ -21,19 +22,25 @@
"author": "Denisoed",
"license": "MIT",
"devDependencies": {
"@babel/preset-env": "^7.16.11",
"@babel/preset-typescript": "^7.16.7",
"@rollup/plugin-commonjs": "^15.1.0",
"@rollup/plugin-node-resolve": "^9.0.0",
"@rollup/plugin-typescript": "^6.1.0",
"@types/jest": "^27.4.1",
"@types/node": "^14.14.2",
"@typescript-eslint/eslint-plugin": "^4.13.0",
"@typescript-eslint/parser": "^4.13.0",
"babel-jest": "^27.5.1",
"eslint": "^7.18.0",
"eslint-config-prettier": "^7.1.0",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-prettier": "^3.3.1",
"jest": "^27.5.1",
"obsidian": "https://github.com/obsidianmd/obsidian-api/tarball/master",
"prettier": "^2.2.1",
"rollup": "^2.32.1",
"ts-jest": "^27.1.3",
"tslib": "^2.0.3",
"typescript": "^4.0.3"
}
Expand Down
16 changes: 16 additions & 0 deletions src/interfaces.ts
Expand Up @@ -20,3 +20,19 @@ export interface IOriginalWord {
end: number;
len: number;
}

export interface IEditor {
eachLine(callback: any): void;
markText(
from: { line: number; ch: number },
to: { line: number; ch: number },
oprions: any
): void;
getDoc(): {
replaceRange(
newText: string,
from: { line: number; ch: number },
to: { line: number; ch: number }
): void;
};
}
18 changes: 11 additions & 7 deletions src/orthography/orthographyEditor.ts
@@ -1,7 +1,7 @@
import { OrthographySettings } from '../settings';
import type { App } from 'obsidian';
import { O_HIGHLIGHT } from '../cssClasses';
import { IOriginalWord, IData } from 'src/interfaces';
import { IOriginalWord, IData, IEditor } from 'src/interfaces';

interface IOrthographyEditor {
init(): void;
Expand All @@ -27,7 +27,7 @@ export class OrthographyEditor implements IOrthographyEditor {
self.clearHighlightWords();
}

public highlightWords(editor: any, alerts: IData[]): void {
public highlightWords(editor: IEditor, alerts: IData[]): void {
this.clearHighlightWords();

alerts.forEach((alert: any) => {
Expand All @@ -42,7 +42,7 @@ export class OrthographyEditor implements IOrthographyEditor {
}

private highlightWord(
editor: any,
editor: IEditor,
originalWord: { begin: number; end: number; len: number }
): void {
const colRow = this.getColRow(editor, originalWord);
Expand All @@ -64,7 +64,7 @@ export class OrthographyEditor implements IOrthographyEditor {
}

public replaceWord(
editor: any,
editor: IEditor,
originalWord: IOriginalWord,
newWord: string
): void {
Expand All @@ -87,15 +87,19 @@ export class OrthographyEditor implements IOrthographyEditor {
doc.replaceRange(newWord, from, to);
}

private getColRow(
editor: any,
getColRow(
editor: IEditor,
originalWord: IOriginalWord
): { col: number; row: number } {
if (!editor || !originalWord) return;

let ttl = 0;
let row = 0;
let result = null;
let result;
const { begin } = originalWord;

if (!editor.eachLine) return undefined;

editor.eachLine((l: any) => {
const s = ttl === 0 ? ttl : ttl + 1;
const lineTextLength = l.text.length;
Expand Down
14 changes: 4 additions & 10 deletions tsconfig.json
Expand Up @@ -4,19 +4,13 @@
"inlineSourceMap": true,
"inlineSources": true,
"module": "ESNext",
"target": "es5",
"target": "es6",
"allowJs": true,
"noImplicitAny": true,
"moduleResolution": "node",
"importHelpers": true,
"lib": [
"dom",
"es5",
"scripthost",
"es2015"
]
"allowSyntheticDefaultImports": true,
"lib": ["dom", "es5", "scripthost", "es2015"]
},
"include": [
"**/*.ts"
]
"include": ["**/*.ts"]
}

0 comments on commit 5c5db92

Please sign in to comment.