mirror of
https://github.com/zadam/trilium.git
synced 2025-12-03 22:14:24 +01:00
test(client): running script bundle with dayjs
This commit is contained in:
parent
f7f7fda040
commit
8c324cd185
32
apps/client/src/services/bundle.spec.ts
Normal file
32
apps/client/src/services/bundle.spec.ts
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
import { Bundle, executeBundle } from "./bundle";
|
||||||
|
import { buildNote } from "../test/easy-froca";
|
||||||
|
|
||||||
|
describe("Script bundle", () => {
|
||||||
|
it("dayjs is available", async () => {
|
||||||
|
const script = /* js */`return api.dayjs().format("YYYY-MM-DD");`;
|
||||||
|
const bundle = getBundle(script);
|
||||||
|
const result = await executeBundle(bundle, null, $());
|
||||||
|
expect(result).toMatch(/^\d{4}-\d{2}-\d{2}$/);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("dayjs is-same-or-before plugin exists", async () => {
|
||||||
|
const script = /* js */`return api.dayjs("2023-10-01").isSameOrBefore(api.dayjs("2023-10-02"));`;
|
||||||
|
const bundle = getBundle(script);
|
||||||
|
const result = await executeBundle(bundle, null, $());
|
||||||
|
expect(result).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
function getBundle(script: string) {
|
||||||
|
const id = buildNote({
|
||||||
|
title: "Script note"
|
||||||
|
}).noteId;
|
||||||
|
const bundle: Bundle = {
|
||||||
|
script: `\napiContext.modules['${id}'] = { exports: {} };\nreturn await ((async function(exports, module, require, api) {\ntry {\n${script}\n;\n} catch (e) { throw new Error(\"Load of script note \\\"Client\\\" (${id}) failed with: \" + e.message); }\nfor (const exportKey in exports) module.exports[exportKey] = exports[exportKey];\nreturn module.exports;\n}).call({}, {}, apiContext.modules['${id}'], apiContext.require([]), apiContext.apis['${id}']));\n`,
|
||||||
|
html: "",
|
||||||
|
noteId: id,
|
||||||
|
allNoteIds: [ id ]
|
||||||
|
};
|
||||||
|
return bundle;
|
||||||
|
}
|
||||||
@ -27,7 +27,7 @@ async function getAndExecuteBundle(noteId: string, originEntity = null, script =
|
|||||||
return await executeBundle(bundle, originEntity);
|
return await executeBundle(bundle, originEntity);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function executeBundle(bundle: Bundle, originEntity?: Entity | null, $container?: JQuery<HTMLElement>) {
|
export async function executeBundle(bundle: Bundle, originEntity?: Entity | null, $container?: JQuery<HTMLElement>) {
|
||||||
const apiContext = await ScriptContext(bundle.noteId, bundle.allNoteIds, originEntity, $container);
|
const apiContext = await ScriptContext(bundle.noteId, bundle.allNoteIds, originEntity, $container);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user