Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix mocking Chrome API for Electron #1748

Merged
merged 2 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/chilled-feet-marry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'remotedev-redux-devtools-extension': patch
---

Fix mocking Chrome API for Electron
10 changes: 3 additions & 7 deletions extension/src/app/Actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,7 @@ interface OwnProps {
}
type Props = StateProps & DispatchProps & OwnProps;

declare global {
interface Window {
isElectron?: boolean;
}
}
const isElectron = navigator.userAgent.includes('Electron');

function sendMessage(message: SingleMessage) {
chrome.runtime.sendMessage(message);
Expand Down Expand Up @@ -98,7 +94,7 @@ class Actions extends Component<Props> {
<DispatcherButton dispatcherIsOpen={this.props.dispatcherIsOpen} />
)}
<Divider />
{!window.isElectron && (
{!isElectron && (
<Button
onClick={() => {
this.openWindow('window');
Expand All @@ -107,7 +103,7 @@ class Actions extends Component<Props> {
<MdOutlineWindow />
</Button>
)}
{!window.isElectron && (
{!isElectron && (
<Button
onClick={() => {
this.openWindow('remote');
Expand Down
1 change: 1 addition & 0 deletions extension/src/background/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import '../chromeApiMock';
import configureStore from './store/backgroundStore';
import openDevToolsWindow, { DevToolsPosition } from './openWindow';
import { createMenu, removeMenu } from './contextMenus';
Expand Down
20 changes: 11 additions & 9 deletions extension/src/chromeApiMock.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
// Mock not supported chrome.* API for Firefox and Electron

window.isElectron =
window.navigator && window.navigator.userAgent.indexOf('Electron') !== -1;

const isFirefox = navigator.userAgent.indexOf('Firefox') !== -1;
const isElectron = navigator.userAgent.includes('Electron');
const isFirefox = navigator.userAgent.includes('Firefox');

// Background page only
if (
(window.isElectron &&
location.pathname === '/_generated_background_page.html') ||
(isElectron && location.pathname === '/background.bundle.js') ||
isFirefox
) {
(chrome.runtime as any).onConnectExternal = {
Expand All @@ -18,7 +15,7 @@ if (
addListener() {},
};

if (window.isElectron) {
if (isElectron) {
(chrome.notifications as any) = {
onClicked: {
addListener() {},
Expand All @@ -31,6 +28,11 @@ if (
addListener() {},
},
};
(chrome.commands as any) = {
onCommand: {
addListener() {},
},
};
} else {
(chrome.storage as any).sync = chrome.storage.local;
(chrome.runtime as any).onInstalled = {
Expand All @@ -39,7 +41,7 @@ if (
}
}

if (window.isElectron) {
if (isElectron) {
if (!chrome.storage.local || !chrome.storage.local.remove) {
(chrome.storage as any).local = {
set(obj: any, callback: any) {
Expand Down Expand Up @@ -87,6 +89,6 @@ if (window.isElectron) {
};
}

if (isFirefox || window.isElectron) {
if (isFirefox || isElectron) {
(chrome.storage as any).sync = chrome.storage.local;
}
10 changes: 5 additions & 5 deletions extension/test/electron/devpanel.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import chromedriver from 'chromedriver';
import { switchMonitorTests, delay } from '../utils/e2e';

const devPanelPath =
'chrome-extension://lmhkpmbekcpmknklioeibfkpmmfibljd/window.html';
'chrome-extension://lmhkpmbekcpmknklioeibfkpmmfibljd/devpanel.html';

describe('DevTools panel for Electron', function () {
let driver;
Expand Down Expand Up @@ -76,7 +76,7 @@ describe('DevTools panel for Electron', function () {
expect(className).not.toMatch(/hidden/); // not hidden
});

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

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

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

Object.keys(switchMonitorTests).forEach((description) =>
it.skip(description, () => switchMonitorTests[description](driver)),
it(description, () => switchMonitorTests[description](driver)),
);

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