Skip to content
This repository was archived by the owner on Feb 17, 2025. It is now read-only.

Commit b110897

Browse files
committed
Refactor ipc-handlers.ts and ipc-handler.test.ts
1 parent d416360 commit b110897

File tree

2 files changed

+15
-26
lines changed

2 files changed

+15
-26
lines changed

src/lib/system/ipc-handlers.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { ImageFormat } from '@/enum/image-format';
88
import { Media } from '@/types/media';
99

1010
// Create a single instance of ConversionHandler to handle all conversions
11-
const conversionHandler = new ConversionHandler();
11+
export const conversionHandler = new ConversionHandler();
1212

1313
/**
1414
* Configure the IPC handlers

tests/main/ipc-handler.test.ts

+14-25
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { dialog, IpcMainInvokeEvent } from 'electron';
22
import { getDesktopPath } from '@/lib/utils/desktop-path';
33
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';
65

76
// Mock the dependencies
87
jest.mock('electron', () => ({
@@ -13,27 +12,21 @@ jest.mock('electron', () => ({
1312
jest.mock('@/lib/utils/desktop-path', () => ({
1413
getDesktopPath: jest.fn(),
1514
}));
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');
2516

2617
describe('configureIpcHandlers', () => {
2718
let ipcMainMock: any;
28-
let conversionHandlerMock: any;
19+
let conversionHandlerMock: jest.Mocked<typeof conversionHandler>;
2920

3021
beforeEach(() => {
3122
ipcMainMock = {
3223
handle: jest.fn(),
3324
};
3425

3526
jest.clearAllMocks();
36-
conversionHandlerMock = new ConversionHandler();
27+
28+
// Mock the exported conversionHandler instance
29+
conversionHandlerMock = conversionHandler as jest.Mocked<typeof conversionHandler>;
3730
});
3831

3932
it('should handle GET_DESKTOP_PATH and call getDesktopPath()', () => {
@@ -77,14 +70,10 @@ describe('configureIpcHandlers', () => {
7770
it('should handle CONVERT_MEDIA and call conversionHandler.handle()', async () => {
7871
configureIpcHandlers(ipcMainMock);
7972

80-
const mockHandle = conversionHandlerMock.handle as jest.Mock;
81-
mockHandle.mockResolvedValue('conversion-success');
73+
conversionHandlerMock.handle.mockResolvedValue('conversion-success'); // Mock the result
8274

8375
const handler = ipcMainMock.handle.mock.calls.find((call: any) => call[0] === IpcEvent.CONVERT_MEDIA)[1];
8476

85-
console.log(handler);
86-
87-
8877
const mockEvent = {} as IpcMainInvokeEvent;
8978
const mediaParams = {
9079
id: '1',
@@ -96,7 +85,7 @@ describe('configureIpcHandlers', () => {
9685

9786
const result = await handler(mockEvent, mediaParams);
9887

99-
expect(mockHandle).toHaveBeenCalledWith(
88+
expect(conversionHandlerMock.handle).toHaveBeenCalledWith(
10089
'1',
10190
'/path/to/file',
10291
'mp4',
@@ -110,24 +99,24 @@ describe('configureIpcHandlers', () => {
11099
it('should handle CANCEL_CONVERSION and call conversionHandler.cancelAll()', () => {
111100
configureIpcHandlers(ipcMainMock);
112101

113-
const mockCancelAll = conversionHandlerMock.cancelAll as jest.Mock;
102+
conversionHandlerMock.cancelAll.mockReturnValue(undefined); // Mock the result
114103

115104
const handler = ipcMainMock.handle.mock.calls.find((call: any) => call[0] === IpcEvent.CANCEL_CONVERSION)[1];
116105
const result = handler();
117106

118-
expect(mockCancelAll).toHaveBeenCalled();
119-
expect(result).toBe('cancel-all-success');
107+
expect(conversionHandlerMock.cancelAll).toHaveBeenCalled();
108+
expect(result).toBe(undefined);
120109
});
121110

122111
it('should handle CANCEL_ITEM_CONVERSION and call conversionHandler.cancel()', () => {
123112
configureIpcHandlers(ipcMainMock);
124113

125-
const mockCancel = conversionHandlerMock.cancel as jest.Mock;
114+
conversionHandlerMock.cancel.mockReturnValue(undefined as any); // Mock the result
126115

127116
const handler = ipcMainMock.handle.mock.calls.find((call: any) => call[0] === IpcEvent.CANCEL_ITEM_CONVERSION)[1];
128117
const result = handler({}, '1');
129118

130-
expect(mockCancel).toHaveBeenCalledWith('1');
131-
expect(result).toBe('cancel-item-success');
119+
expect(conversionHandlerMock.cancel).toHaveBeenCalledWith('1');
120+
expect(result).toBe(undefined);
132121
});
133122
});

0 commit comments

Comments
 (0)