1
1
import { dialog , IpcMainInvokeEvent } from 'electron' ;
2
2
import { getDesktopPath } from '@/lib/utils/desktop-path' ;
3
3
import { IpcEvent } from '@/enum/ipc-event' ;
4
- import { ConversionHandler } from '@/lib/conversion/conversion-handler' ;
5
- import { configureIpcHandlers } from '@/lib/system/ipc-handlers' ;
4
+ import { configureIpcHandlers , conversionHandler } from '@/lib/system/ipc-handlers' ;
6
5
7
6
// Mock the dependencies
8
7
jest . mock ( 'electron' , ( ) => ( {
@@ -13,27 +12,21 @@ jest.mock('electron', () => ({
13
12
jest . mock ( '@/lib/utils/desktop-path' , ( ) => ( {
14
13
getDesktopPath : jest . fn ( ) ,
15
14
} ) ) ;
16
- jest . mock ( '@/lib/conversion/conversion-handler' , ( ) => {
17
- return {
18
- ConversionHandler : jest . fn ( ) . mockImplementation ( ( ) => ( {
19
- handle : jest . fn ( ) ,
20
- cancelAll : jest . fn ( ) ,
21
- cancel : jest . fn ( ) ,
22
- } ) ) ,
23
- } ;
24
- } ) ;
15
+ jest . mock ( '@/lib/conversion/conversion-handler' ) ;
25
16
26
17
describe ( 'configureIpcHandlers' , ( ) => {
27
18
let ipcMainMock : any ;
28
- let conversionHandlerMock : any ;
19
+ let conversionHandlerMock : jest . Mocked < typeof conversionHandler > ;
29
20
30
21
beforeEach ( ( ) => {
31
22
ipcMainMock = {
32
23
handle : jest . fn ( ) ,
33
24
} ;
34
25
35
26
jest . clearAllMocks ( ) ;
36
- conversionHandlerMock = new ConversionHandler ( ) ;
27
+
28
+ // Mock the exported conversionHandler instance
29
+ conversionHandlerMock = conversionHandler as jest . Mocked < typeof conversionHandler > ;
37
30
} ) ;
38
31
39
32
it ( 'should handle GET_DESKTOP_PATH and call getDesktopPath()' , ( ) => {
@@ -77,14 +70,10 @@ describe('configureIpcHandlers', () => {
77
70
it ( 'should handle CONVERT_MEDIA and call conversionHandler.handle()' , async ( ) => {
78
71
configureIpcHandlers ( ipcMainMock ) ;
79
72
80
- const mockHandle = conversionHandlerMock . handle as jest . Mock ;
81
- mockHandle . mockResolvedValue ( 'conversion-success' ) ;
73
+ conversionHandlerMock . handle . mockResolvedValue ( 'conversion-success' ) ; // Mock the result
82
74
83
75
const handler = ipcMainMock . handle . mock . calls . find ( ( call : any ) => call [ 0 ] === IpcEvent . CONVERT_MEDIA ) [ 1 ] ;
84
76
85
- console . log ( handler ) ;
86
-
87
-
88
77
const mockEvent = { } as IpcMainInvokeEvent ;
89
78
const mediaParams = {
90
79
id : '1' ,
@@ -96,7 +85,7 @@ describe('configureIpcHandlers', () => {
96
85
97
86
const result = await handler ( mockEvent , mediaParams ) ;
98
87
99
- expect ( mockHandle ) . toHaveBeenCalledWith (
88
+ expect ( conversionHandlerMock . handle ) . toHaveBeenCalledWith (
100
89
'1' ,
101
90
'/path/to/file' ,
102
91
'mp4' ,
@@ -110,24 +99,24 @@ describe('configureIpcHandlers', () => {
110
99
it ( 'should handle CANCEL_CONVERSION and call conversionHandler.cancelAll()' , ( ) => {
111
100
configureIpcHandlers ( ipcMainMock ) ;
112
101
113
- const mockCancelAll = conversionHandlerMock . cancelAll as jest . Mock ;
102
+ conversionHandlerMock . cancelAll . mockReturnValue ( undefined ) ; // Mock the result
114
103
115
104
const handler = ipcMainMock . handle . mock . calls . find ( ( call : any ) => call [ 0 ] === IpcEvent . CANCEL_CONVERSION ) [ 1 ] ;
116
105
const result = handler ( ) ;
117
106
118
- expect ( mockCancelAll ) . toHaveBeenCalled ( ) ;
119
- expect ( result ) . toBe ( 'cancel-all-success' ) ;
107
+ expect ( conversionHandlerMock . cancelAll ) . toHaveBeenCalled ( ) ;
108
+ expect ( result ) . toBe ( undefined ) ;
120
109
} ) ;
121
110
122
111
it ( 'should handle CANCEL_ITEM_CONVERSION and call conversionHandler.cancel()' , ( ) => {
123
112
configureIpcHandlers ( ipcMainMock ) ;
124
113
125
- const mockCancel = conversionHandlerMock . cancel as jest . Mock ;
114
+ conversionHandlerMock . cancel . mockReturnValue ( undefined as any ) ; // Mock the result
126
115
127
116
const handler = ipcMainMock . handle . mock . calls . find ( ( call : any ) => call [ 0 ] === IpcEvent . CANCEL_ITEM_CONVERSION ) [ 1 ] ;
128
117
const result = handler ( { } , '1' ) ;
129
118
130
- expect ( mockCancel ) . toHaveBeenCalledWith ( '1' ) ;
131
- expect ( result ) . toBe ( 'cancel-item-success' ) ;
119
+ expect ( conversionHandlerMock . cancel ) . toHaveBeenCalledWith ( '1' ) ;
120
+ expect ( result ) . toBe ( undefined ) ;
132
121
} ) ;
133
122
} ) ;
0 commit comments