Skip to content

Commit

Permalink
Refactor ipc-handlers.ts and ipc-handler.test.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
Thavarshan committed Sep 20, 2024
1 parent d416360 commit b110897
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/lib/system/ipc-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { ImageFormat } from '@/enum/image-format';
import { Media } from '@/types/media';

// Create a single instance of ConversionHandler to handle all conversions
const conversionHandler = new ConversionHandler();
export const conversionHandler = new ConversionHandler();

/**
* Configure the IPC handlers
Expand Down
39 changes: 14 additions & 25 deletions tests/main/ipc-handler.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { dialog, IpcMainInvokeEvent } from 'electron';
import { getDesktopPath } from '@/lib/utils/desktop-path';
import { IpcEvent } from '@/enum/ipc-event';
import { ConversionHandler } from '@/lib/conversion/conversion-handler';
import { configureIpcHandlers } from '@/lib/system/ipc-handlers';
import { configureIpcHandlers, conversionHandler } from '@/lib/system/ipc-handlers';

// Mock the dependencies
jest.mock('electron', () => ({
Expand All @@ -13,27 +12,21 @@ jest.mock('electron', () => ({
jest.mock('@/lib/utils/desktop-path', () => ({
getDesktopPath: jest.fn(),
}));
jest.mock('@/lib/conversion/conversion-handler', () => {
return {
ConversionHandler: jest.fn().mockImplementation(() => ({
handle: jest.fn(),
cancelAll: jest.fn(),
cancel: jest.fn(),
})),
};
});
jest.mock('@/lib/conversion/conversion-handler');

describe('configureIpcHandlers', () => {
let ipcMainMock: any;
let conversionHandlerMock: any;
let conversionHandlerMock: jest.Mocked<typeof conversionHandler>;

beforeEach(() => {
ipcMainMock = {
handle: jest.fn(),
};

jest.clearAllMocks();
conversionHandlerMock = new ConversionHandler();

// Mock the exported conversionHandler instance
conversionHandlerMock = conversionHandler as jest.Mocked<typeof conversionHandler>;
});

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

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

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

console.log(handler);


const mockEvent = {} as IpcMainInvokeEvent;
const mediaParams = {
id: '1',
Expand All @@ -96,7 +85,7 @@ describe('configureIpcHandlers', () => {

const result = await handler(mockEvent, mediaParams);

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

const mockCancelAll = conversionHandlerMock.cancelAll as jest.Mock;
conversionHandlerMock.cancelAll.mockReturnValue(undefined); // Mock the result

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

expect(mockCancelAll).toHaveBeenCalled();
expect(result).toBe('cancel-all-success');
expect(conversionHandlerMock.cancelAll).toHaveBeenCalled();
expect(result).toBe(undefined);
});

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

const mockCancel = conversionHandlerMock.cancel as jest.Mock;
conversionHandlerMock.cancel.mockReturnValue(undefined as any); // Mock the result

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

expect(mockCancel).toHaveBeenCalledWith('1');
expect(result).toBe('cancel-item-success');
expect(conversionHandlerMock.cancel).toHaveBeenCalledWith('1');
expect(result).toBe(undefined);
});
});

0 comments on commit b110897

Please sign in to comment.