|
1 |
| -/** |
2 |
| - * @jest-environment node |
3 |
| - */ |
4 |
| - |
5 |
| -import { configureIpcHandlers } from '../../src/lib/system/ipc-handlers'; |
6 |
| -import { IpcEvent } from '../../src/enum/ipc-event'; |
7 |
| -import { dialog, ipcMain, IpcMainInvokeEvent } from 'electron'; |
8 |
| -import { getDesktopPath } from '../../src/lib/utils/desktop-path'; |
9 |
| -import { ConversionHandler } from '../../src/lib/conversion/conversion-handler'; |
10 |
| - |
11 |
| -jest.mock('../../src/lib/utils/desktop-path'); |
12 |
| -jest.mock('../../src/lib/conversion/conversion-handler'); |
| 1 | +import { dialog, IpcMainInvokeEvent } from 'electron'; |
| 2 | +import { getDesktopPath } from '@/lib/utils/desktop-path'; |
| 3 | +import { IpcEvent } from '@/enum/ipc-event'; |
| 4 | +import { ConversionHandler } from '@/lib/conversion/conversion-handler'; |
| 5 | +import { configureIpcHandlers } from '@/lib/system/ipc-handlers'; |
| 6 | + |
| 7 | +// Mock the dependencies |
| 8 | +jest.mock('electron', () => ({ |
| 9 | + dialog: { |
| 10 | + showOpenDialog: jest.fn(), |
| 11 | + }, |
| 12 | +})); |
| 13 | +jest.mock('@/lib/utils/desktop-path', () => ({ |
| 14 | + getDesktopPath: jest.fn(), |
| 15 | +})); |
| 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 | +}); |
13 | 25 |
|
14 | 26 | describe('configureIpcHandlers', () => {
|
15 |
| - let mockConversionHandler: jest.Mocked<ConversionHandler>; |
| 27 | + let ipcMainMock: any; |
| 28 | + let conversionHandlerMock: any; |
16 | 29 |
|
17 | 30 | beforeEach(() => {
|
18 |
| - jest.resetAllMocks(); |
19 |
| - mockConversionHandler = new ConversionHandler() as jest.Mocked<ConversionHandler>; |
20 |
| - }); |
| 31 | + ipcMainMock = { |
| 32 | + handle: jest.fn(), |
| 33 | + }; |
21 | 34 |
|
22 |
| - afterEach(() => { |
23 |
| - ipcMain.removeAllListeners(); |
| 35 | + jest.clearAllMocks(); |
| 36 | + conversionHandlerMock = new ConversionHandler(); |
24 | 37 | });
|
25 | 38 |
|
26 |
| - test('should handle GET_DESKTOP_PATH', async () => { |
27 |
| - const mockGetDesktopPath = jest.mocked(getDesktopPath); |
28 |
| - mockGetDesktopPath.mockReturnValue('/mock/desktop/path'); |
| 39 | + it('should handle GET_DESKTOP_PATH and call getDesktopPath()', () => { |
| 40 | + configureIpcHandlers(ipcMainMock); |
29 | 41 |
|
30 |
| - configureIpcHandlers(ipcMain); |
| 42 | + const mockGetDesktopPath = getDesktopPath as jest.Mock; |
| 43 | + mockGetDesktopPath.mockReturnValue('/path/to/desktop'); |
31 | 44 |
|
32 |
| - const handler = (ipcMain.handle as jest.Mock).mock.calls.find( |
33 |
| - ([event]) => event === IpcEvent.GET_DESKTOP_PATH |
34 |
| - )[1]; |
| 45 | + const handler = ipcMainMock.handle.mock.calls.find((call: any) => call[0] === IpcEvent.GET_DESKTOP_PATH)[1]; |
| 46 | + handler(); |
35 | 47 |
|
36 |
| - const result = await handler(); |
37 |
| - |
38 |
| - expect(result).toBe('/mock/desktop/path'); |
39 | 48 | expect(mockGetDesktopPath).toHaveBeenCalled();
|
40 | 49 | });
|
41 | 50 |
|
42 |
| - test('should handle DIALOG_SELECT_DIRECTORY', async () => { |
43 |
| - const mockShowOpenDialog = jest.mocked(dialog.showOpenDialog); |
44 |
| - mockShowOpenDialog.mockResolvedValue({ canceled: false, filePaths: ['/mock/directory'] }); |
45 |
| - |
46 |
| - configureIpcHandlers(ipcMain); |
| 51 | + it('should handle DIALOG_SELECT_DIRECTORY and call dialog.showOpenDialog()', async () => { |
| 52 | + configureIpcHandlers(ipcMainMock); |
47 | 53 |
|
48 |
| - const handler = (ipcMain.handle as jest.Mock).mock.calls.find( |
49 |
| - ([event]) => event === IpcEvent.DIALOG_SELECT_DIRECTORY |
50 |
| - )[1]; |
| 54 | + const dialogMock = dialog.showOpenDialog as jest.Mock; |
| 55 | + dialogMock.mockResolvedValue({ canceled: false, filePaths: ['/path/to/directory'] }); |
51 | 56 |
|
| 57 | + const handler = ipcMainMock.handle.mock.calls.find((call: any) => call[0] === IpcEvent.DIALOG_SELECT_DIRECTORY)[1]; |
52 | 58 | const result = await handler();
|
53 | 59 |
|
54 |
| - expect(result).toBe('/mock/directory'); |
55 |
| - expect(mockShowOpenDialog).toHaveBeenCalledWith({ |
56 |
| - properties: ['openDirectory'], |
57 |
| - }); |
| 60 | + expect(dialogMock).toHaveBeenCalledWith({ properties: ['openDirectory'] }); |
| 61 | + expect(result).toBe('/path/to/directory'); |
58 | 62 | });
|
59 | 63 |
|
60 |
| - test('should handle DIALOG_SELECT_DIRECTORY cancellation', async () => { |
61 |
| - const mockShowOpenDialog = jest.mocked(dialog.showOpenDialog); |
62 |
| - mockShowOpenDialog.mockResolvedValue({ canceled: true, filePaths: [] }); |
| 64 | + it('should return null if directory selection is canceled', async () => { |
| 65 | + configureIpcHandlers(ipcMainMock); |
63 | 66 |
|
64 |
| - configureIpcHandlers(ipcMain); |
65 |
| - |
66 |
| - const handler = (ipcMain.handle as jest.Mock).mock.calls.find( |
67 |
| - ([event]) => event === IpcEvent.DIALOG_SELECT_DIRECTORY |
68 |
| - )[1]; |
| 67 | + const dialogMock = dialog.showOpenDialog as jest.Mock; |
| 68 | + dialogMock.mockResolvedValue({ canceled: true, filePaths: [] }); |
69 | 69 |
|
| 70 | + const handler = ipcMainMock.handle.mock.calls.find((call: any) => call[0] === IpcEvent.DIALOG_SELECT_DIRECTORY)[1]; |
70 | 71 | const result = await handler();
|
71 | 72 |
|
| 73 | + expect(dialogMock).toHaveBeenCalledWith({ properties: ['openDirectory'] }); |
72 | 74 | expect(result).toBeNull();
|
73 |
| - expect(mockShowOpenDialog).toHaveBeenCalledWith({ |
74 |
| - properties: ['openDirectory'], |
75 |
| - }); |
76 | 75 | });
|
77 | 76 |
|
78 |
| - test('should handle CONVERT_MEDIA', async () => { |
79 |
| - // Make sure the mocked conversion handler returns a resolved promise |
80 |
| - mockConversionHandler.handle.mockResolvedValue('/mock/output/path'); |
81 |
| - |
82 |
| - configureIpcHandlers(ipcMain); |
83 |
| - |
84 |
| - const handler = (ipcMain.handle as jest.Mock).mock.calls.find( |
85 |
| - ([event]) => event === IpcEvent.CONVERT_MEDIA |
86 |
| - )[1]; |
87 |
| - |
88 |
| - const result = await handler( |
89 |
| - {} as IpcMainInvokeEvent, |
90 |
| - { |
91 |
| - id: '1', |
92 |
| - filePath: '/mock/path/video.mp4', |
93 |
| - outputFormat: 'mp4', |
94 |
| - saveDirectory: '/mock/save', |
95 |
| - mediaType: 'video', |
96 |
| - } |
97 |
| - ); |
| 77 | + it('should handle CONVERT_MEDIA and call conversionHandler.handle()', async () => { |
| 78 | + configureIpcHandlers(ipcMainMock); |
98 | 79 |
|
99 |
| - expect(result).toBe('/mock/output/path'); |
100 |
| - expect(mockConversionHandler.handle).toHaveBeenCalledWith( |
| 80 | + const mockHandle = conversionHandlerMock.handle as jest.Mock; |
| 81 | + mockHandle.mockResolvedValue('conversion-success'); |
| 82 | + |
| 83 | + const handler = ipcMainMock.handle.mock.calls.find((call: any) => call[0] === IpcEvent.CONVERT_MEDIA)[1]; |
| 84 | + |
| 85 | + console.log(handler); |
| 86 | + |
| 87 | + |
| 88 | + const mockEvent = {} as IpcMainInvokeEvent; |
| 89 | + const mediaParams = { |
| 90 | + id: '1', |
| 91 | + filePath: '/path/to/file', |
| 92 | + outputFormat: 'mp4', |
| 93 | + saveDirectory: '/path/to/save', |
| 94 | + mediaType: 'video', |
| 95 | + }; |
| 96 | + |
| 97 | + const result = await handler(mockEvent, mediaParams); |
| 98 | + |
| 99 | + expect(mockHandle).toHaveBeenCalledWith( |
101 | 100 | '1',
|
102 |
| - '/mock/path/video.mp4', |
| 101 | + '/path/to/file', |
103 | 102 | 'mp4',
|
104 |
| - '/mock/save', |
| 103 | + '/path/to/save', |
105 | 104 | 'video',
|
106 |
| - expect.any(Object) |
| 105 | + mockEvent |
107 | 106 | );
|
| 107 | + expect(result).toBe('conversion-success'); |
108 | 108 | });
|
109 | 109 |
|
110 |
| - test('should handle CANCEL_ITEM_CONVERSION', () => { |
111 |
| - // Mock the cancel method to return true |
112 |
| - mockConversionHandler.cancel.mockReturnValue(true); |
| 110 | + it('should handle CANCEL_CONVERSION and call conversionHandler.cancelAll()', () => { |
| 111 | + configureIpcHandlers(ipcMainMock); |
113 | 112 |
|
114 |
| - configureIpcHandlers(ipcMain); |
| 113 | + const mockCancelAll = conversionHandlerMock.cancelAll as jest.Mock; |
115 | 114 |
|
116 |
| - const handler = (ipcMain.handle as jest.Mock).mock.calls.find( |
117 |
| - ([event]) => event === IpcEvent.CANCEL_ITEM_CONVERSION |
118 |
| - )[1]; |
| 115 | + const handler = ipcMainMock.handle.mock.calls.find((call: any) => call[0] === IpcEvent.CANCEL_CONVERSION)[1]; |
| 116 | + const result = handler(); |
119 | 117 |
|
120 |
| - const result = handler({} as IpcMainInvokeEvent, '1'); |
121 |
| - |
122 |
| - expect(result).toBe(true); |
123 |
| - expect(mockConversionHandler.cancel).toHaveBeenCalledWith('1'); |
| 118 | + expect(mockCancelAll).toHaveBeenCalled(); |
| 119 | + expect(result).toBe('cancel-all-success'); |
124 | 120 | });
|
125 | 121 |
|
126 |
| - test('should handle CANCEL_CONVERSION', () => { |
127 |
| - // Ensure cancelAll doesn't return undefined but behaves as expected |
128 |
| - mockConversionHandler.cancelAll.mockImplementation(() => true); |
129 |
| - |
130 |
| - configureIpcHandlers(ipcMain); |
| 122 | + it('should handle CANCEL_ITEM_CONVERSION and call conversionHandler.cancel()', () => { |
| 123 | + configureIpcHandlers(ipcMainMock); |
131 | 124 |
|
132 |
| - const handler = (ipcMain.handle as jest.Mock).mock.calls.find( |
133 |
| - ([event]) => event === IpcEvent.CANCEL_CONVERSION |
134 |
| - )[1]; |
| 125 | + const mockCancel = conversionHandlerMock.cancel as jest.Mock; |
135 | 126 |
|
136 |
| - const result = handler({} as IpcMainInvokeEvent); |
| 127 | + const handler = ipcMainMock.handle.mock.calls.find((call: any) => call[0] === IpcEvent.CANCEL_ITEM_CONVERSION)[1]; |
| 128 | + const result = handler({}, '1'); |
137 | 129 |
|
138 |
| - expect(result).toBe(true); |
139 |
| - expect(mockConversionHandler.cancelAll).toHaveBeenCalled(); |
| 130 | + expect(mockCancel).toHaveBeenCalledWith('1'); |
| 131 | + expect(result).toBe('cancel-item-success'); |
140 | 132 | });
|
141 | 133 | });
|
0 commit comments