diff --git a/packages/main/src/mainWindow.ts b/packages/main/src/mainWindow.ts index 165bd97..afdcac5 100644 --- a/packages/main/src/mainWindow.ts +++ b/packages/main/src/mainWindow.ts @@ -37,8 +37,7 @@ async function createWindow() { } }); - - browserWindow.webContents.on('will-navigate', (event, url) => { + browserWindow.webContents?.on('will-navigate', (event, url) => { if (url !== browserWindow.webContents.getURL()) { event.preventDefault(); shell.openExternal(url); @@ -48,20 +47,20 @@ async function createWindow() { /** * icpMain */ - ipcMain.handle('close', () => { + ipcMain?.handle('close', () => { browserWindow?.close(); }); - ipcMain.handle('minimize', () => { + ipcMain?.handle('minimize', () => { browserWindow.minimize(); }); - ipcMain.handle('maximize', () => { + ipcMain?.handle('maximize', () => { if (browserWindow.isMaximized()) { browserWindow.unmaximize(); } else { browserWindow.maximize(); } }); - ipcMain.handle('isMaximized', () => { + ipcMain?.handle('isMaximized', () => { return browserWindow.isMaximized(); }); @@ -93,7 +92,6 @@ async function createWindow() { * Restore an existing BrowserWindow or Create a new BrowserWindow. */ export async function restoreOrCreateWindow() { - let window = BrowserWindow.getAllWindows().find(w => !w.isDestroyed()); if (window === undefined) { diff --git a/packages/main/tests/unit.spec.ts b/packages/main/tests/unit.spec.ts index 859d621..2c637dc 100644 --- a/packages/main/tests/unit.spec.ts +++ b/packages/main/tests/unit.spec.ts @@ -28,7 +28,13 @@ vi.mock('electron', () => { }, }; - return {BrowserWindow: bw, app}; + // 模拟 ipcMain + const ipcMain = { + handle: vi.fn(), + // 根据需要模拟其他 ipcMain 方法 + }; + + return { BrowserWindow: bw, app, ipcMain }; }); beforeEach(() => {