@wvb/bridge 0.1.0-next.3603a22
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.
- package/CHANGELOG.md +8 -0
- package/README.md +1 -0
- package/dist/index-eeBgJJSy.d.cts +198 -0
- package/dist/index-eeBgJJSy.d.ts +198 -0
- package/dist/index.cjs +340 -0
- package/dist/index.d.cts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +331 -0
- package/dist/platform-mock-Bvaw9qjP.js +50 -0
- package/dist/platform-mock-DrAsRJ91.cjs +73 -0
- package/dist/testing/index.cjs +117 -0
- package/dist/testing/index.d.cts +75 -0
- package/dist/testing/index.d.ts +75 -0
- package/dist/testing/index.js +113 -0
- package/package.json +53 -0
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
//#region \0@oxc-project+runtime@0.133.0/helpers/esm/typeof.js
|
|
2
|
+
function _typeof(o) {
|
|
3
|
+
"@babel/helpers - typeof";
|
|
4
|
+
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
|
|
5
|
+
return typeof o;
|
|
6
|
+
} : function(o) {
|
|
7
|
+
return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
|
|
8
|
+
}, _typeof(o);
|
|
9
|
+
}
|
|
10
|
+
//#endregion
|
|
11
|
+
//#region \0@oxc-project+runtime@0.133.0/helpers/esm/toPrimitive.js
|
|
12
|
+
function toPrimitive(t, r) {
|
|
13
|
+
if ("object" != _typeof(t) || !t) return t;
|
|
14
|
+
var e = t[Symbol.toPrimitive];
|
|
15
|
+
if (void 0 !== e) {
|
|
16
|
+
var i = e.call(t, r || "default");
|
|
17
|
+
if ("object" != _typeof(i)) return i;
|
|
18
|
+
throw new TypeError("@@toPrimitive must return a primitive value.");
|
|
19
|
+
}
|
|
20
|
+
return ("string" === r ? String : Number)(t);
|
|
21
|
+
}
|
|
22
|
+
//#endregion
|
|
23
|
+
//#region \0@oxc-project+runtime@0.133.0/helpers/esm/toPropertyKey.js
|
|
24
|
+
function toPropertyKey(t) {
|
|
25
|
+
var i = toPrimitive(t, "string");
|
|
26
|
+
return "symbol" == _typeof(i) ? i : i + "";
|
|
27
|
+
}
|
|
28
|
+
//#endregion
|
|
29
|
+
//#region \0@oxc-project+runtime@0.133.0/helpers/esm/defineProperty.js
|
|
30
|
+
function _defineProperty(e, r, t) {
|
|
31
|
+
return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
|
|
32
|
+
value: t,
|
|
33
|
+
enumerable: !0,
|
|
34
|
+
configurable: !0,
|
|
35
|
+
writable: !0
|
|
36
|
+
}) : e[r] = t, e;
|
|
37
|
+
}
|
|
38
|
+
//#endregion
|
|
39
|
+
//#region src/window.ts
|
|
40
|
+
function getWindow() {
|
|
41
|
+
return globalThis || window;
|
|
42
|
+
}
|
|
43
|
+
//#endregion
|
|
44
|
+
//#region src/invoke-mock.ts
|
|
45
|
+
const INVOKE_MOCK_KEY = "__wvb_invoke_mock__";
|
|
46
|
+
//#endregion
|
|
47
|
+
//#region src/platform-mock.ts
|
|
48
|
+
const PLATFORM_MOCK_KEY = "__wvb_platform_mock__";
|
|
49
|
+
//#endregion
|
|
50
|
+
export { _defineProperty as i, INVOKE_MOCK_KEY as n, getWindow as r, PLATFORM_MOCK_KEY as t };
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
//#region \0@oxc-project+runtime@0.133.0/helpers/esm/typeof.js
|
|
2
|
+
function _typeof(o) {
|
|
3
|
+
"@babel/helpers - typeof";
|
|
4
|
+
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
|
|
5
|
+
return typeof o;
|
|
6
|
+
} : function(o) {
|
|
7
|
+
return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
|
|
8
|
+
}, _typeof(o);
|
|
9
|
+
}
|
|
10
|
+
//#endregion
|
|
11
|
+
//#region \0@oxc-project+runtime@0.133.0/helpers/esm/toPrimitive.js
|
|
12
|
+
function toPrimitive(t, r) {
|
|
13
|
+
if ("object" != _typeof(t) || !t) return t;
|
|
14
|
+
var e = t[Symbol.toPrimitive];
|
|
15
|
+
if (void 0 !== e) {
|
|
16
|
+
var i = e.call(t, r || "default");
|
|
17
|
+
if ("object" != _typeof(i)) return i;
|
|
18
|
+
throw new TypeError("@@toPrimitive must return a primitive value.");
|
|
19
|
+
}
|
|
20
|
+
return ("string" === r ? String : Number)(t);
|
|
21
|
+
}
|
|
22
|
+
//#endregion
|
|
23
|
+
//#region \0@oxc-project+runtime@0.133.0/helpers/esm/toPropertyKey.js
|
|
24
|
+
function toPropertyKey(t) {
|
|
25
|
+
var i = toPrimitive(t, "string");
|
|
26
|
+
return "symbol" == _typeof(i) ? i : i + "";
|
|
27
|
+
}
|
|
28
|
+
//#endregion
|
|
29
|
+
//#region \0@oxc-project+runtime@0.133.0/helpers/esm/defineProperty.js
|
|
30
|
+
function _defineProperty(e, r, t) {
|
|
31
|
+
return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
|
|
32
|
+
value: t,
|
|
33
|
+
enumerable: !0,
|
|
34
|
+
configurable: !0,
|
|
35
|
+
writable: !0
|
|
36
|
+
}) : e[r] = t, e;
|
|
37
|
+
}
|
|
38
|
+
//#endregion
|
|
39
|
+
//#region src/window.ts
|
|
40
|
+
function getWindow() {
|
|
41
|
+
return globalThis || window;
|
|
42
|
+
}
|
|
43
|
+
//#endregion
|
|
44
|
+
//#region src/invoke-mock.ts
|
|
45
|
+
const INVOKE_MOCK_KEY = "__wvb_invoke_mock__";
|
|
46
|
+
//#endregion
|
|
47
|
+
//#region src/platform-mock.ts
|
|
48
|
+
const PLATFORM_MOCK_KEY = "__wvb_platform_mock__";
|
|
49
|
+
//#endregion
|
|
50
|
+
Object.defineProperty(exports, "INVOKE_MOCK_KEY", {
|
|
51
|
+
enumerable: true,
|
|
52
|
+
get: function() {
|
|
53
|
+
return INVOKE_MOCK_KEY;
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
Object.defineProperty(exports, "PLATFORM_MOCK_KEY", {
|
|
57
|
+
enumerable: true,
|
|
58
|
+
get: function() {
|
|
59
|
+
return PLATFORM_MOCK_KEY;
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
Object.defineProperty(exports, "_defineProperty", {
|
|
63
|
+
enumerable: true,
|
|
64
|
+
get: function() {
|
|
65
|
+
return _defineProperty;
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
Object.defineProperty(exports, "getWindow", {
|
|
69
|
+
enumerable: true,
|
|
70
|
+
get: function() {
|
|
71
|
+
return getWindow;
|
|
72
|
+
}
|
|
73
|
+
});
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_platform_mock = require("../platform-mock-DrAsRJ91.cjs");
|
|
3
|
+
//#region src/testing/mock.ts
|
|
4
|
+
const store = /* @__PURE__ */ new Map();
|
|
5
|
+
function ensureMocked() {
|
|
6
|
+
const w = require_platform_mock.getWindow();
|
|
7
|
+
if (w["__wvb_invoke_mock__"] != null) return;
|
|
8
|
+
w[require_platform_mock.INVOKE_MOCK_KEY] = (name, params) => {
|
|
9
|
+
const handler = store.get(name);
|
|
10
|
+
if (handler == null) return Promise.reject(/* @__PURE__ */ new Error(`[@wvb/bridge/testing] no mock registered for invoke "${name}"`));
|
|
11
|
+
const args = params == null ? [] : Object.values(params);
|
|
12
|
+
return Promise.resolve().then(() => handler(...args));
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
function toInvokeName(command) {
|
|
16
|
+
const [namespace = "", method = ""] = command.split(".");
|
|
17
|
+
return `${namespace}${method.charAt(0).toUpperCase()}${method.slice(1)}`;
|
|
18
|
+
}
|
|
19
|
+
function registerMockInvoke(command, handler) {
|
|
20
|
+
ensureMocked();
|
|
21
|
+
const name = toInvokeName(command);
|
|
22
|
+
store.set(name, handler);
|
|
23
|
+
const clear = () => {
|
|
24
|
+
if (store.get(name) === handler) store.delete(name);
|
|
25
|
+
};
|
|
26
|
+
return {
|
|
27
|
+
clear,
|
|
28
|
+
[Symbol.dispose]: clear
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
function clearInvokeMocks() {
|
|
32
|
+
store.clear();
|
|
33
|
+
}
|
|
34
|
+
//#endregion
|
|
35
|
+
//#region src/testing/index.ts
|
|
36
|
+
let _Symbol$dispose;
|
|
37
|
+
/**
|
|
38
|
+
* Registers a mock handler that responds a single bridge `invoke` command.
|
|
39
|
+
*
|
|
40
|
+
* Returns a `MockInvoke` (a `Disposable`): declare it with `using` to clear the mock
|
|
41
|
+
* automatically when the scope exits, call `.clear()` to remove it manually, or let it
|
|
42
|
+
* persist until {@link clearInvokeMocks}.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```ts
|
|
46
|
+
* import { source } from '@wvb/bridge';
|
|
47
|
+
* import { mockInvoke } from '@wvb/bridge/testing';
|
|
48
|
+
* import { expect, test } from 'vitest';
|
|
49
|
+
*
|
|
50
|
+
* test('loads the active version', async () => {
|
|
51
|
+
* using _mock = mockInvoke('source.loadVersion', bundleName => {
|
|
52
|
+
* expect(bundleName).toBe('app');
|
|
53
|
+
* return { type: 'remote', version: '1.0.0' };
|
|
54
|
+
* });
|
|
55
|
+
* await expect(source.loadVersion('app')).resolves.toEqual({ type: 'remote', version: '1.0.0' });
|
|
56
|
+
* });
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
function mockInvoke(command, handler) {
|
|
60
|
+
return registerMockInvoke(command, handler);
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Mock platform type.
|
|
64
|
+
*/
|
|
65
|
+
function mockPlatform(type) {
|
|
66
|
+
const host = require_platform_mock.getWindow();
|
|
67
|
+
const previous = host[require_platform_mock.PLATFORM_MOCK_KEY];
|
|
68
|
+
host[require_platform_mock.PLATFORM_MOCK_KEY] = type;
|
|
69
|
+
return { [Symbol.dispose]() {
|
|
70
|
+
host[require_platform_mock.PLATFORM_MOCK_KEY] = previous;
|
|
71
|
+
} };
|
|
72
|
+
}
|
|
73
|
+
_Symbol$dispose = Symbol.dispose;
|
|
74
|
+
var MockBridgeImpl = class {
|
|
75
|
+
constructor(options) {
|
|
76
|
+
require_platform_mock._defineProperty(this, "disposables", []);
|
|
77
|
+
if (options?.platform != null) this.disposables.push(mockPlatform(options.platform));
|
|
78
|
+
}
|
|
79
|
+
mockInvoke(command, handler) {
|
|
80
|
+
this.disposables.push(mockInvoke(command, handler));
|
|
81
|
+
return this;
|
|
82
|
+
}
|
|
83
|
+
clear() {
|
|
84
|
+
for (const disposable of this.disposables.reverse()) disposable[Symbol.dispose]();
|
|
85
|
+
this.disposables = [];
|
|
86
|
+
}
|
|
87
|
+
[_Symbol$dispose]() {
|
|
88
|
+
this.clear();
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
/**
|
|
92
|
+
* Create a mock bridge instance.
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* ```ts
|
|
96
|
+
* import { source } from '@wvb/bridge';
|
|
97
|
+
* import { mockBridge } from '@wvb/bridge/testing';
|
|
98
|
+
* import { expect, test } from 'vitest';
|
|
99
|
+
*
|
|
100
|
+
* test('loads the active version', async () => {
|
|
101
|
+
* using bridge = mockBridge()
|
|
102
|
+
* .mockInvoke('source.loadVersion', bundleName => {
|
|
103
|
+
* expect(bundleName).toBe('app');
|
|
104
|
+
* return { type: 'remote', version: '1.0.0' };
|
|
105
|
+
* });
|
|
106
|
+
*
|
|
107
|
+
* await expect(source.loadVersion('app')).resolves.toEqual({ type: 'remote', version: '1.0.0' });
|
|
108
|
+
* });
|
|
109
|
+
*/
|
|
110
|
+
function mockBridge(options) {
|
|
111
|
+
return new MockBridgeImpl(options);
|
|
112
|
+
}
|
|
113
|
+
//#endregion
|
|
114
|
+
exports.clearInvokeMocks = clearInvokeMocks;
|
|
115
|
+
exports.mockBridge = mockBridge;
|
|
116
|
+
exports.mockInvoke = mockInvoke;
|
|
117
|
+
exports.mockPlatform = mockPlatform;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { f as RemoteApi, h as PlatformType, l as SourceApi, n as UpdaterApi } from "../index-eeBgJJSy.cjs";
|
|
2
|
+
|
|
3
|
+
//#region src/testing/mock.d.ts
|
|
4
|
+
interface MockInvoke extends Disposable {
|
|
5
|
+
clear(): void;
|
|
6
|
+
}
|
|
7
|
+
declare function clearInvokeMocks(): void;
|
|
8
|
+
//#endregion
|
|
9
|
+
//#region src/testing/typesmap.d.ts
|
|
10
|
+
type Commands<Namespace extends string, Api> = { [K in keyof Api as `${Namespace}.${K & string}`]: Api[K] extends ((...args: infer P) => infer R) ? {
|
|
11
|
+
params: P;
|
|
12
|
+
result: Awaited<R>;
|
|
13
|
+
} : never };
|
|
14
|
+
type BridgeCommandMap = Commands<"source", SourceApi> & Commands<"remote", RemoteApi> & Commands<"updater", UpdaterApi>;
|
|
15
|
+
type MockInvokeCommand = keyof BridgeCommandMap;
|
|
16
|
+
type MockInvokeHandler<K extends MockInvokeCommand> = (...params: BridgeCommandMap[K]["params"]) => BridgeCommandMap[K]["result"] | Promise<BridgeCommandMap[K]["result"]>;
|
|
17
|
+
//#endregion
|
|
18
|
+
//#region src/testing/index.d.ts
|
|
19
|
+
/**
|
|
20
|
+
* Registers a mock handler that responds a single bridge `invoke` command.
|
|
21
|
+
*
|
|
22
|
+
* Returns a `MockInvoke` (a `Disposable`): declare it with `using` to clear the mock
|
|
23
|
+
* automatically when the scope exits, call `.clear()` to remove it manually, or let it
|
|
24
|
+
* persist until {@link clearInvokeMocks}.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts
|
|
28
|
+
* import { source } from '@wvb/bridge';
|
|
29
|
+
* import { mockInvoke } from '@wvb/bridge/testing';
|
|
30
|
+
* import { expect, test } from 'vitest';
|
|
31
|
+
*
|
|
32
|
+
* test('loads the active version', async () => {
|
|
33
|
+
* using _mock = mockInvoke('source.loadVersion', bundleName => {
|
|
34
|
+
* expect(bundleName).toBe('app');
|
|
35
|
+
* return { type: 'remote', version: '1.0.0' };
|
|
36
|
+
* });
|
|
37
|
+
* await expect(source.loadVersion('app')).resolves.toEqual({ type: 'remote', version: '1.0.0' });
|
|
38
|
+
* });
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
declare function mockInvoke<K extends MockInvokeCommand>(command: K, handler: MockInvokeHandler<K>): MockInvoke;
|
|
42
|
+
/**
|
|
43
|
+
* Mock platform type.
|
|
44
|
+
*/
|
|
45
|
+
declare function mockPlatform(type: PlatformType): Disposable;
|
|
46
|
+
interface MockBridge extends Disposable {
|
|
47
|
+
mockInvoke<K extends MockInvokeCommand>(command: K, handler: MockInvokeHandler<K>): this;
|
|
48
|
+
clear(): void;
|
|
49
|
+
}
|
|
50
|
+
interface MockBridgeOptions {
|
|
51
|
+
/** Platform type to mock. */
|
|
52
|
+
platform?: PlatformType;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Create a mock bridge instance.
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```ts
|
|
59
|
+
* import { source } from '@wvb/bridge';
|
|
60
|
+
* import { mockBridge } from '@wvb/bridge/testing';
|
|
61
|
+
* import { expect, test } from 'vitest';
|
|
62
|
+
*
|
|
63
|
+
* test('loads the active version', async () => {
|
|
64
|
+
* using bridge = mockBridge()
|
|
65
|
+
* .mockInvoke('source.loadVersion', bundleName => {
|
|
66
|
+
* expect(bundleName).toBe('app');
|
|
67
|
+
* return { type: 'remote', version: '1.0.0' };
|
|
68
|
+
* });
|
|
69
|
+
*
|
|
70
|
+
* await expect(source.loadVersion('app')).resolves.toEqual({ type: 'remote', version: '1.0.0' });
|
|
71
|
+
* });
|
|
72
|
+
*/
|
|
73
|
+
declare function mockBridge(options?: MockBridgeOptions): MockBridge;
|
|
74
|
+
//#endregion
|
|
75
|
+
export { type BridgeCommandMap, MockBridge, MockBridgeOptions, type MockInvoke, type MockInvokeCommand, type MockInvokeHandler, clearInvokeMocks, mockBridge, mockInvoke, mockPlatform };
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { f as RemoteApi, h as PlatformType, l as SourceApi, n as UpdaterApi } from "../index-eeBgJJSy.js";
|
|
2
|
+
|
|
3
|
+
//#region src/testing/mock.d.ts
|
|
4
|
+
interface MockInvoke extends Disposable {
|
|
5
|
+
clear(): void;
|
|
6
|
+
}
|
|
7
|
+
declare function clearInvokeMocks(): void;
|
|
8
|
+
//#endregion
|
|
9
|
+
//#region src/testing/typesmap.d.ts
|
|
10
|
+
type Commands<Namespace extends string, Api> = { [K in keyof Api as `${Namespace}.${K & string}`]: Api[K] extends ((...args: infer P) => infer R) ? {
|
|
11
|
+
params: P;
|
|
12
|
+
result: Awaited<R>;
|
|
13
|
+
} : never };
|
|
14
|
+
type BridgeCommandMap = Commands<"source", SourceApi> & Commands<"remote", RemoteApi> & Commands<"updater", UpdaterApi>;
|
|
15
|
+
type MockInvokeCommand = keyof BridgeCommandMap;
|
|
16
|
+
type MockInvokeHandler<K extends MockInvokeCommand> = (...params: BridgeCommandMap[K]["params"]) => BridgeCommandMap[K]["result"] | Promise<BridgeCommandMap[K]["result"]>;
|
|
17
|
+
//#endregion
|
|
18
|
+
//#region src/testing/index.d.ts
|
|
19
|
+
/**
|
|
20
|
+
* Registers a mock handler that responds a single bridge `invoke` command.
|
|
21
|
+
*
|
|
22
|
+
* Returns a `MockInvoke` (a `Disposable`): declare it with `using` to clear the mock
|
|
23
|
+
* automatically when the scope exits, call `.clear()` to remove it manually, or let it
|
|
24
|
+
* persist until {@link clearInvokeMocks}.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts
|
|
28
|
+
* import { source } from '@wvb/bridge';
|
|
29
|
+
* import { mockInvoke } from '@wvb/bridge/testing';
|
|
30
|
+
* import { expect, test } from 'vitest';
|
|
31
|
+
*
|
|
32
|
+
* test('loads the active version', async () => {
|
|
33
|
+
* using _mock = mockInvoke('source.loadVersion', bundleName => {
|
|
34
|
+
* expect(bundleName).toBe('app');
|
|
35
|
+
* return { type: 'remote', version: '1.0.0' };
|
|
36
|
+
* });
|
|
37
|
+
* await expect(source.loadVersion('app')).resolves.toEqual({ type: 'remote', version: '1.0.0' });
|
|
38
|
+
* });
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
declare function mockInvoke<K extends MockInvokeCommand>(command: K, handler: MockInvokeHandler<K>): MockInvoke;
|
|
42
|
+
/**
|
|
43
|
+
* Mock platform type.
|
|
44
|
+
*/
|
|
45
|
+
declare function mockPlatform(type: PlatformType): Disposable;
|
|
46
|
+
interface MockBridge extends Disposable {
|
|
47
|
+
mockInvoke<K extends MockInvokeCommand>(command: K, handler: MockInvokeHandler<K>): this;
|
|
48
|
+
clear(): void;
|
|
49
|
+
}
|
|
50
|
+
interface MockBridgeOptions {
|
|
51
|
+
/** Platform type to mock. */
|
|
52
|
+
platform?: PlatformType;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Create a mock bridge instance.
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```ts
|
|
59
|
+
* import { source } from '@wvb/bridge';
|
|
60
|
+
* import { mockBridge } from '@wvb/bridge/testing';
|
|
61
|
+
* import { expect, test } from 'vitest';
|
|
62
|
+
*
|
|
63
|
+
* test('loads the active version', async () => {
|
|
64
|
+
* using bridge = mockBridge()
|
|
65
|
+
* .mockInvoke('source.loadVersion', bundleName => {
|
|
66
|
+
* expect(bundleName).toBe('app');
|
|
67
|
+
* return { type: 'remote', version: '1.0.0' };
|
|
68
|
+
* });
|
|
69
|
+
*
|
|
70
|
+
* await expect(source.loadVersion('app')).resolves.toEqual({ type: 'remote', version: '1.0.0' });
|
|
71
|
+
* });
|
|
72
|
+
*/
|
|
73
|
+
declare function mockBridge(options?: MockBridgeOptions): MockBridge;
|
|
74
|
+
//#endregion
|
|
75
|
+
export { type BridgeCommandMap, MockBridge, MockBridgeOptions, type MockInvoke, type MockInvokeCommand, type MockInvokeHandler, clearInvokeMocks, mockBridge, mockInvoke, mockPlatform };
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { i as _defineProperty, n as INVOKE_MOCK_KEY, r as getWindow, t as PLATFORM_MOCK_KEY } from "../platform-mock-Bvaw9qjP.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.3603a22",
|
|
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
|
+
}
|