1+ import expect from 'expect' ;
12import { createAppMenu , MenuItemHandlers } from '@electron-app/app-menu' ;
2- import { expect , use } from 'chai' ;
33import { MenuItemConstructorOptions } from 'electron' ;
4- import sinonChai from 'sinon-chai' ;
5- import { StubbedInstance , stubObject } from 'ts-sinon' ;
6-
7- use ( sinonChai ) ;
4+ import { DeepMockProxy , mockDeep } from 'jest-mock-extended' ;
85
96describe ( 'Create Electron App Menu' , ( ) => {
10- const handlerObj : MenuItemHandlers = {
11- onFindClicked : ( ) => undefined ,
12- onNewClicked : ( ) => undefined ,
13- onOpenClicked : ( ) => undefined ,
14- onOpenRecentClicked : ( ) => undefined ,
15- onPreferencesClicked : ( ) => undefined ,
16- onAboutClicked : ( ) => undefined ,
17- onQuitClicked : ( ) => undefined ,
18- onRedoClicked : ( ) => undefined ,
19- onReplaceClicked : ( ) => undefined ,
20- onSaveAsClicked : ( ) => undefined ,
21- onSaveClicked : ( ) => undefined ,
22- onUndoClicked : ( ) => undefined ,
23- } ;
24- let handlers : StubbedInstance < MenuItemHandlers > ;
7+ let handlers : DeepMockProxy < MenuItemHandlers > ;
258
269 beforeEach ( ( ) => {
27- handlers = stubObject < MenuItemHandlers > ( handlerObj ) ;
10+ jest . resetAllMocks ( ) ;
11+ handlers = mockDeep < MenuItemHandlers > ( ) ;
2812
29- Object . keys ( handlerObj )
13+ Object . keys ( handlers )
3014 . filter ( ( name ) => name . startsWith ( 'on' ) )
31- . forEach ( ( funcName ) => {
32- // @ts -expect-error types are lost here due to stubbing
33- handlers [ funcName ]
34- . onSecondCall ( )
35- . throws ( new Error ( `${ funcName } was registered multiple times!` ) ) ;
15+ . forEach ( ( funcName : keyof MenuItemHandlers ) => {
16+ handlers [ funcName ] . mockReturnValueOnce ( ) . mockImplementation ( ( ) => {
17+ new Error ( `${ funcName } was registered multiple times!` ) ;
18+ } ) ;
3619 } ) ;
3720 } ) ;
3821
3922 it ( 'actually creates a menu' , ( ) => {
4023 const actual = createAppMenu ( 'MyApp' , 'linux' , handlers ) ;
4124
42- expect ( actual ) . to . not . be . null ;
43- expect ( actual ) . to . not . be . empty ;
25+ expect ( actual ) . not . toBeNull ( ) ;
26+ expect ( Object . keys ( actual ) ) . not . toHaveLength ( 0 ) ;
4427 } ) ;
4528
4629 it ( 'includes the Mac menu when on Mac' , ( ) => {
4730 const actual = createAppMenu ( 'MyApp' , 'darwin' , handlers ) ;
4831
49- expect ( actual [ 0 ] . label ) . to . be . equal ( 'MyApp' ) ;
32+ expect ( actual [ 0 ] . label ) . toBe ( 'MyApp' ) ;
5033 } ) ;
5134
5235 it ( "doesn't show prefs in File when on Mac" , ( ) => {
5336 const actual = createAppMenu ( 'MyApp' , 'darwin' , handlers ) ;
5437
55- expect ( actual [ 1 ] . submenu ) . to . not . containSubset ( [ { label : 'Preferences' } ] ) ;
38+ expect ( actual [ 1 ] . submenu ) . not . toContainEqual ( [ { label : 'Preferences' } ] ) ;
5639 } ) ;
5740
5841 it ( "doesn't show quit in File when on Mac" , ( ) => {
5942 const actual = createAppMenu ( 'MyApp' , 'darwin' , handlers ) ;
6043
61- expect ( actual [ 1 ] . submenu ) . to . not . containSubset ( [ { label : 'Quit' } ] ) ;
44+ expect ( actual [ 1 ] . submenu ) . not . toContainEqual ( [ { label : 'Quit' } ] ) ;
6245 } ) ;
6346
6447 it ( "doesn't show about in File when on Mac" , ( ) => {
6548 const actual = createAppMenu ( 'MyApp' , 'darwin' , handlers ) ;
6649
67- expect ( actual [ 1 ] . submenu ) . to . not . containSubset ( [
50+ expect ( actual [ 1 ] . submenu ) . not . toContainEqual ( [
6851 { label : 'About Lyricistant...' } ,
6952 ] ) ;
7053 } ) ;
7154
7255 it ( 'does show about in Mac menu when on Mac' , ( ) => {
7356 const actual = createAppMenu ( 'MyApp' , 'darwin' , handlers ) ;
7457
75- expect ( actual [ 0 ] . submenu ) . to . containSubset ( [
76- { label : 'About Lyricistant...' } ,
77- ] ) ;
58+ expect ( actual [ 0 ] . submenu ) . toContainEqual (
59+ expect . objectContaining ( { label : 'About Lyricistant...' } ) ,
60+ ) ;
7861 } ) ;
7962
8063 it ( 'does show prefs in File when on Windows' , ( ) => {
8164 const actual = createAppMenu ( 'MyApp' , 'win32' , handlers ) ;
8265
83- expect ( actual [ 0 ] . submenu ) . to . containSubset ( [ { label : 'Preferences' } ] ) ;
66+ expect ( actual [ 0 ] . submenu ) . toContainEqual (
67+ expect . objectContaining ( { label : 'Preferences' } ) ,
68+ ) ;
8469 } ) ;
8570
8671 it ( 'does show quit in File when on Windows' , ( ) => {
8772 const actual = createAppMenu ( 'MyApp' , 'win32' , handlers ) ;
8873
89- expect ( actual [ 0 ] . submenu ) . to . containSubset ( [ { label : 'Quit' } ] ) ;
74+ expect ( actual [ 0 ] . submenu ) . toContainEqual (
75+ expect . objectContaining ( { label : 'Quit' } ) ,
76+ ) ;
9077 } ) ;
9178
9279 it ( 'does show about in File when on Windows' , ( ) => {
9380 const actual = createAppMenu ( 'MyApp' , 'win32' , handlers ) ;
9481
95- expect ( actual [ 0 ] . submenu ) . to . containSubset ( [
96- { label : 'About Lyricistant...' } ,
97- ] ) ;
82+ expect ( actual [ 0 ] . submenu ) . toContainEqual (
83+ expect . objectContaining ( { label : 'About Lyricistant...' } ) ,
84+ ) ;
9885 } ) ;
9986
10087 it ( 'does show prefs in File when on Linux' , ( ) => {
10188 const actual = createAppMenu ( 'MyApp' , 'linux' , handlers ) ;
10289
103- expect ( actual [ 0 ] . submenu ) . to . containSubset ( [ { label : 'Preferences' } ] ) ;
90+ expect ( actual [ 0 ] . submenu ) . toContainEqual (
91+ expect . objectContaining ( { label : 'Preferences' } ) ,
92+ ) ;
10493 } ) ;
10594
10695 it ( 'does show quit in File when on Linux' , ( ) => {
10796 const actual = createAppMenu ( 'MyApp' , 'linux' , handlers ) ;
10897
109- expect ( actual [ 0 ] . submenu ) . to . containSubset ( [ { label : 'Quit' } ] ) ;
98+ expect ( actual [ 0 ] . submenu ) . toContainEqual (
99+ expect . objectContaining ( { label : 'Quit' } ) ,
100+ ) ;
110101 } ) ;
111102
112103 it ( 'includes all of the major sections on Mac' , ( ) => {
@@ -119,7 +110,7 @@ describe('Create Electron App Menu', () => {
119110 const actual = createAppMenu ( 'MyApp' , 'darwin' , handlers ) ;
120111
121112 actual . forEach ( ( menu , index ) => {
122- expect ( menu ) . to . include ( expected [ index ] ) ;
113+ expect ( menu ) . toMatchObject ( expected [ index ] ) ;
123114 } ) ;
124115 } ) ;
125116
@@ -128,7 +119,7 @@ describe('Create Electron App Menu', () => {
128119 const actual = createAppMenu ( 'MyApp' , 'linux' , handlers ) ;
129120
130121 actual . forEach ( ( menu , index ) => {
131- expect ( menu ) . to . include ( expected [ index ] ) ;
122+ expect ( menu ) . toMatchObject ( expected [ index ] ) ;
132123 } ) ;
133124 } ) ;
134125
@@ -137,7 +128,7 @@ describe('Create Electron App Menu', () => {
137128 const actual = createAppMenu ( 'MyApp' , 'win32' , handlers ) ;
138129
139130 actual . forEach ( ( menu , index ) => {
140- expect ( menu ) . to . include ( expected [ index ] ) ;
131+ expect ( menu ) . toMatchObject ( expected [ index ] ) ;
141132 } ) ;
142133 } ) ;
143134
@@ -147,8 +138,8 @@ describe('Create Electron App Menu', () => {
147138 ( { label } ) => label === 'Open recent' ,
148139 ) ;
149140
150- expect ( recents ) . to . exist ;
151- expect ( recents . submenu ) . to . eql ( [
141+ expect ( recents ) . toBeDefined ( ) ;
142+ expect ( recents . submenu ) . toEqual ( [
152143 {
153144 label : '<No recent files>' ,
154145 enabled : false ,
@@ -163,9 +154,9 @@ describe('Create Electron App Menu', () => {
163154 ( { label } ) => label === 'Open recent' ,
164155 ) ;
165156
166- expect ( recents ) . to . exist ;
157+ expect ( recents ) . toBeDefined ( ) ;
167158 ( recents . submenu as MenuItemConstructorOptions [ ] ) . forEach ( ( menu , index ) => {
168- expect ( menu ) . to . include ( { label : recentFiles [ index ] } ) ;
159+ expect ( menu ) . toMatchObject ( { label : recentFiles [ index ] } ) ;
169160 } ) ;
170161 } ) ;
171162
0 commit comments