Skip to content

Commit

Permalink
Fix tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
robinjhuang committed Nov 9, 2024
1 parent 75d64dc commit 8992d68
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 32 deletions.
7 changes: 6 additions & 1 deletion src/handlers/appInfoHandlers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { app, ipcMain } from 'electron';
import { app, ipcMain, shell } from 'electron';
import { IPC_CHANNELS } from '../constants';
/**
* Handles static information about the app in IPC channels.
Expand All @@ -14,5 +14,10 @@ export class AppInfoHandlers {
ipcMain.handle(IPC_CHANNELS.GET_ELECTRON_VERSION, () => {
return app.getVersion();
});

ipcMain.handle(IPC_CHANNELS.OPEN_FORUM, () => {
shell.openExternal('https://forum.comfy.org');
});
ipcMain.handle(IPC_CHANNELS.DEFAULT_INSTALL_LOCATION, () => app.getPath('documents'));
}
}
6 changes: 1 addition & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,6 @@ if (!gotTheLock) {
new PathHandlers().registerHandlers();
new AppInfoHandlers().registerHandlers();

ipcMain.handle(IPC_CHANNELS.OPEN_FORUM, () => {
shell.openExternal('https://forum.comfy.org');
});
ipcMain.handle(IPC_CHANNELS.DEFAULT_INSTALL_LOCATION, () => app.getPath('documents'));
ipcMain.handle(IPC_CHANNELS.OPEN_DIALOG, (event, options: Electron.OpenDialogOptions) => {
log.info('Open dialog');
return dialog.showOpenDialogSync({
Expand Down Expand Up @@ -565,7 +561,7 @@ function isFirstTimeSetup(): boolean {
async function selectedInstallDirectory(): Promise<string> {
return new Promise((resolve, reject) => {
ipcMain.on(IPC_CHANNELS.SELECTED_DIRECTORY, (_event, value) => {
log.info('Directory selected:', value);
log.info('User selected to install ComfyUI in:', value);
resolve(value);
});
});
Expand Down
25 changes: 25 additions & 0 deletions tests/unit/handlers/appinfoHandlers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { ipcMain } from 'electron';
import { AppInfoHandlers } from '../../../src/handlers/appInfoHandlers';
import { IPC_CHANNELS } from '../../../src/constants';

jest.mock('electron', () => ({
ipcMain: {
handle: jest.fn(),
},
}));

describe('AppInfoHandlers', () => {
let handler: AppInfoHandlers;
beforeEach(() => {
handler = new AppInfoHandlers();
handler.registerHandlers();
});

it('should register all expected handle channels', () => {
const expectedChannels = [IPC_CHANNELS.IS_PACKAGED, IPC_CHANNELS.GET_ELECTRON_VERSION];

expectedChannels.forEach((channel) => {
expect(ipcMain.handle).toHaveBeenCalledWith(channel, expect.any(Function));
});
});
});
49 changes: 23 additions & 26 deletions tests/unit/handlers/pathHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,35 @@ import { ipcMain } from 'electron';
import { PathHandlers } from '../../../src/handlers/pathHandlers';
import { IPC_CHANNELS } from '../../../src/constants';

const isChannelRegistered = (channel: string): boolean => {
// @ts-ignore - accessing private property for testing
return ipcMain.listeners(channel).length > 0;
};
jest.mock('electron', () => ({
ipcMain: {
on: jest.fn(),
handle: jest.fn(),
},
}));

describe('IPC Handlers', () => {
describe('PathHandlers', () => {
let handler: PathHandlers;
beforeEach(() => {
// Clear all existing listeners before each test
ipcMain.removeAllListeners();
handler = new PathHandlers();
handler.registerHandlers();
});
describe('PathHandlers', () => {
let handler: PathHandlers;
beforeEach(() => {
// Clear all existing listeners before each test
ipcMain.removeAllListeners();
handler = new PathHandlers();
handler.registerHandlers();
});

it('should register all expected channels', () => {
handler.registerHandlers();
it('should register all expected handle channels', () => {
handler.registerHandlers();

const expectedChannelsForHandle = [IPC_CHANNELS.GET_MODEL_CONFIG_PATH, IPC_CHANNELS.GET_BASE_PATH];

expectedChannelsForHandle.forEach((channel) => {
expect(ipcMain.handle).toHaveBeenCalledWith(channel, expect.any(Function));
});
});

const expectedChannels = [
IPC_CHANNELS.GET_MODEL_CONFIG_PATH,
IPC_CHANNELS.GET_BASE_PATH,
IPC_CHANNELS.OPEN_LOGS_PATH,
IPC_CHANNELS.OPEN_PATH,
];
it('should register all expected on channels', () => {
const expectedChannelsForOn = [IPC_CHANNELS.OPEN_LOGS_PATH, IPC_CHANNELS.OPEN_PATH];

expectedChannels.forEach((channel) => {
expect(isChannelRegistered(channel)).toBe(true);
});
expectedChannelsForOn.forEach((channel) => {
expect(ipcMain.on).toHaveBeenCalledWith(channel, expect.any(Function));
});
});
});

0 comments on commit 8992d68

Please sign in to comment.