refactor: move mock IPC to initializer

This commit is contained in:
Randall Morey committed 2020-11-23 14:16:43 -05:00
1 parent fd3a84c2ad
commit 0c6187e707
3 files changed
+79 -32

No files matched your search

+51
View File
@@ -0,0 +1,51 @@
import Ember from 'ember';
import config from '../config/environment';
/**
* We strive to make this application runnable in a regular web browser, since
* it is a convenient environment for development and testing. But only an
* Electron environment has true IPC. Outside of Electron, we mock the handling
* of the message-based IPC requests originating from the
* renderer (the Ember app).
*/
class MockIPC {
async invoke(method, payload) {
console.log('MockIPC:', method, payload);
return await this[method](payload);
}
origin = null;
getOrigin() {
return this.origin;
}
setOrigin(origin) {
this.origin = origin;
return this.origin;
}
}
/**
* Establishes a mock IPC handler, which mimics the behavior of the Electron
* main process, and routes messages to it in a way similar to our
* preload.js script.
*
* Initializes mock IPC only in a non-Electron non-testing context.
*/
export function initialize() {
if (!config.isElectron && !Ember.testing) {
const mockIPC = new MockIPC();
window.addEventListener('message', async function (event) {
if (event.origin !== window.location.origin) return;
const { method, payload } = event.data;
if (method) {
const response =
await mockIPC.invoke(method, payload);
event.ports[0].postMessage(response);
}
});
}
}
export default { initialize };
-32
View File
@@ -6,38 +6,6 @@ import { Response } from 'miragejs';
export default function() {
if (!config.isElectron && !Ember.testing) {
class MockIPC {
invoke(method, payload) {
console.log('MockIPC:', method, payload);
return this[method](payload);
}
origin = null;
getOrigin() {
return this.origin;
}
setOrigin(origin) {
this.origin = origin;
return this.origin;
}
}
const mockIPC = new MockIPC();
window.addEventListener('message', async function (event) {
if (event.origin !== window.location.origin) return;
const { method, payload } = event.data;
if (method) {
const response =
await mockIPC.invoke(method, payload);
event.ports[0].postMessage(response);
}
});
}
// make this `http://localhost:8080`, for example, if your API is on a different server
// this.urlPrefix = '';
// make this `/api`, for example, if your API is namespaced
@@ -0,0 +1,28 @@
import Application from '@ember/application';
import { initialize } from 'desktop/initializers/ipc';
import { module, test } from 'qunit';
import { run } from '@ember/runloop';
module('Unit | Initializer | ipc', function(hooks) {
hooks.beforeEach(function() {
this.TestApplication = Application.extend();
this.TestApplication.initializer({
name: 'initializer under test',
initialize
});
this.application = this.TestApplication.create({ autoboot: false });
});
hooks.afterEach(function() {
run(this.application, 'destroy');
});
// TODO: Replace this with your real tests.
test('it works', async function(assert) {
await this.application.boot();
assert.ok(true);
});
});