Skip to content

Commit

Permalink
Merge pull request #171 from electron/properly-quit
Browse files Browse the repository at this point in the history
fix: On quit, quit properly
  • Loading branch information
felixrieseberg authored Jan 30, 2019
2 parents f7a19b9 + f8f1f0e commit f1e51c7
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 4 deletions.
10 changes: 10 additions & 0 deletions src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ export async function onReady() {
setupDialogs();
}

/**
* Handle the "before-quit" event
*
* @export
*/
export function onBeforeQuit() {
(global as any).isQuitting = true;
}

/**
* All windows have been closed, quit on anything but
* macOS.
Expand Down Expand Up @@ -62,6 +71,7 @@ export function main() {

// Launch
app.on('ready', onReady);
app.on('before-quit', onBeforeQuit);
app.on('window-all-closed', onWindowsAllClosed);
app.on('activate', getOrCreateMainWindow);
}
Expand Down
11 changes: 10 additions & 1 deletion src/renderer/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,16 @@ export class AppState {
// The user confirmed, let's close for real.
if (this.warningDialogLastResult) {
window.onbeforeunload = null;
window.close();

// Should we just close or quit?
const remote = require('electron').remote;
const isQuitting = remote.getGlobal('isQuitting');

if (isQuitting) {
remote.app.quit();
} else {
window.close();
}
}
});

Expand Down
13 changes: 11 additions & 2 deletions tests/main/main-spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { app } from 'electron';

import { main, onReady, onWindowsAllClosed } from '../../src/main/main';
import { main, onBeforeQuit, onReady, onWindowsAllClosed } from '../../src/main/main';
import { shouldQuit } from '../../src/main/squirrel';
import { setupUpdates } from '../../src/main/update';
import { getOrCreateMainWindow } from '../../src/main/windows';
Expand Down Expand Up @@ -48,7 +48,16 @@ describe('main', () => {

it('listens to core events', () => {
main();
expect(app.on).toHaveBeenCalledTimes(4);
expect(app.on).toHaveBeenCalledTimes(5);
});
});

describe('onBeforeQuit()', () => {
it('sets a global', () => {
onBeforeQuit();

expect((global as any).isQuitting).toBe(true);
(global as any).isQuitting = false;
});
});

Expand Down
1 change: 1 addition & 0 deletions tests/mocks/electron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ const electronMock = {
session,
BrowserWindow: MockBrowserWindow,
getCurrentWindow: jest.fn(),
getGlobal: jest.fn(),
Menu: MockMenu,
MenuItem: MockMenuItem,
process: {
Expand Down
22 changes: 21 additions & 1 deletion tests/renderer/state-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,27 @@ describe('AppState', () => {
});
});

it('closes the window', (done) => {
it('closes the app', (done) => {
const { remote } = require('electron');
window.close = jest.fn();
appState.isUnsaved = true;
(remote.getGlobal as jest.Mock).mockReturnValueOnce(true);
expect(window.onbeforeunload).toBeTruthy();

const result = window.onbeforeunload!(undefined as any);
expect(result).toBe(false);
expect(appState.isWarningDialogShowing).toBe(true);

appState.warningDialogLastResult = true;
appState.isWarningDialogShowing = false;
process.nextTick(() => {
expect(window.close).toHaveBeenCalledTimes(0);
expect(remote.app.quit).toHaveBeenCalledTimes(1);
done();
});
});

it('does not close the window', (done) => {
window.close = jest.fn();
appState.isUnsaved = true;
expect(window.onbeforeunload).toBeTruthy();
Expand Down

0 comments on commit f1e51c7

Please sign in to comment.