Skip to content

Commit

Permalink
test: transform delay to async to be able to await it
Browse files Browse the repository at this point in the history
  • Loading branch information
aleks-elkin committed Jun 28, 2023
1 parent a6d80fc commit c8554ea
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 20 deletions.
12 changes: 9 additions & 3 deletions test/lib/helpers.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const isWindow = win => win && win.postMessage;
const isWindow = (win) => win && win.postMessage;

export const verifyTestMessage = (message, source) =>
source && message && message.io;

export const forwardMessage = msg =>
export const forwardMessage = (msg) =>
window.top.postMessage(
{
io: {
Expand Down Expand Up @@ -35,6 +35,12 @@ export const removeAppOrFail = (app, apps, message) => {
}
};

export const delay = cb => setTimeout(cb, 0);
export const delay = (cb, timeout = 0) =>
new Promise((resolve) => {
setTimeout(() => {
cb();
resolve();
}, timeout);
});

export const inBrowserstack = () => window.BrowserStack !== undefined;
15 changes: 7 additions & 8 deletions test/spec/app.emit.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,18 @@ export default ({ apps, topApp }) => {

it('app.emit() after CONNECT', done => {
let connectCount = 0;
const connectListener = event => {
const connectListener = async (event) => {
const testMessage = event.data;
if (!verifyTestMessage(testMessage, event.source)) {
return;
}
if (testMessage.io.method === 'connect') {
delay(() =>
sendCommand(event.source, {
command: 'emit',
topic: TOPIC_AFTER_CONNECT,
target: CHROME_APP
})
);
await delay(() =>
sendCommand(event.source, {
command: 'emit',
topic: TOPIC_AFTER_CONNECT,
target: CHROME_APP
}));
if (++connectCount === 4) {
window.removeEventListener('message', connectListener);
}
Expand Down
10 changes: 5 additions & 5 deletions test/spec/app.on.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import {
import { windowMapper } from '../lib/hub-mock';

export default ({ apps, topApp }) => {
it('app.once() [on() + off()]', done => {
it('app.once() [on() + off()]', (done) => {
let connectCount = 0;
const connectListener = event => {
const connectListener = async (event) => {
const testMessage = event.data;
if (!verifyTestMessage(testMessage, event.source)) {
return;
Expand All @@ -23,7 +23,7 @@ export default ({ apps, topApp }) => {
target: CHROME_APP,
data: ''
};
delay(() => {
await delay(() => {
sendCommand(event.source, command);
sendCommand(event.source, command);
});
Expand All @@ -34,7 +34,7 @@ export default ({ apps, topApp }) => {
};
window.addEventListener('message', connectListener);

const messageHandler = message => {
const messageHandler = (message) => {
removeAppOrFail(
message.source,
apps,
Expand All @@ -51,7 +51,7 @@ export default ({ apps, topApp }) => {
topApp.once(apps[3], messageHandler);

let messageCount = 0;
const topHandler = msg => {
const topHandler = (msg) => {
if (++messageCount === 8) {
topApp.off('*', topHandler);
if (!inBrowserstack()) {
Expand Down
8 changes: 4 additions & 4 deletions test/spec/app.spawn.spec.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { NOT_FOUND_APP } from '../lib/constants';
import {
sendCommand,
verifyTestMessage,
delay,
verifyTestMessage,
inBrowserstack
} from '../lib/helpers';

export default () => {
it('app.spawn() > App not found', done => {
it('app.spawn() > App not found', (done) => {
let connectCount = 0;
let spawnCount = 0;
const spawnListener = event => {
const spawnListener = async (event) => {
const testMessage = event.data;
if (!verifyTestMessage(testMessage, event.source)) {
return;
Expand All @@ -20,7 +20,7 @@ export default () => {
command: 'spawn',
target: NOT_FOUND_APP
};
delay(() => sendCommand(event.source, command));
await delay(() => sendCommand(event.source, command));
expect(++connectCount).toBeLessThanOrEqual(4);
} else if (testMessage.io.method === 'spawn') {
expect(testMessage.io.err).toContain(NOT_FOUND_APP);
Expand Down

0 comments on commit c8554ea

Please sign in to comment.