Skip to content

Commit

Permalink
Added a Test for 2 way communication
Browse files Browse the repository at this point in the history
  • Loading branch information
MrMadClown committed Jan 30, 2021
1 parent cc5874a commit cb2e488
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
10 changes: 10 additions & 0 deletions tests/fixtures/two-way-iframe.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<script type="module">
import * as Comlink from "/base/dist/esm/comlink.mjs";

const parentEndpoint = Comlink.windowEndpoint(self.parent);
const wrappedParent = Comlink.wrap(parentEndpoint);

Comlink.expose(async (a, b) => {
return a + (await wrappedParent(b));
}, parentEndpoint);
</script>
40 changes: 40 additions & 0 deletions tests/two-way-iframe.comlink.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Copyright 2017 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as Comlink from "/base/dist/esm/comlink.mjs";

describe("Comlink across iframes", function () {
beforeEach(function () {
this.ifr = document.createElement("iframe");
this.ifr.sandbox.add("allow-scripts", "allow-same-origin");
this.ifr.src = "/base/tests/fixtures/two-way-iframe.html";
document.body.appendChild(this.ifr);
return new Promise((resolve) => (this.ifr.onload = resolve));
});

afterEach(function () {
this.ifr.remove();
});

it("can communicate both ways", async function () {
let called = false;
const iframe = Comlink.windowEndpoint(this.ifr.contentWindow);
Comlink.expose((a) => {
called = true;
return ++a;
}, iframe);
const proxy = Comlink.wrap(iframe);
expect(await proxy(1, 3)).to.equal(5);
expect(called).to.equal(true);
});
});

0 comments on commit cb2e488

Please sign in to comment.