feat: 🎸 Ember IPC service rejects when receiving error instance

This commit is contained in:
Randall Morey committed 2020-12-03 15:08:36 -05:00
1 parent fdb43fedb0
commit 77f21f696d
2 files changed
+28 -2

No files matched your search

+6 -2
View File
@@ -59,10 +59,14 @@ export class IPCRequest {
this.payload = payload;
this.origin = window?.location?.origin;
this.#channel = new MessageChannel();
this.#promise = new Promise((resolve /*, reject*/) => {
this.#promise = new Promise((resolve, reject) => {
this.#channel.port1.onmessage = (event) => {
this.close();
resolve(event.data);
if (event.data instanceof Error) {
reject(JSON.parse(event.data.message));
} else {
resolve(event.data);
}
};
});
window.postMessage(
@@ -53,4 +53,26 @@ module('Unit | Service | ipc', function (hooks) {
test: 42,
});
});
test('IPCRequests reject if receiving an Error instance, where the rejection value is the parsed JSON Error message', async (assert) => {
assert.expect(1);
const stringifiedPayload = JSON.stringify({
foo: 'bar',
test: 42
});
const myWindow = {
postMessage(request, origin, [port]) {
// Post back an error on the response port
port.postMessage(new Error(stringifiedPayload));
}
};
try {
await new IPCRequest('hello', 'world', myWindow);
} catch (e) {
assert.deepEqual(e, {
foo: 'bar',
test: 42,
});
}
});
});