Skip to content

Commit f1d6158

Browse files
authored
Fix mocking Chrome API for Electron (#1748)
* Fix mocking Chrome API for Electron * Create chilled-feet-marry.md
1 parent c5aef77 commit f1d6158

File tree

5 files changed

+25
-21
lines changed

5 files changed

+25
-21
lines changed

.changeset/chilled-feet-marry.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'remotedev-redux-devtools-extension': patch
3+
---
4+
5+
Fix mocking Chrome API for Electron

extension/src/app/Actions.tsx

+3-7
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,7 @@ interface OwnProps {
2929
}
3030
type Props = StateProps & DispatchProps & OwnProps;
3131

32-
declare global {
33-
interface Window {
34-
isElectron?: boolean;
35-
}
36-
}
32+
const isElectron = navigator.userAgent.includes('Electron');
3733

3834
function sendMessage(message: SingleMessage) {
3935
chrome.runtime.sendMessage(message);
@@ -98,7 +94,7 @@ class Actions extends Component<Props> {
9894
<DispatcherButton dispatcherIsOpen={this.props.dispatcherIsOpen} />
9995
)}
10096
<Divider />
101-
{!window.isElectron && (
97+
{!isElectron && (
10298
<Button
10399
onClick={() => {
104100
this.openWindow('window');
@@ -107,7 +103,7 @@ class Actions extends Component<Props> {
107103
<MdOutlineWindow />
108104
</Button>
109105
)}
110-
{!window.isElectron && (
106+
{!isElectron && (
111107
<Button
112108
onClick={() => {
113109
this.openWindow('remote');

extension/src/background/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import '../chromeApiMock';
12
import configureStore from './store/backgroundStore';
23
import openDevToolsWindow, { DevToolsPosition } from './openWindow';
34
import { createMenu, removeMenu } from './contextMenus';

extension/src/chromeApiMock.ts

+11-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
// Mock not supported chrome.* API for Firefox and Electron
22

3-
window.isElectron =
4-
window.navigator && window.navigator.userAgent.indexOf('Electron') !== -1;
5-
6-
const isFirefox = navigator.userAgent.indexOf('Firefox') !== -1;
3+
const isElectron = navigator.userAgent.includes('Electron');
4+
const isFirefox = navigator.userAgent.includes('Firefox');
75

86
// Background page only
97
if (
10-
(window.isElectron &&
11-
location.pathname === '/_generated_background_page.html') ||
8+
(isElectron && location.pathname === '/background.bundle.js') ||
129
isFirefox
1310
) {
1411
(chrome.runtime as any).onConnectExternal = {
@@ -18,7 +15,7 @@ if (
1815
addListener() {},
1916
};
2017

21-
if (window.isElectron) {
18+
if (isElectron) {
2219
(chrome.notifications as any) = {
2320
onClicked: {
2421
addListener() {},
@@ -31,6 +28,11 @@ if (
3128
addListener() {},
3229
},
3330
};
31+
(chrome.commands as any) = {
32+
onCommand: {
33+
addListener() {},
34+
},
35+
};
3436
} else {
3537
(chrome.storage as any).sync = chrome.storage.local;
3638
(chrome.runtime as any).onInstalled = {
@@ -39,7 +41,7 @@ if (
3941
}
4042
}
4143

42-
if (window.isElectron) {
44+
if (isElectron) {
4345
if (!chrome.storage.local || !chrome.storage.local.remove) {
4446
(chrome.storage as any).local = {
4547
set(obj: any, callback: any) {
@@ -87,6 +89,6 @@ if (window.isElectron) {
8789
};
8890
}
8991

90-
if (isFirefox || window.isElectron) {
92+
if (isFirefox || isElectron) {
9193
(chrome.storage as any).sync = chrome.storage.local;
9294
}

extension/test/electron/devpanel.spec.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import chromedriver from 'chromedriver';
66
import { switchMonitorTests, delay } from '../utils/e2e';
77

88
const devPanelPath =
9-
'chrome-extension://lmhkpmbekcpmknklioeibfkpmmfibljd/window.html';
9+
'chrome-extension://lmhkpmbekcpmknklioeibfkpmmfibljd/devpanel.html';
1010

1111
describe('DevTools panel for Electron', function () {
1212
let driver;
@@ -76,7 +76,7 @@ describe('DevTools panel for Electron', function () {
7676
expect(className).not.toMatch(/hidden/); // not hidden
7777
});
7878

79-
it.skip('should have Redux DevTools UI on current tab', async () => {
79+
it('should have Redux DevTools UI on current tab', async () => {
8080
await driver
8181
.switchTo()
8282
.frame(
@@ -87,7 +87,7 @@ describe('DevTools panel for Electron', function () {
8787
await delay(1000);
8888
});
8989

90-
it.skip('should contain INIT action', async () => {
90+
it('should contain INIT action', async () => {
9191
const element = await driver.wait(
9292
webdriver.until.elementLocated(
9393
webdriver.By.xpath('//div[@data-testid="actionListRows"]'),
@@ -99,15 +99,15 @@ describe('DevTools panel for Electron', function () {
9999
expect(val).toMatch(/@@INIT/);
100100
});
101101

102-
it.skip("should contain Inspector monitor's component", async () => {
102+
it("should contain Inspector monitor's component", async () => {
103103
const val = await driver
104104
.findElement(webdriver.By.xpath('//div[@data-testid="inspector"]'))
105105
.getText();
106106
expect(val).toBeDefined();
107107
});
108108

109109
Object.keys(switchMonitorTests).forEach((description) =>
110-
it.skip(description, () => switchMonitorTests[description](driver)),
110+
it(description, () => switchMonitorTests[description](driver)),
111111
);
112112

113113
/* it('should be no logs in console of main window', async () => {

0 commit comments

Comments
 (0)