Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/lib/pluginContext.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
const cordovaExec = cordova.exec.bind(cordova);
// `cordova.exec` is mapped asynchronously while Cordova starts. Importing this
// module can happen before that mapping completes, so use Cordova's internal
// module loader, which is available as soon as cordova.js has been evaluated.
const cordovaExec = cordova.require("cordova/exec");

const exec = (resolve, reject, action, args) =>
cordovaExec(resolve, reject, "Tee", action, args);
Expand Down
27 changes: 27 additions & 0 deletions tests/unit/pluginContext.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { afterEach, describe, expect, it, vi } from "vitest";

afterEach(() => {
vi.resetModules();
vi.unstubAllGlobals();
});

describe("plugin context Cordova bridge", () => {
it("loads before cordova.exec is mapped", async () => {
const nativeExec = vi.fn((resolve, _reject, _service, action) => {
if (action === "establishConnection") {
resolve("trusted-session");
} else if (action === "requestToken") {
resolve("plugin-token");
}
});
const requireModule = vi.fn(() => nativeExec);
vi.stubGlobal("cordova", { require: requireModule });

const pluginContext = await import("lib/pluginContext");

expect(requireModule).toHaveBeenCalledWith("cordova/exec");
expect(cordova.exec).toBeUndefined();
await expect(pluginContext.connect()).resolves.toBe(true);
await expect(pluginContext.default("example.plugin", "{}")).resolves.toBeTruthy();
});
});