1
1
import EventEmitter from 'events' ;
2
2
import type { MenuItemConstructorOptions } from 'electron' ;
3
- import { BrowserWindow , ipcMain , Menu , app } from 'electron' ;
3
+ import { BrowserWindow , ipcMain , Menu , app , dialog } from 'electron' ;
4
4
import { expect } from 'chai' ;
5
5
import sinon from 'sinon' ;
6
6
import { createSandboxFromDefaultPreferences } from 'compass-preferences-model' ;
7
7
8
8
import type { CompassApplication } from './application' ;
9
9
import type { CompassMenu as _CompassMenu } from './menu' ;
10
+ import { quitItem } from './menu' ;
10
11
import { AutoUpdateManagerState } from './auto-update-manager' ;
11
12
12
13
function serializable < T > ( obj : T ) : T {
@@ -32,6 +33,7 @@ describe('CompassMenu', function () {
32
33
afterEach ( function ( ) {
33
34
App . removeAllListeners ( ) ;
34
35
ipcMain . removeAllListeners ( ) ;
36
+ sinon . restore ( ) ;
35
37
} ) ;
36
38
37
39
it ( 'should create an instance of Compass menu handler with initial state where no window is loaded' , function ( ) {
@@ -131,10 +133,6 @@ describe('CompassMenu', function () {
131
133
} ) ;
132
134
133
135
describe ( 'getTemplate' , function ( ) {
134
- afterEach ( function ( ) {
135
- sinon . restore ( ) ;
136
- } ) ;
137
-
138
136
it ( 'should generate a menu template that can be passed to the Electron Menu without errors' , function ( ) {
139
137
expect ( ( ) => {
140
138
const template = CompassMenu . getTemplate ( 0 ) ;
@@ -675,4 +673,57 @@ describe('CompassMenu', function () {
675
673
} ) ;
676
674
} ) ;
677
675
} ) ;
676
+
677
+ describe ( 'quitItem' , ( ) => {
678
+ it ( 'should show box if enableShowDialogOnQuit is true, then cancels and does not save changes' , async function ( ) {
679
+ await App . preferences . savePreferences ( {
680
+ enableShowDialogOnQuit : true ,
681
+ } ) ;
682
+ const showMessageBoxStub = sinon
683
+ . stub ( dialog , 'showMessageBox' )
684
+ . resolves ( { response : 1 , checkboxChecked : true } ) ;
685
+ const quitStub = sinon . stub ( app , 'quit' ) ;
686
+ const item = quitItem ( 'Quit' , App ) ;
687
+ await ( item as any ) . click ( ) ;
688
+
689
+ expect ( showMessageBoxStub ) . to . have . been . called ;
690
+ expect ( quitStub ) . not . to . have . been . called ;
691
+ expect ( App . preferences . getPreferences ( ) . enableShowDialogOnQuit ) . to . be
692
+ . true ;
693
+ } ) ;
694
+
695
+ it ( 'should show box if enableShowDialogOnQuit is true, then quits app and saves changes' , async function ( ) {
696
+ await App . preferences . savePreferences ( {
697
+ enableShowDialogOnQuit : true ,
698
+ } ) ;
699
+ const showMessageBoxStub = sinon
700
+ . stub ( dialog , 'showMessageBox' )
701
+ . resolves ( { response : 0 , checkboxChecked : true } ) ;
702
+ const quitStub = sinon . stub ( app , 'quit' ) ;
703
+ const item = quitItem ( 'Quit' , App ) ;
704
+ await ( item as any ) . click ( ) ;
705
+
706
+ expect ( showMessageBoxStub ) . to . have . been . called ;
707
+ expect ( quitStub ) . to . have . been . called ;
708
+ expect ( App . preferences . getPreferences ( ) . enableShowDialogOnQuit ) . to . be
709
+ . false ;
710
+ } ) ;
711
+
712
+ it ( 'should quit app immediately if enableShowDialogOnQuit is false and keeps changes' , async function ( ) {
713
+ await App . preferences . savePreferences ( {
714
+ enableShowDialogOnQuit : false ,
715
+ } ) ;
716
+ const showMessageBoxStub = sinon
717
+ . stub ( dialog , 'showMessageBox' )
718
+ . resolves ( { response : 0 , checkboxChecked : true } ) ;
719
+ const quitStub = sinon . stub ( app , 'quit' ) ;
720
+ const item = quitItem ( 'Quit' , App ) ;
721
+ ( item as any ) . click ( ) ;
722
+
723
+ expect ( showMessageBoxStub ) . not . to . have . been . called ;
724
+ expect ( quitStub ) . to . have . been . called ;
725
+ expect ( App . preferences . getPreferences ( ) . enableShowDialogOnQuit ) . to . be
726
+ . false ;
727
+ } ) ;
728
+ } ) ;
678
729
} ) ;
0 commit comments