Skip to content

Commit

Permalink
Merge pull request #173 from TriliumNext/feat/ts-unit-and-integration…
Browse files Browse the repository at this point in the history
…-tests

feat: TS unit and integration tests
  • Loading branch information
eliandoran authored Jul 15, 2024
2 parents 653fba3 + 6a7eb9b commit eff6ca3
Show file tree
Hide file tree
Showing 31 changed files with 1,814 additions and 1,700 deletions.
17 changes: 15 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
"build-frontend-docs": "rm -rf ./docs/frontend_api && ./node_modules/.bin/jsdoc -c jsdoc-conf.json -d ./docs/frontend_api src/public/app/entities/*.js src/public/app/services/frontend_script_api.js src/public/app/widgets/basic_widget.js src/public/app/widgets/note_context_aware_widget.js src/public/app/widgets/right_panel_widget.js",
"build-docs": "npm run build-backend-docs && npm run build-frontend-docs",
"webpack": "webpack -c webpack.config.ts",
"test-jasmine": "TRILIUM_DATA_DIR=~/trilium/data-test jasmine",
"test-es6": "node -r esm spec-es6/attribute_parser.spec.js ",
"test-jasmine": "TRILIUM_DATA_DIR=./data-test ts-node ./node_modules/.bin/jasmine",
"test-es6": "ts-node -r esm spec-es6/attribute_parser.spec.ts",
"test": "npm run test-jasmine && npm run test-es6",
"postinstall": "rimraf ./node_modules/canvas"
},
Expand Down Expand Up @@ -121,6 +121,7 @@
"@types/express-session": "^1.18.0",
"@types/html": "^1.0.4",
"@types/ini": "^4.1.0",
"@types/jasmine": "^5.1.4",
"@types/jsdom": "^21.1.6",
"@types/mime-types": "^2.1.4",
"@types/multer": "^1.4.11",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
import attributeParser from '../src/public/app/services/attribute_parser.js';
import {describe, it, expect, execute} from './mini_test.js';
import * as attributeParser from '../src/public/app/services/attribute_parser.js';

import {describe, it, expect, execute} from './mini_test';

describe("Lexing", () => {
it("simple label", () => {
expect(attributeParser.lex("#label").map(t => t.text))
expect(attributeParser.lex("#label").map((t: any) => t.text))
.toEqual(["#label"]);
});

it("simple label with trailing spaces", () => {
expect(attributeParser.lex(" #label ").map(t => t.text))
expect(attributeParser.lex(" #label ").map((t: any) => t.text))
.toEqual(["#label"]);
});

it("inherited label", () => {
expect(attributeParser.lex("#label(inheritable)").map(t => t.text))
expect(attributeParser.lex("#label(inheritable)").map((t: any) => t.text))
.toEqual(["#label", "(", "inheritable", ")"]);

expect(attributeParser.lex("#label ( inheritable ) ").map(t => t.text))
expect(attributeParser.lex("#label ( inheritable ) ").map((t: any) => t.text))
.toEqual(["#label", "(", "inheritable", ")"]);
});

it("label with value", () => {
expect(attributeParser.lex("#label=Hallo").map(t => t.text))
expect(attributeParser.lex("#label=Hallo").map((t: any) => t.text))
.toEqual(["#label", "=", "Hallo"]);
});

Expand All @@ -32,25 +33,25 @@ describe("Lexing", () => {
});

it("relation with value", () => {
expect(attributeParser.lex('~relation=#root/RclIpMauTOKS/NFi2gL4xtPxM').map(t => t.text))
expect(attributeParser.lex('~relation=#root/RclIpMauTOKS/NFi2gL4xtPxM').map((t: any) => t.text))
.toEqual(["~relation", "=", "#root/RclIpMauTOKS/NFi2gL4xtPxM"]);
});

it("use quotes to define value", () => {
expect(attributeParser.lex("#'label a'='hello\"` world'").map(t => t.text))
expect(attributeParser.lex("#'label a'='hello\"` world'").map((t: any) => t.text))
.toEqual(["#label a", "=", 'hello"` world']);

expect(attributeParser.lex('#"label a" = "hello\'` world"').map(t => t.text))
expect(attributeParser.lex('#"label a" = "hello\'` world"').map((t: any) => t.text))
.toEqual(["#label a", "=", "hello'` world"]);

expect(attributeParser.lex('#`label a` = `hello\'" world`').map(t => t.text))
expect(attributeParser.lex('#`label a` = `hello\'" world`').map((t: any) => t.text))
.toEqual(["#label a", "=", "hello'\" world"]);
});
});

describe("Parser", () => {
it("simple label", () => {
const attrs = attributeParser.parse(["#token"].map(t => ({text: t})));
const attrs = attributeParser.parse(["#token"].map((t: any) => ({text: t})));

expect(attrs.length).toEqual(1);
expect(attrs[0].type).toEqual('label');
Expand All @@ -60,7 +61,7 @@ describe("Parser", () => {
});

it("inherited label", () => {
const attrs = attributeParser.parse(["#token", "(", "inheritable", ")"].map(t => ({text: t})));
const attrs = attributeParser.parse(["#token", "(", "inheritable", ")"].map((t: any) => ({text: t})));

expect(attrs.length).toEqual(1);
expect(attrs[0].type).toEqual('label');
Expand All @@ -70,7 +71,7 @@ describe("Parser", () => {
});

it("label with value", () => {
const attrs = attributeParser.parse(["#token", "=", "val"].map(t => ({text: t})));
const attrs = attributeParser.parse(["#token", "=", "val"].map((t: any) => ({text: t})));

expect(attrs.length).toEqual(1);
expect(attrs[0].type).toEqual('label');
Expand All @@ -79,14 +80,14 @@ describe("Parser", () => {
});

it("relation", () => {
let attrs = attributeParser.parse(["~token", "=", "#root/RclIpMauTOKS/NFi2gL4xtPxM"].map(t => ({text: t})));
let attrs = attributeParser.parse(["~token", "=", "#root/RclIpMauTOKS/NFi2gL4xtPxM"].map((t: any) => ({text: t})));

expect(attrs.length).toEqual(1);
expect(attrs[0].type).toEqual('relation');
expect(attrs[0].name).toEqual("token");
expect(attrs[0].value).toEqual('NFi2gL4xtPxM');

attrs = attributeParser.parse(["~token", "=", "#NFi2gL4xtPxM"].map(t => ({text: t})));
attrs = attributeParser.parse(["~token", "=", "#NFi2gL4xtPxM"].map((t: any) => ({text: t})));

expect(attrs.length).toEqual(1);
expect(attrs[0].type).toEqual('relation');
Expand Down
12 changes: 6 additions & 6 deletions spec-es6/mini_test.js → spec-es6/mini_test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
export function describe(name, cb) {
export function describe(name: string, cb: () => any) {
console.log(`Running ${name}`);

cb();
}

export async function it(name, cb) {
export async function it(name: string, cb: () => any) {
console.log(` Running ${name}`);

cb();
}

let errorCount = 0;

export function expect(val) {
export function expect(val: any) {
return {
toEqual: comparedVal => {
toEqual: (comparedVal: any) => {
const jsonVal = JSON.stringify(val);
const comparedJsonVal = JSON.stringify(comparedVal);

Expand Down Expand Up @@ -44,11 +44,11 @@ export function expect(val) {
errorCount++;
}
},
toThrow: errorMessage => {
toThrow: (errorMessage: any) => {
try {
val();
}
catch (e) {
catch (e: any) {
if (e.message !== errorMessage) {
console.trace("toThrow caught exception, but messages differ");
console.error(`expected: ${errorMessage}`);
Expand Down
12 changes: 0 additions & 12 deletions spec/etapi/app_info.js

This file was deleted.

8 changes: 8 additions & 0 deletions spec/etapi/app_info.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import etapi = require("../support/etapi");

etapi.describeEtapi("app_info", () => {
it("get", async () => {
const appInfo = await etapi.getEtapi("app-info");
expect(appInfo.clipperProtocolVersion).toEqual("1.0");
});
});
12 changes: 0 additions & 12 deletions spec/etapi/backup.js

This file was deleted.

8 changes: 8 additions & 0 deletions spec/etapi/backup.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import etapi = require("../support/etapi");

etapi.describeEtapi("backup", () => {
it("create", async () => {
const response = await etapi.putEtapiContent("backup/etapi_test");
expect(response.status).toEqual(204);
});
});
24 changes: 0 additions & 24 deletions spec/etapi/import.js

This file was deleted.

28 changes: 28 additions & 0 deletions spec/etapi/import.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import etapi = require("../support/etapi");
import fs = require("fs");
import path = require("path");

etapi.describeEtapi("import", () => {
// temporarily skip this test since test-export.zip is missing
xit("import", async () => {
const zipFileBuffer = fs.readFileSync(
path.resolve(__dirname, "test-export.zip")
);

const response = await etapi.postEtapiContent(
"notes/root/import",
zipFileBuffer
);
expect(response.status).toEqual(201);

const { note, branch } = await response.json();

expect(note.title).toEqual("test-export");
expect(branch.parentNoteId).toEqual("root");

const content = await (
await etapi.getEtapiContent(`notes/${note.noteId}/content`)
).text();
expect(content).toContain("test export content");
});
});
Loading

0 comments on commit eff6ca3

Please sign in to comment.