From 80e009ebf97a7006406bca6f573aa44b93e5f7fd Mon Sep 17 00:00:00 2001 From: Eduardo Date: Thu, 20 Jan 2022 12:16:47 +0100 Subject: [PATCH] Apply suggestions from code review Co-authored-by: Robert Knight --- .../inter-frame-communication-test.js | 180 +++++++++--------- 1 file changed, 90 insertions(+), 90 deletions(-) diff --git a/src/shared/test/integration/inter-frame-communication-test.js b/src/shared/test/integration/inter-frame-communication-test.js index 0b6100c6718..858564e3503 100644 --- a/src/shared/test/integration/inter-frame-communication-test.js +++ b/src/shared/test/integration/inter-frame-communication-test.js @@ -49,76 +49,72 @@ describe('PortProvider-PortFinder-PortRPC integration', () => { }); it('enables the communication between guest-host', async () => { - let done; - const promise = new Promise(resolve => (done = resolve)); - - // guest frame - const portFinder = new PortFinder({ - hostFrame: window, - source: 'guest', - }); - const hostRPC = new PortRPC(); + const simulateGuest = async () => { + const portFinder = new PortFinder({ + hostFrame: window, + source: 'guest', + }); + const port = await portFinder.discover('host'); - portFinder.discover('host').then(port => { + const hostRPC = new PortRPC(); hostRPC.connect(port); - hostRPC.call('ping', response => { - assert.equal(response, 'pong'); - done(); + await new Promise(resolve => + hostRPC.call('ping', response => { + assert.equal(response, 'pong'); + resolve(); + }) + ); + }; + + const simulateHost = async () => { + await delay(10); // simulate scenario when `Host` is ready after `Guest` + + const portProvider = new PortProvider(window.location.origin); + portProvider.listen(); + + const guestRPC = new PortRPC(); + guestRPC.on('ping', cb => cb('pong')); + + portProvider.on('frameConnected', (source, port) => { + if (source === 'guest') { + guestRPC.connect(port); + } }); - }); - - await delay(10); // add some realism + }; - // host frame - const portProvider = new PortProvider(window.location.origin); - portProvider.listen(); - const guestRPC = new PortRPC(); - - // Register RPC method *before* connection - guestRPC.on('ping', cb => cb('pong')); - - portProvider.on('frameConnected', (source, port) => { - if (source === 'guest') { - guestRPC.connect(port); - } - }); - - return promise; + return Promise.all([simulateGuest(), simulateHost()]); }); it('enables the communication between guest-sidebar', async () => { - let done; - const promise = new Promise(resolve => (done = resolve)); - - // guest frame; - const portFinder1 = new PortFinder({ - hostFrame: window, - source: 'guest', - }); - const sidebarRPC = new PortRPC(); - - portFinder1.discover('sidebar').then(port => { - sidebarRPC.connect(port); - sidebarRPC.call('ping', response => { - assert.equal(response, 'pong'); - done(); + const simulateGuest = async () => { + const portFinder = new PortFinder({ + hostFrame: window, + source: 'guest', }); - }); + const hostRPC = new PortRPC(); - await delay(10); // add some realism + const port = await portFinder.discover('sidebar'); + hostRPC.connect(port); - // sidebar frame - const portFinder2 = new PortFinder({ - hostFrame: window, - source: 'sidebar', - }); - const guestRPC = new PortRPC(); + await new Promise(resolve => + hostRPC.call('ping', response => { + assert.equal(response, 'pong'); + resolve(); + }) + ); + }; + + const simulateSidebar = async () => { + const portFinder = new PortFinder({ + hostFrame: window, + source: 'sidebar', + }); + const port = await portFinder.discover('host'); - // Register RPC method *before* connection - guestRPC.on('ping', cb => cb('pong')); + const guestRPC = new PortRPC(); + guestRPC.on('ping', cb => cb('pong')); - const listenerCollection = new ListenerCollection(); - portFinder2.discover('host').then(port => { + const listenerCollection = new ListenerCollection(); listenerCollection.add(port, 'message', ({ data, ports }) => { if ( isMessageEqual(data, { @@ -130,49 +126,53 @@ describe('PortProvider-PortFinder-PortRPC integration', () => { guestRPC.connect(ports[0]); } }); - port.start(); // `start` method would be triggered by `hostRPC.connect(port)` - }); - // host frame - const portProvider = new PortProvider(window.location.origin); - portProvider.listen(); + port.start(); // `start` is normally invoked by `hostRPC.connect(port)` + }; + + const simulateHost = async () => { + await delay(10); // simulate scenario when `Host` is ready after `Guest` - return promise; + const portProvider = new PortProvider(window.location.origin); + portProvider.listen(); + }; + + return Promise.all([simulateGuest(), simulateSidebar(), simulateHost()]); }); it('enables the communication between sidebar-host', async () => { - let done; - const promise = new Promise(resolve => (done = resolve)); - - // sidebar frame - const portFinder = new PortFinder({ - hostFrame: window, - source: 'sidebar', - }); - const hostRPC = new PortRPC(); + const simulateSidebar = async () => { + const portFinder = new PortFinder({ + hostFrame: window, + source: 'sidebar', + }); + const port = await portFinder.discover('host'); - portFinder.discover('host').then(port => { + const hostRPC = new PortRPC(); hostRPC.connect(port); - hostRPC.call('ping', response => { - assert.equal(response, 'pong'); - done(); + + await new Promise(resolve => { + hostRPC.call('ping', response => { + assert.equal(response, 'pong'); + resolve(); + }); }); - }); + }; - // host frame - const portProvider = new PortProvider(window.location.origin); - portProvider.listen(); - const sidebarRPC = new PortRPC(); + const simulateHost = () => { + const portProvider = new PortProvider(window.location.origin); + portProvider.listen(); - // Register RPC method *before* connection - sidebarRPC.on('ping', cb => cb('pong')); + const sidebarRPC = new PortRPC(); + sidebarRPC.on('ping', cb => cb('pong')); - portProvider.on('frameConnected', (source, port) => { - if (source === 'sidebar') { - sidebarRPC.connect(port); - } - }); + portProvider.on('frameConnected', (source, port) => { + if (source === 'sidebar') { + sidebarRPC.connect(port); + } + }); + }; - return promise; + return Promise.all([simulateSidebar(), simulateHost()]); }); });