UNPKG

nope-js-node

Version:

NoPE Runtime for Nodejs. For Browser-Support please use nope-browser

132 lines (131 loc) 5.73 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); require("reflect-metadata"); const mocha_1 = require("mocha"); const chai_1 = require("chai"); const index_browser_1 = require("../helpers/index.browser"); (0, mocha_1.describe)("Plugins", function () { // Describe the required Test: (0, mocha_1.describe)("rpcCallbacks", function () { (0, mocha_1.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; }, ]); (0, chai_1.assert)(res === 0, "Value should be 0"); (0, chai_1.assert)(called === 2, "Value should be called twice."); (0, chai_1.assert)(loader.dispatcher.rpcManager.services.data.getContent().length === 2); } catch (e) { throw e; } await loader.dispatcher.dispose(); delete require.cache[require.resolve("../index.nodejs")]; }); (0, mocha_1.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], }); (0, chai_1.assert)(res === 0, "Value should be 0"); (0, chai_1.assert)(called === 2, "Value should be called twice."); (0, chai_1.assert)(loader.dispatcher.rpcManager.services.data.getContent().length === 1); } catch (e) { throw e; } await loader.dispatcher.dispose(); delete require.cache[require.resolve("../index.nodejs")]; }); (0, mocha_1.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, }); (0, chai_1.assert)(res === 0, "Value should be 0"); (0, chai_1.assert)(called === 2, "Value should be called twice."); (0, chai_1.assert)(loader.dispatcher.rpcManager.services.data.getContent().length === 2); await (0, index_browser_1.sleep)(200); (0, chai_1.assert)(loader.dispatcher.rpcManager.services.data.getContent().length === 1); } catch (e) { throw e; } await loader.dispatcher.dispose(); delete require.cache[require.resolve("../index.nodejs")]; }); }); });