1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | import * as Comlink from "/base/dist/esm/comlink.mjs";
|
15 |
|
16 | describe("Comlink across iframes", function () {
|
17 | beforeEach(function () {
|
18 | this.ifr = document.createElement("iframe");
|
19 | this.ifr.sandbox.add("allow-scripts", "allow-same-origin");
|
20 | this.ifr.src = "/base/tests/fixtures/two-way-iframe.html";
|
21 | document.body.appendChild(this.ifr);
|
22 | return new Promise((resolve) => (this.ifr.onload = resolve));
|
23 | });
|
24 |
|
25 | afterEach(function () {
|
26 | this.ifr.remove();
|
27 | });
|
28 |
|
29 | it("can communicate both ways", async function () {
|
30 | let called = false;
|
31 | const iframe = Comlink.windowEndpoint(this.ifr.contentWindow);
|
32 | Comlink.expose((a) => {
|
33 | called = true;
|
34 | return ++a;
|
35 | }, iframe);
|
36 | const proxy = Comlink.wrap(iframe);
|
37 | expect(await proxy(1, 3)).to.equal(5);
|
38 | expect(called).to.equal(true);
|
39 | });
|
40 | });
|