Skip to content

Commit 97024c4

Browse files
committed
Commit prior refactorings
- tests
1 parent 56d15e7 commit 97024c4

19 files changed

Lines changed: 368 additions & 128 deletions

__mocks__/math.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import './jquery-easyui/jquery.easyui.min'; // ADDED for unit test
2+
import { it, expect, describe, vi, beforeEach, afterEach } from 'vitest';
3+
import { IMath } from '../tests/interfaces';
4+
import { container } from '../tests/container';
5+
6+
/* DID NOT WORK as MOCK
7+
*/
8+
export class MathFormulae implements IMath {
9+
constructor(p1, p2, p3, p4) {
10+
console.info('Here in mocked ctor');
11+
throw Error('Messager is not defined');
12+
}
13+
14+
equipWithInteractivity(a: any, javascript: boolean) { }
15+
equipWithTooltip(selector: any, text: string, javascript: boolean) { }
16+
inplaceUpdate(selector: any, javascript: boolean) { }
17+
insert(b: any) { }
18+
insertMath(text: string, element: any, multiple: boolean, displayMode: boolean) { };
19+
codeMirror: any { return { }; }
20+
setFocus() { }
21+
updateAnchor(a: any) { }
22+
updateHeaders(selector: string) { }
23+
updateLatexMenu() { }
24+
updateOutput() { }
25+
updateTables() : Promise<void> { return new Promise(); }
26+
}
27+
28+
// This helps to import symbols in test suite
29+
try {
30+
module.exports = { MathFormulae };
31+
} catch(e) { }

__mocks__/messager.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { IMessager } from '@/js/interfaces';
2+
3+
export class Messager implements IMessager {
4+
error(msgKey: string, e: any) { }
5+
show(titleKey: string, msgKey: string, e?: any) { }
6+
}

package-lock.json

Lines changed: 94 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@
2828
"@babel/core": "^7.27.1",
2929
"@babel/preset-env": "^7.27.2",
3030
"@joplin/lib": "^2.6.3",
31+
"@rollup/plugin-inject": "^5.0.5",
3132
"@types/codemirror": "^5.60.15",
3233
"@types/jquery": "^3.5.32",
34+
"@types/jsdom": "^21.1.7",
3335
"@types/katex": "^0.16.7",
3436
"@types/node": "^18.7.13",
3537
"@types/platform": "^1.3.6",

src/assets/js/interfaces.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { KIHPanel } from './panels';
21

32
export interface IBootLoader {
43
init1() : Promise<void>;
@@ -47,7 +46,7 @@ export interface IMath {
4746
equipWithTooltip(selector: any, text: string, javascript: boolean);
4847
inplaceUpdate(selector: any, javascript: boolean);
4948
insert(b: any);
50-
insertMath(text: string, element: any, multiple: boolean, displayMode: boolean);
49+
insertMath(text: string, element: any, multiple?: boolean, displayMode?: boolean);
5150
codeMirror: any;
5251
setFocus();
5352
updateAnchor(a: any);
@@ -75,6 +74,19 @@ export interface IPanels {
7574
showWindowDI(wndId: any, id: string, ...params: any);
7675
}
7776

77+
export interface IPanel {
78+
onMove(left: number, top: number);
79+
onResize(width: number, height: number);
80+
onClose();
81+
get handlers() : any;
82+
initialise(dummy?: any) : Promise<void>;
83+
update(...params) : Promise<void>;
84+
show() : Promise<void>;
85+
resize();
86+
open();
87+
toggle();
88+
}
89+
7890
export interface ICategoriesTree {
7991
nodeSelected: any;
8092
treeChanged: any;

src/assets/js/localization.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export class Localizer implements ILocalizer {
4545
return json;
4646
} catch(e) {
4747
console.error(`Could not load language file - ${e}`);
48-
return { };
48+
throw(e);
4949
}
5050
}
5151

src/assets/js/math.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
const katex = await import('katex/dist/katex'); // This version of import is essential for mhchem
1+
import './jquery-easyui/jquery.easyui.min'; // ADDED for unit test
2+
const katex = await import('katex/dist/katex'); // This version of import is essential for mhchem
23
await import('katex/dist/contrib/mhchem');
34
const CodeMirror = (await import('codemirror')).default;
45
await import('codemirror/mode/stex/stex'); // manual recommendation
@@ -30,10 +31,10 @@ export class MathFormulae implements IMath {
3031
* Constructor.
3132
*/
3233
constructor(
33-
@inject(localizerId) localizer: ILocalizer,
34-
@inject(parametersId) parameters: any,
35-
@inject(parserId) parser: IParser,
36-
@inject(messagerId) messager: IMessager
34+
@inject(localizerId) localizer: ILocalizer|null,
35+
@inject(parametersId) parameters: any|null,
36+
@inject(parserId) parser: IParser|null,
37+
@inject(messagerId) messager: IMessager|null
3738
) {
3839
this.mathTextInput = document.getElementById('mathTextInput');
3940
this.mathVisualOutput = document.getElementById('mathVisualOutput');
@@ -62,13 +63,16 @@ export class MathFormulae implements IMath {
6263
});
6364

6465
(codeMirrorEditor as ICodeMirror).version = CodeMirror.version;
65-
let panelOptions = $('#divMathTextInput').panel('options');
66-
panelOptions.onResize = function(width: string|number, height: string|number) {
67-
try {
68-
codeMirrorEditor.setSize(width, height);
69-
codeMirrorEditor.refresh();
70-
} catch(e) { }
71-
};
66+
// PURPOSE of this clause is to overcome the exception in unit test. Mocking did not help.
67+
try {
68+
let panelOptions = $('#divMathTextInput').panel('options');
69+
panelOptions.onResize = function(width: string|number, height: string|number) {
70+
try {
71+
codeMirrorEditor.setSize(width, height);
72+
codeMirrorEditor.refresh();
73+
} catch(e) { }
74+
};
75+
} catch(e) { console.warn(`Could not apply 'panel' : ${typeof $('#divMathTextInput').panel} : ${e}`); }
7276

7377
return codeMirrorEditor as ICodeMirror;
7478
}

src/assets/js/panels.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import { FileHandler } from './fileHandling';
33

44
import { inject, injectable, injectFromBase } from 'inversify';
55
import { ILocalizer, IParser, IMath, utilitiesId, IUtilities, categoriesTreeId, ICategoriesTree,
6-
IMessager, dynamicParametersId, panelFactoryId } from './interfaces';
6+
IMessager, dynamicParametersId, panelFactoryId, IPanel } from './interfaces';
77

88
/**
99
* The base class of all Panels, Dialogs, Windows.
1010
*/
11-
export class KIHPanel {
11+
export class KIHPanel implements IPanel {
1212
id = "";
1313
parent = null;
1414
initialised = false;

src/assets/js/versions.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
"easyuiVersion": "1.11.3",
55
"katexVersion": "0.16.22",
66
"colorPickerVersion": "23/05/2009",
7-
"build": 141
7+
"build": 146
88
}

tests/container.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Container } from 'inversify';
2+
import { ILocalizer, localizerId, IMessager, messagerId, IUtilities, utilitiesId, parametersId, IParser, parserId, IMath, mathId } from '@/js/interfaces';
3+
4+
import { ParametersProxy } from '../src/assets/js/parameters';
5+
import { Localizer } from '../src/assets/js/localization';
6+
import { Utilities } from '../src/assets/js/helpers';
7+
import { ParserExtension } from '../src/assets/js/parserExtension';
8+
import { MathFormulae } from '../src/assets/js/math';
9+
import { Messager } from '../__mocks__/messager';
10+
11+
export const container = new Container();
12+
13+
container.bind(parametersId).toDynamicValue(ParametersProxy).inSingletonScope();
14+
container.bind<ILocalizer>(localizerId).to(Localizer).inSingletonScope();
15+
container.bind<IMessager>(messagerId).to(Messager).inSingletonScope();
16+
container.bind<IUtilities>(utilitiesId).to(Utilities).inSingletonScope();
17+
container.bind<IParser>(parserId).to(ParserExtension).inSingletonScope();
18+
container.bind<IMath>(mathId).to(MathFormulae).inSingletonScope();

0 commit comments

Comments
 (0)