electron/spec-main/api-ipc-main-spec.ts
Samuel Attard 0d2e967960
feat: expose electron/{process} typed modules (#22937)
* feat: expose electron/{process} typed modules

* chore: update imports for common modules

* chore: update typescript generator

* chore: remap electron/* to the internal packages
2020-04-06 17:04:09 -07:00

63 lines
1.9 KiB
TypeScript

import { expect } from 'chai';
import * as path from 'path';
import * as cp from 'child_process';
import { closeAllWindows } from './window-helpers';
import { emittedOnce } from './events-helpers';
import { ipcMain, BrowserWindow } from 'electron/main';
describe('ipc main module', () => {
const fixtures = path.join(__dirname, 'fixtures');
afterEach(closeAllWindows);
describe('ipc.sendSync', () => {
afterEach(() => { ipcMain.removeAllListeners('send-sync-message'); });
it('does not crash when reply is not sent and browser is destroyed', (done) => {
const w = new BrowserWindow({
show: false,
webPreferences: {
nodeIntegration: true
}
});
ipcMain.once('send-sync-message', (event) => {
event.returnValue = null;
done();
});
w.loadFile(path.join(fixtures, 'api', 'send-sync-message.html'));
});
it('does not crash when reply is sent by multiple listeners', (done) => {
const w = new BrowserWindow({
show: false,
webPreferences: {
nodeIntegration: true
}
});
ipcMain.on('send-sync-message', (event) => {
event.returnValue = null;
});
ipcMain.on('send-sync-message', (event) => {
event.returnValue = null;
done();
});
w.loadFile(path.join(fixtures, 'api', 'send-sync-message.html'));
});
});
describe('ipcMain.on', () => {
it('is not used for internals', async () => {
const appPath = path.join(fixtures, 'api', 'ipc-main-listeners');
const electronPath = process.execPath;
const appProcess = cp.spawn(electronPath, [appPath]);
let output = '';
appProcess.stdout.on('data', (data) => { output += data; });
await emittedOnce(appProcess.stdout, 'end');
output = JSON.parse(output);
expect(output).to.deep.equal(['error']);
});
});
});