@wvb/bridge 0.1.0-next.e459de3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,113 @@
1
+ import { i as getWindow, n as INVOKE_MOCK_KEY, r as _defineProperty, t as PLATFORM_MOCK_KEY } from "../platform-mock-Dh3dBF3P.js";
2
+ //#region src/testing/mock.ts
3
+ const store = /* @__PURE__ */ new Map();
4
+ function ensureMocked() {
5
+ const w = getWindow();
6
+ if (w["__wvb_invoke_mock__"] != null) return;
7
+ w[INVOKE_MOCK_KEY] = (name, params) => {
8
+ const handler = store.get(name);
9
+ if (handler == null) return Promise.reject(/* @__PURE__ */ new Error(`[@wvb/bridge/testing] no mock registered for invoke "${name}"`));
10
+ const args = params == null ? [] : Object.values(params);
11
+ return Promise.resolve().then(() => handler(...args));
12
+ };
13
+ }
14
+ function toInvokeName(command) {
15
+ const [namespace = "", method = ""] = command.split(".");
16
+ return `${namespace}${method.charAt(0).toUpperCase()}${method.slice(1)}`;
17
+ }
18
+ function registerMockInvoke(command, handler) {
19
+ ensureMocked();
20
+ const name = toInvokeName(command);
21
+ store.set(name, handler);
22
+ const clear = () => {
23
+ if (store.get(name) === handler) store.delete(name);
24
+ };
25
+ return {
26
+ clear,
27
+ [Symbol.dispose]: clear
28
+ };
29
+ }
30
+ function clearInvokeMocks() {
31
+ store.clear();
32
+ }
33
+ //#endregion
34
+ //#region src/testing/index.ts
35
+ let _Symbol$dispose;
36
+ /**
37
+ * Registers a mock handler that responds a single bridge `invoke` command.
38
+ *
39
+ * Returns a `MockInvoke` (a `Disposable`): declare it with `using` to clear the mock
40
+ * automatically when the scope exits, call `.clear()` to remove it manually, or let it
41
+ * persist until {@link clearInvokeMocks}.
42
+ *
43
+ * @example
44
+ * ```ts
45
+ * import { source } from '@wvb/bridge';
46
+ * import { mockInvoke } from '@wvb/bridge/testing';
47
+ * import { expect, test } from 'vitest';
48
+ *
49
+ * test('loads the active version', async () => {
50
+ * using _mock = mockInvoke('source.loadVersion', bundleName => {
51
+ * expect(bundleName).toBe('app');
52
+ * return { type: 'remote', version: '1.0.0' };
53
+ * });
54
+ * await expect(source.loadVersion('app')).resolves.toEqual({ type: 'remote', version: '1.0.0' });
55
+ * });
56
+ * ```
57
+ */
58
+ function mockInvoke(command, handler) {
59
+ return registerMockInvoke(command, handler);
60
+ }
61
+ /**
62
+ * Mock platform type.
63
+ */
64
+ function mockPlatform(type) {
65
+ const host = getWindow();
66
+ const previous = host[PLATFORM_MOCK_KEY];
67
+ host[PLATFORM_MOCK_KEY] = type;
68
+ return { [Symbol.dispose]() {
69
+ host[PLATFORM_MOCK_KEY] = previous;
70
+ } };
71
+ }
72
+ _Symbol$dispose = Symbol.dispose;
73
+ var MockBridgeImpl = class {
74
+ constructor(options) {
75
+ _defineProperty(this, "disposables", []);
76
+ if (options?.platform != null) this.disposables.push(mockPlatform(options.platform));
77
+ }
78
+ mockInvoke(command, handler) {
79
+ this.disposables.push(mockInvoke(command, handler));
80
+ return this;
81
+ }
82
+ clear() {
83
+ for (const disposable of this.disposables.reverse()) disposable[Symbol.dispose]();
84
+ this.disposables = [];
85
+ }
86
+ [_Symbol$dispose]() {
87
+ this.clear();
88
+ }
89
+ };
90
+ /**
91
+ * Create a mock bridge instance.
92
+ *
93
+ * @example
94
+ * ```ts
95
+ * import { source } from '@wvb/bridge';
96
+ * import { mockBridge } from '@wvb/bridge/testing';
97
+ * import { expect, test } from 'vitest';
98
+ *
99
+ * test('loads the active version', async () => {
100
+ * using bridge = mockBridge()
101
+ * .mockInvoke('source.loadVersion', bundleName => {
102
+ * expect(bundleName).toBe('app');
103
+ * return { type: 'remote', version: '1.0.0' };
104
+ * });
105
+ *
106
+ * await expect(source.loadVersion('app')).resolves.toEqual({ type: 'remote', version: '1.0.0' });
107
+ * });
108
+ */
109
+ function mockBridge(options) {
110
+ return new MockBridgeImpl(options);
111
+ }
112
+ //#endregion
113
+ export { clearInvokeMocks, mockBridge, mockInvoke, mockPlatform };
package/package.json ADDED
@@ -0,0 +1,53 @@
1
+ {
2
+ "name": "@wvb/bridge",
3
+ "version": "0.1.0-next.e459de3",
4
+ "description": "WebView bridge",
5
+ "homepage": "https://github.com/webview-bundle/webview-bundle",
6
+ "bugs": {
7
+ "url": "https://github.com/webview-bundle/webview-bundle/issues"
8
+ },
9
+ "license": "MIT",
10
+ "author": {
11
+ "name": "Seokju Na",
12
+ "email": "seokju.me@gmail.com",
13
+ "url": "https://github.com/seokju-na"
14
+ },
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "https://github.com/webview-bundle/webview-bundle",
18
+ "directory": "packages/bridge"
19
+ },
20
+ "files": [
21
+ "dist",
22
+ "README.md",
23
+ "CHANGELOG.md"
24
+ ],
25
+ "type": "module",
26
+ "main": "./dist/index.cjs",
27
+ "types": "./dist/index.d.ts",
28
+ "exports": {
29
+ ".": {
30
+ "import": "./dist/index.js",
31
+ "require": "./dist/index.cjs"
32
+ },
33
+ "./testing": {
34
+ "import": "./dist/testing/index.js",
35
+ "require": "./dist/testing/index.cjs"
36
+ },
37
+ "./package.json": "./package.json"
38
+ },
39
+ "scripts": {
40
+ "prepack": "yarn build",
41
+ "build": "tsdown",
42
+ "typecheck": "tsc --noEmit"
43
+ },
44
+ "dependencies": {
45
+ "@tauri-apps/api": "^2"
46
+ },
47
+ "devDependencies": {
48
+ "tsdown": "0.22.1",
49
+ "type-fest": "^4.40.1",
50
+ "typescript": "6.0.3",
51
+ "vitest": "4.1.8"
52
+ }
53
+ }