nope-js-browser
Version:
NoPE Runtime for the Browser. For nodejs please use nope-js-node
130 lines (129 loc) • 5.43 kB
JavaScript
import "reflect-metadata";
import { describe, it } from "mocha";
import { assert } from "chai";
import { sleep } from "../helpers/index.browser";
describe("Plugins", function () {
// Describe the required Test:
describe("rpcCallbacks", function () {
it("callback", async function () {
// Adapt the Test Time
this.timeout(4000);
delete require.cache[require.resolve("../index.nodejs")];
const nope = require("../index.nodejs");
nope.plugins.installPlugins(nope, "rpcCallbacks", false);
const loader = await nope.runNopeBackend({
skipLoadingConfig: true,
log: "error",
});
await loader.dispatcher.ready.waitFor();
try {
let called = 0;
async function funcWithCallback(param01, cb) {
called++;
return await cb(param01);
}
await loader.dispatcher.rpcManager.registerService(funcWithCallback, {
id: "funcWithCallback",
schema: {},
});
const res = await loader.dispatcher.rpcManager.performCall("funcWithCallback", [
0,
async (param) => {
called++;
return param;
},
]);
assert(res === 0, "Value should be 0");
assert(called === 2, "Value should be called twice.");
assert(loader.dispatcher.rpcManager.services.data.getContent().length === 2);
}
catch (e) {
throw e;
}
await loader.dispatcher.dispose();
delete require.cache[require.resolve("../index.nodejs")];
});
it("delete-afterwards", async function () {
// Adapt the Test Time
this.timeout(4000);
delete require.cache[require.resolve("../index.nodejs")];
const nope = require("../index.nodejs");
nope.plugins.installPlugins(nope, "rpcCallbacks", false);
const loader = await nope.runNopeBackend({
skipLoadingConfig: true,
log: "error",
});
await loader.dispatcher.ready.waitFor();
try {
let called = 0;
async function funcWithCallback(param01, cb) {
called++;
return await cb(param01);
}
await loader.dispatcher.rpcManager.registerService(funcWithCallback, {
id: "funcWithCallback",
schema: {},
});
const res = await loader.dispatcher.rpcManager.performCall("funcWithCallback", [
0,
async (param) => {
called++;
return param;
},
], {
calledOnce: [1],
});
assert(res === 0, "Value should be 0");
assert(called === 2, "Value should be called twice.");
assert(loader.dispatcher.rpcManager.services.data.getContent().length === 1);
}
catch (e) {
throw e;
}
await loader.dispatcher.dispose();
delete require.cache[require.resolve("../index.nodejs")];
});
it("auto-delete", async function () {
// Adapt the Test Time
this.timeout(4000);
delete require.cache[require.resolve("../index.nodejs")];
const nope = require("../index.nodejs");
nope.plugins.installPlugins(nope, "rpcCallbacks", false);
const loader = await nope.runNopeBackend({
skipLoadingConfig: true,
log: "error",
});
await loader.dispatcher.ready.waitFor();
try {
let called = 0;
async function funcWithCallback(param01, cb) {
called++;
return await cb(param01);
}
await loader.dispatcher.rpcManager.registerService(funcWithCallback, {
id: "funcWithCallback",
schema: {},
});
const res = await loader.dispatcher.rpcManager.performCall("funcWithCallback", [
0,
async (param) => {
called++;
return param;
},
], {
timeToLifeAfterCall: 100,
});
assert(res === 0, "Value should be 0");
assert(called === 2, "Value should be called twice.");
assert(loader.dispatcher.rpcManager.services.data.getContent().length === 2);
await sleep(200);
assert(loader.dispatcher.rpcManager.services.data.getContent().length === 1);
}
catch (e) {
throw e;
}
await loader.dispatcher.dispose();
delete require.cache[require.resolve("../index.nodejs")];
});
});
});