@@ -53,10 +53,46 @@ jest.mock('uuid', () => ({
53
53
v4 : ( ) : string => manualAttributionUuid ,
54
54
} ) ) ;
55
55
56
+ type SendCall = {
57
+ channel : string ;
58
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
59
+ args : Array < any > ;
60
+ } ;
61
+
62
+ class MockWebContents {
63
+ #calls: Array < SendCall > = [ ] ;
64
+
65
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
66
+ send ( channel : string , args : Array < any > ) : void {
67
+ this . #calls. push ( { channel, args } ) ;
68
+ }
69
+
70
+ #callsFromChannel( channel : AllowedFrontendChannels ) : Array < SendCall > {
71
+ return this . #calls. filter (
72
+ ( sendCall ) => sendCall . channel === String ( channel ) ,
73
+ ) ;
74
+ }
75
+
76
+ numberOfCallsFromChannel ( channel : AllowedFrontendChannels ) : number {
77
+ return this . #callsFromChannel( channel ) . length ;
78
+ }
79
+
80
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
81
+ lastArgumentFromChannel ( channel : AllowedFrontendChannels ) : any {
82
+ const callsFromChannel = this . #callsFromChannel( channel ) ;
83
+ if ( callsFromChannel . length ) {
84
+ return callsFromChannel [ callsFromChannel . length - 1 ] . args ;
85
+ }
86
+ return undefined ;
87
+ }
88
+
89
+ reset ( ) : void {
90
+ this . #calls = [ ] ;
91
+ }
92
+ }
93
+
56
94
const mainWindow = {
57
- webContents : {
58
- send : jest . fn ( ) ,
59
- } ,
95
+ webContents : new MockWebContents ( ) ,
60
96
setTitle : jest . fn ( ) ,
61
97
} as unknown as BrowserWindow ;
62
98
@@ -184,6 +220,7 @@ const validMetadata = {
184
220
describe ( 'Test of loading function' , ( ) => {
185
221
afterEach ( ( ) => {
186
222
jest . resetAllMocks ( ) ;
223
+ ( mainWindow . webContents as unknown as MockWebContents ) . reset ( ) ;
187
224
} ) ;
188
225
189
226
it ( 'handles Parsing error correctly' , async ( ) => {
@@ -209,10 +246,15 @@ describe('Test of loading function', () => {
209
246
210
247
await loadInputAndOutputFromFilePath ( mainWindow , corruptJsonPath ) ;
211
248
212
- const expectedNumberOfCalls = 3 ;
213
- expect ( mainWindow . webContents . send ) . toHaveBeenCalledTimes (
214
- expectedNumberOfCalls ,
215
- ) ;
249
+ const webContents = mainWindow . webContents as unknown as MockWebContents ;
250
+ expect (
251
+ webContents . numberOfCallsFromChannel (
252
+ AllowedFrontendChannels . ResetLoadedFile ,
253
+ ) ,
254
+ ) . toBe ( 2 ) ;
255
+ expect (
256
+ webContents . numberOfCallsFromChannel ( AllowedFrontendChannels . FileLoaded ) ,
257
+ ) . toBe ( 1 ) ;
216
258
217
259
expect ( getGlobalBackendState ( ) ) . toEqual ( expectedBackendState ) ;
218
260
} ) ;
@@ -273,11 +315,18 @@ describe('Test of loading function', () => {
273
315
setGlobalBackendState ( { } ) ;
274
316
await loadInputAndOutputFromFilePath ( mainWindow , opossumPath ) ;
275
317
276
- expect ( mainWindow . webContents . send ) . toHaveBeenLastCalledWith (
277
- AllowedFrontendChannels . FileLoaded ,
278
- expectedFileContent ,
279
- ) ;
280
- expect ( mainWindow . webContents . send ) . toHaveBeenCalledTimes ( 2 ) ;
318
+ const webContents = mainWindow . webContents as unknown as MockWebContents ;
319
+ expect (
320
+ webContents . lastArgumentFromChannel ( AllowedFrontendChannels . FileLoaded ) ,
321
+ ) . toEqual ( expectedFileContent ) ;
322
+ expect (
323
+ webContents . numberOfCallsFromChannel ( AllowedFrontendChannels . FileLoaded ) ,
324
+ ) . toBe ( 1 ) ;
325
+ expect (
326
+ webContents . numberOfCallsFromChannel (
327
+ AllowedFrontendChannels . ResetLoadedFile ,
328
+ ) ,
329
+ ) . toBe ( 1 ) ;
281
330
282
331
expect ( dialog . showMessageBox ) . not . toHaveBeenCalled ( ) ;
283
332
} ) ;
@@ -292,11 +341,20 @@ describe('Test of loading function', () => {
292
341
setGlobalBackendState ( { } ) ;
293
342
await loadInputAndOutputFromFilePath ( mainWindow , jsonPath ) ;
294
343
295
- expect ( mainWindow . webContents . send ) . toHaveBeenCalledTimes ( 2 ) ;
296
- expect ( mainWindow . webContents . send ) . toHaveBeenLastCalledWith (
297
- AllowedFrontendChannels . FileLoaded ,
298
- expectedFileContent ,
299
- ) ;
344
+ const webContents = mainWindow . webContents as unknown as MockWebContents ;
345
+ expect (
346
+ webContents . numberOfCallsFromChannel (
347
+ AllowedFrontendChannels . FileLoaded ,
348
+ ) ,
349
+ ) . toBe ( 1 ) ;
350
+ expect (
351
+ webContents . lastArgumentFromChannel ( AllowedFrontendChannels . FileLoaded ) ,
352
+ ) . toEqual ( expectedFileContent ) ;
353
+ expect (
354
+ webContents . numberOfCallsFromChannel (
355
+ AllowedFrontendChannels . ResetLoadedFile ,
356
+ ) ,
357
+ ) . toBe ( 1 ) ;
300
358
301
359
expect ( dialog . showMessageBox ) . not . toHaveBeenCalled ( ) ;
302
360
} ) ;
@@ -313,11 +371,20 @@ describe('Test of loading function', () => {
313
371
setGlobalBackendState ( { } ) ;
314
372
await loadInputAndOutputFromFilePath ( mainWindow , jsonPath ) ;
315
373
316
- expect ( mainWindow . webContents . send ) . toHaveBeenCalledTimes ( 2 ) ;
317
- expect ( mainWindow . webContents . send ) . toHaveBeenLastCalledWith (
318
- AllowedFrontendChannels . FileLoaded ,
319
- expectedFileContent ,
320
- ) ;
374
+ const webContents = mainWindow . webContents as unknown as MockWebContents ;
375
+ expect (
376
+ webContents . numberOfCallsFromChannel (
377
+ AllowedFrontendChannels . FileLoaded ,
378
+ ) ,
379
+ ) . toBe ( 1 ) ;
380
+ expect (
381
+ webContents . lastArgumentFromChannel ( AllowedFrontendChannels . FileLoaded ) ,
382
+ ) . toEqual ( expectedFileContent ) ;
383
+ expect (
384
+ webContents . numberOfCallsFromChannel (
385
+ AllowedFrontendChannels . ResetLoadedFile ,
386
+ ) ,
387
+ ) . toBe ( 1 ) ;
321
388
expect ( dialog . showMessageBox ) . not . toHaveBeenCalled ( ) ;
322
389
} ) ;
323
390
} ) ;
@@ -519,10 +586,10 @@ describe('Test of loading function', () => {
519
586
} ,
520
587
} ;
521
588
522
- expect ( mainWindow . webContents . send ) . toHaveBeenLastCalledWith (
523
- AllowedFrontendChannels . FileLoaded ,
524
- expectedLoadedFile ,
525
- ) ;
589
+ const webContents = mainWindow . webContents as unknown as MockWebContents ;
590
+ expect (
591
+ webContents . lastArgumentFromChannel ( AllowedFrontendChannels . FileLoaded ) ,
592
+ ) . toEqual ( expectedLoadedFile ) ;
526
593
expect ( dialog . showMessageBox ) . not . toHaveBeenCalled ( ) ;
527
594
} ,
528
595
) ;
@@ -558,10 +625,10 @@ describe('Test of loading function', () => {
558
625
metadata : inputFileContentWithCustomMetadata . metadata ,
559
626
} ;
560
627
561
- expect ( mainWindow . webContents . send ) . toHaveBeenLastCalledWith (
562
- AllowedFrontendChannels . FileLoaded ,
563
- expectedLoadedFile ,
564
- ) ;
628
+ const webContents = mainWindow . webContents as unknown as MockWebContents ;
629
+ expect (
630
+ webContents . lastArgumentFromChannel ( AllowedFrontendChannels . FileLoaded ) ,
631
+ ) . toEqual ( expectedLoadedFile ) ;
565
632
expect ( dialog . showMessageBox ) . not . toHaveBeenCalled ( ) ;
566
633
} ) ;
567
634
@@ -613,11 +680,18 @@ describe('Test of loading function', () => {
613
680
} ,
614
681
} ;
615
682
616
- expect ( mainWindow . webContents . send ) . toHaveBeenCalledTimes ( 2 ) ;
617
- expect ( mainWindow . webContents . send ) . toHaveBeenLastCalledWith (
618
- AllowedFrontendChannels . FileLoaded ,
619
- expectedLoadedFile ,
620
- ) ;
683
+ const webContents = mainWindow . webContents as unknown as MockWebContents ;
684
+ expect (
685
+ webContents . lastArgumentFromChannel ( AllowedFrontendChannels . FileLoaded ) ,
686
+ ) . toEqual ( expectedLoadedFile ) ;
687
+ expect (
688
+ webContents . numberOfCallsFromChannel ( AllowedFrontendChannels . FileLoaded ) ,
689
+ ) . toBe ( 1 ) ;
690
+ expect (
691
+ webContents . numberOfCallsFromChannel (
692
+ AllowedFrontendChannels . ResetLoadedFile ,
693
+ ) ,
694
+ ) . toBe ( 1 ) ;
621
695
} ) ;
622
696
} ) ;
623
697
@@ -644,10 +718,13 @@ function assertFileLoadedCorrectly(testUuid: string): void {
644
718
} ,
645
719
} ;
646
720
647
- expect ( mainWindow . webContents . send ) . toHaveBeenCalledWith (
648
- AllowedFrontendChannels . FileLoaded ,
649
- expectedLoadedFile ,
650
- ) ;
721
+ const webContents = mainWindow . webContents as unknown as MockWebContents ;
722
+ expect (
723
+ webContents . lastArgumentFromChannel ( AllowedFrontendChannels . FileLoaded ) ,
724
+ ) . toEqual ( expectedLoadedFile ) ;
725
+ expect (
726
+ webContents . numberOfCallsFromChannel ( AllowedFrontendChannels . FileLoaded ) ,
727
+ ) . toBe ( 1 ) ;
651
728
expect ( dialog . showMessageBox ) . not . toHaveBeenCalled ( ) ;
652
729
}
653
730
0 commit comments