diff --git a/package.json b/package.json index c6a261d83..8540b2188 100644 --- a/package.json +++ b/package.json @@ -181,7 +181,7 @@ "cheerio": "v1.0.0-rc.12", "cross-env": "7.0.3", "del": "3.0.0", - "electron": "32.0.1", + "electron": "32.1.2", "electron-builder": "^24.13.2", "electron-devtools-installer": "^3.2.0", "electron-icon-maker": "0.0.5", diff --git a/spec/appMenu.spec.ts b/spec/appMenu.spec.ts index 23b724908..eaa391bf5 100644 --- a/spec/appMenu.spec.ts +++ b/spec/appMenu.spec.ts @@ -6,7 +6,9 @@ import { updateAlwaysOnTop } from '../src/app/window-actions'; import { zoomIn, zoomOut } from '../src/app/window-utils'; import * as envMock from '../src/common/env'; import { logger } from '../src/common/logger'; -import { dialog, session, shell } from './__mocks__/electron'; +import { BrowserWindow, dialog, session, shell } from './__mocks__/electron'; +import { windowHandler } from '../src/app/window-handler'; +import { apiName } from '../src/common/api-interface'; jest.mock('../src/app/stores', () => { const mock = new Map(); @@ -100,6 +102,7 @@ jest.mock('../src/app/window-handler', () => { windowHandler: { createMoreInfoWindow: jest.fn(), getMainWindow: jest.fn(), + getMainWebContents: jest.fn(), isMana: true, }, }; @@ -373,20 +376,78 @@ describe('app menu', () => { }); describe(`Toggle Developer Tools`, () => { - it('should call `toggleDevTools` when focusedWindow is not null', () => { - const focusedWindow = { + it('should toggle devtools on focused window - focused window is not main window', () => { + const focusedWindowMock = { isDestroyed: jest.fn(() => false), reload: jest.fn(), webContents: { toggleDevTools: jest.fn(), }, }; - const spy = jest.spyOn(focusedWindow.webContents, 'toggleDevTools'); + + const mainWebContentsMock = { + toggleDevTools: jest.fn(), + }; + + jest + .spyOn(BrowserWindow, 'getFocusedWindow') + .mockReturnValue(focusedWindowMock); + const focusedWindowDevtoolsSpy = jest.spyOn( + focusedWindowMock.webContents, + 'toggleDevTools', + ); + const mainWebContentsSpy = jest + .spyOn(windowHandler, 'getMainWebContents') + .mockReturnValue(mainWebContentsMock); + const mainWebContentsDevToolsSpy = jest.spyOn( + mainWebContentsMock, + 'toggleDevTools', + ); const menuItem = findHelpTroubleshootingMenuItem( 'Toggle Developer Tools', ); - menuItem.click({}, focusedWindow); - expect(spy).toBeCalled(); + menuItem.click({}, undefined); + expect(focusedWindowDevtoolsSpy).toBeCalled(); + expect(mainWebContentsSpy).not.toBeCalled(); + expect(mainWebContentsDevToolsSpy).not.toBeCalled(); + }); + + it('should toggle devtools on focused window - focused window is main window', () => { + const focusedWindowMock = { + isDestroyed: jest.fn(() => false), + reload: jest.fn(), + webContents: { + toggleDevTools: jest.fn(), + }, + winName: apiName.mainWindowName, + }; + + const mainWebContentsMock = { + toggleDevTools: jest.fn(), + isDestroyed: jest.fn(() => false), + }; + + jest + .spyOn(BrowserWindow, 'getFocusedWindow') + .mockReturnValue(focusedWindowMock); + const focusedWindowDevtoolsSpy = jest.spyOn( + focusedWindowMock.webContents, + 'toggleDevTools', + ); + const mainWebContentsSpy = jest + .spyOn(windowHandler, 'getMainWebContents') + .mockReturnValue(mainWebContentsMock); + const mainWebContentsDevToolsSpy = jest.spyOn( + mainWebContentsMock, + 'toggleDevTools', + ); + const menuItem = findHelpTroubleshootingMenuItem( + 'Toggle Developer Tools', + ); + menuItem.click({}, undefined); + expect(focusedWindowDevtoolsSpy).not.toBeCalled(); + expect(mainWebContentsSpy).toBeCalled(); + expect(mainWebContentsDevToolsSpy).toBeCalled(); }); it('should not call `electron.dialog` when focusedWindow is null', () => { diff --git a/src/app/app-menu.ts b/src/app/app-menu.ts index 271a4f44b..3cdea8a04 100644 --- a/src/app/app-menu.ts +++ b/src/app/app-menu.ts @@ -1,5 +1,7 @@ import { app, + BaseWindow, + BrowserWindow, Menu, MenuItemConstructorOptions, session, @@ -650,11 +652,12 @@ export class AppMenu { (typeof isDevToolsEnabledCC === 'boolean' && isDevToolsEnabledCC) || devToolsEnabled, - click(_item, focusedWindow) { - if (!focusedWindow || !windowExists(focusedWindow)) { - return; - } + click(_item, _window: BaseWindow | undefined) { if (devToolsEnabled) { + const focusedWindow = BrowserWindow.getFocusedWindow(); + if (!focusedWindow || !windowExists(focusedWindow)) { + return; + } if ( (focusedWindow as ICustomBrowserWindow).winName === apiName.mainWindowName