Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* Upgrade [email protected]

* Fix issue on menu items click returning BaseWindow instead of BrowserWindow objects
  • Loading branch information
sbenmoussati authored Oct 3, 2024
1 parent 769b26e commit e6a733f
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
73 changes: 67 additions & 6 deletions spec/appMenu.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, any>();
Expand Down Expand Up @@ -100,6 +102,7 @@ jest.mock('../src/app/window-handler', () => {
windowHandler: {
createMoreInfoWindow: jest.fn(),
getMainWindow: jest.fn(),
getMainWebContents: jest.fn(),
isMana: true,
},
};
Expand Down Expand Up @@ -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', () => {
Expand Down
11 changes: 7 additions & 4 deletions src/app/app-menu.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {
app,
BaseWindow,
BrowserWindow,
Menu,
MenuItemConstructorOptions,
session,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e6a733f

Please sign in to comment.