I have a multiwindow electron application, namely it consists of a launcher window and a main window.
The main process is responsible for first showing the launcher, and after everything is initialized it gets an event over ipc and shows the main window, hiding the launcher.
I use on('close')
on the main window to detect when a user closes the main window, to show the launcher again and do some teardown logic, after that is finished the app quits.
this.mainWindow = new BrowserWindow({width: 1024, height: 768, webPreferences: {nodeIntegration: true}});
this.mainWindow.setMenu(null);
this.mainWindow.on('close', () => {
this.logger.debug('"close" received');
this.mainWindow.hide();
this.launcherWindow.show();
this.sendShutdown();
});
This works quite well, now I want to integration test this behaviour with Spectron. I have a problem sending the close signal to the window, emulating the user clicking close.
it('should start shutting down the services gracefully if the user clicks on the close button', async () => {
await app.client.windowByIndex(1);
await app.client.close();
//..... expectations that verify the launcher window is visible now
});
I have verified that the window with index 1 is the main window. When I call app.client.close();
the main window closes, but I can see in the logs that the on('close', )
event of the main window is not triggered, therefore it does not jump back to the launcher.
Is there anything I'm missing / misunderstanding?
I ended up wrapping the code that was executed in an extra method and also binding that method to an ipcMain.on("should-shutdown",..
event. This made sense anyway, since other renderers shouls also be able to request a smooth shutdown.
In the Spectron test I then used
const { ipcRenderer } = app.electron;
ipcRenderer.send('smooth-shutdown');
instead of app.client.close()
to initiate the shutdown sequence.