rx-ipc-electron
Version:
Pass RxJS Observables through IPC in Electron
110 lines (102 loc) • 3.04 kB
text/typescript
import { expect } from 'chai';
import { ipcRenderer } from 'electron';
import { slow, suite, test, timeout } from 'mocha-typescript';
import { Observable } from 'rxjs';
import rxIpc from '../src/renderer';
('Rx-Electron-IPC')
class Main {
'It should pass an Observable from main to renderer'() {
const results = [];
return new Promise((resolve) => {
rxIpc.runCommand('test-main', null, 1, 2, {test: 'passed'})
.subscribe(
(data) => {
results.push(data);
},
(err) => {
throw err;
},
() => {
expect(results).to.deep.equal([1, 2, {test: 'passed'}]);
resolve();
}
);
});
}
'It should correctly pass an error'() {
const results = [];
return new Promise((resolve) => {
rxIpc.runCommand('test-error', null)
.subscribe(
(data) => {
results.push(data);
},
(err) => {
expect(results).to.deep.equal([1, 2]);
expect(err).to.equal('Test Error');
resolve();
}
);
});
}
'It should handle two instances of the same command'() {
const results = [];
return new Promise((resolve) => {
rxIpc.runCommand('test-main', null, 1, 2, 3)
.concat(rxIpc.runCommand('test-main', null, 4, 5, 6))
.subscribe(
(data) => {
results.push(data);
},
(err) => {
throw err;
},
() => {
expect(results).to.deep.equal([1, 2, 3, 4, 5, 6]);
resolve();
}
);
});
}
'The renderer should run a command from the main process'() {
return new Promise((resolve) => {
function testCommand(...args) {
return Observable.from(args);
}
ipcRenderer.on('results-from-main', (event, results) => {
expect(results).to.deep.equal([3, 2, 1]);
resolve();
});
rxIpc.registerListener('command-from-main', testCommand);
ipcRenderer.send('main-run-command');
});
}
'It should throw an error if given an unregistered command'() {
return new Promise((resolve, reject) => {
rxIpc.runCommand('invalid', null)
.subscribe(
(data) => {
reject('We should not receive data here.');
},
(err) => {
expect(err).to.equal('Invalid channel: invalid');
resolve();
}
);
});
}
'It should clean up listeners'() {
function noop() {
return new Observable();
}
rxIpc.registerListener('remove-test-1', noop);
rxIpc.registerListener('remove-test-1', noop);
expect(rxIpc._getListenerCount('remove-test-1')).to.equal(2);
rxIpc.removeListeners('remove-test-1');
expect(rxIpc._getListenerCount('remove-test-1')).to.equal(0);
rxIpc.registerListener('remove-test-2', noop);
expect(rxIpc._getListenerCount('remove-test-2')).to.equal(1);
rxIpc.cleanUp();
expect(rxIpc._getListenerCount('remove-test-2')).to.equal(0);
}
}