Skip to content

Commit d312f8e

Browse files
committed
test: podium bridge integration test
1 parent b5949ca commit d312f8e

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"eslint-config-prettier": "9.1.0",
5858
"eslint-plugin-prettier": "5.1.3",
5959
"globals": "15.0.0",
60+
"jsdom": "24.0.0",
6061
"prettier": "3.2.5",
6162
"rollup": "4.15.0",
6263
"tap": "18.7.2"

test/Bridge.test.js

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import tap from 'tap';
2+
import { JSDOM } from 'jsdom';
3+
4+
const html = /* html */ `<!doctype html>
5+
<html>
6+
<head>
7+
<meta charset="utf-8">
8+
</head>
9+
<body></body>
10+
</html>`;
11+
const dom = new JSDOM(html);
12+
globalThis.window = dom.window;
13+
14+
const { PodiumBridge } = await import('@podium/bridge');
15+
const { default: MessageBus } = await import('../src/MessageBus.js');
16+
17+
/** @type {import('../src/MessageBus.js').default} */
18+
let bus;
19+
20+
tap.beforeEach(() => {
21+
// Unregister listeners and clear the global between tests for a clean slate
22+
if (globalThis.window['@podium']?.bridge) {
23+
/** @type {import('@podium/bridge').PodiumBridge} */
24+
const bridge = globalThis.window['@podium'].bridge;
25+
bridge.destroy();
26+
}
27+
28+
globalThis.window['@podium'] = {};
29+
globalThis.window['@podium'].bridge = new PodiumBridge();
30+
bus = new MessageBus();
31+
});
32+
33+
tap.test('bridge.on() - should invoke subscribed messagebus listener', (t) => {
34+
t.ok(
35+
globalThis.window['@podium'].bridge,
36+
'Expected the Podium bridge to be globally available',
37+
);
38+
39+
const payload = { a: 'b' };
40+
bus.subscribe('foo', 'bar', (event) => {
41+
t.equal(event.payload, payload);
42+
t.end();
43+
});
44+
45+
const rpcRequest = {
46+
method: 'foo/bar',
47+
params: payload,
48+
jsonrpc: '2.0',
49+
};
50+
51+
globalThis.window.dispatchEvent(
52+
new globalThis.window.CustomEvent('rpcbridge', {
53+
detail: rpcRequest,
54+
}),
55+
);
56+
});
57+
58+
tap.test('unsubscribe() - should remove subscribed listener', (t) => {
59+
t.ok(
60+
globalThis.window['@podium'].bridge,
61+
'Expected the Podium bridge to be globally available',
62+
);
63+
64+
const payload = { a: 'b' };
65+
let count = 0;
66+
const listener = (event) => {
67+
t.equal(event.payload, payload);
68+
count += 1;
69+
};
70+
71+
bus.subscribe('foo', 'bar', listener);
72+
73+
const rpcRequest = {
74+
method: 'foo/bar',
75+
params: payload,
76+
jsonrpc: '2.0',
77+
};
78+
79+
globalThis.window.dispatchEvent(
80+
new globalThis.window.CustomEvent('rpcbridge', {
81+
detail: rpcRequest,
82+
}),
83+
);
84+
globalThis.window.dispatchEvent(
85+
new globalThis.window.CustomEvent('rpcbridge', {
86+
detail: rpcRequest,
87+
}),
88+
);
89+
90+
t.equal(count, 2);
91+
92+
bus.unsubscribe('foo', 'bar', listener);
93+
94+
globalThis.window.dispatchEvent(
95+
new globalThis.window.CustomEvent('rpcbridge', {
96+
detail: rpcRequest,
97+
}),
98+
);
99+
100+
t.equal(count, 2);
101+
t.end();
102+
});

0 commit comments

Comments
 (0)