Fix mocking Chrome API for Electron (#1748)

* Fix mocking Chrome API for Electron

* Create chilled-feet-marry.md
This commit is contained in:
Nathan Bierema 2024-09-03 21:47:16 -04:00 committed by GitHub
parent c5aef77b85
commit f1d61580a8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 25 additions and 21 deletions

View File

@ -0,0 +1,5 @@
---
'remotedev-redux-devtools-extension': patch
---
Fix mocking Chrome API for Electron

View File

@ -29,11 +29,7 @@ interface OwnProps {
} }
type Props = StateProps & DispatchProps & OwnProps; type Props = StateProps & DispatchProps & OwnProps;
declare global { const isElectron = navigator.userAgent.includes('Electron');
interface Window {
isElectron?: boolean;
}
}
function sendMessage(message: SingleMessage) { function sendMessage(message: SingleMessage) {
chrome.runtime.sendMessage(message); chrome.runtime.sendMessage(message);
@ -98,7 +94,7 @@ class Actions extends Component<Props> {
<DispatcherButton dispatcherIsOpen={this.props.dispatcherIsOpen} /> <DispatcherButton dispatcherIsOpen={this.props.dispatcherIsOpen} />
)} )}
<Divider /> <Divider />
{!window.isElectron && ( {!isElectron && (
<Button <Button
onClick={() => { onClick={() => {
this.openWindow('window'); this.openWindow('window');
@ -107,7 +103,7 @@ class Actions extends Component<Props> {
<MdOutlineWindow /> <MdOutlineWindow />
</Button> </Button>
)} )}
{!window.isElectron && ( {!isElectron && (
<Button <Button
onClick={() => { onClick={() => {
this.openWindow('remote'); this.openWindow('remote');

View File

@ -1,3 +1,4 @@
import '../chromeApiMock';
import configureStore from './store/backgroundStore'; import configureStore from './store/backgroundStore';
import openDevToolsWindow, { DevToolsPosition } from './openWindow'; import openDevToolsWindow, { DevToolsPosition } from './openWindow';
import { createMenu, removeMenu } from './contextMenus'; import { createMenu, removeMenu } from './contextMenus';

View File

@ -1,14 +1,11 @@
// Mock not supported chrome.* API for Firefox and Electron // Mock not supported chrome.* API for Firefox and Electron
window.isElectron = const isElectron = navigator.userAgent.includes('Electron');
window.navigator && window.navigator.userAgent.indexOf('Electron') !== -1; const isFirefox = navigator.userAgent.includes('Firefox');
const isFirefox = navigator.userAgent.indexOf('Firefox') !== -1;
// Background page only // Background page only
if ( if (
(window.isElectron && (isElectron && location.pathname === '/background.bundle.js') ||
location.pathname === '/_generated_background_page.html') ||
isFirefox isFirefox
) { ) {
(chrome.runtime as any).onConnectExternal = { (chrome.runtime as any).onConnectExternal = {
@ -18,7 +15,7 @@ if (
addListener() {}, addListener() {},
}; };
if (window.isElectron) { if (isElectron) {
(chrome.notifications as any) = { (chrome.notifications as any) = {
onClicked: { onClicked: {
addListener() {}, addListener() {},
@ -31,6 +28,11 @@ if (
addListener() {}, addListener() {},
}, },
}; };
(chrome.commands as any) = {
onCommand: {
addListener() {},
},
};
} else { } else {
(chrome.storage as any).sync = chrome.storage.local; (chrome.storage as any).sync = chrome.storage.local;
(chrome.runtime as any).onInstalled = { (chrome.runtime as any).onInstalled = {
@ -39,7 +41,7 @@ if (
} }
} }
if (window.isElectron) { if (isElectron) {
if (!chrome.storage.local || !chrome.storage.local.remove) { if (!chrome.storage.local || !chrome.storage.local.remove) {
(chrome.storage as any).local = { (chrome.storage as any).local = {
set(obj: any, callback: any) { set(obj: any, callback: any) {
@ -87,6 +89,6 @@ if (window.isElectron) {
}; };
} }
if (isFirefox || window.isElectron) { if (isFirefox || isElectron) {
(chrome.storage as any).sync = chrome.storage.local; (chrome.storage as any).sync = chrome.storage.local;
} }

View File

@ -6,7 +6,7 @@ import chromedriver from 'chromedriver';
import { switchMonitorTests, delay } from '../utils/e2e'; import { switchMonitorTests, delay } from '../utils/e2e';
const devPanelPath = const devPanelPath =
'chrome-extension://lmhkpmbekcpmknklioeibfkpmmfibljd/window.html'; 'chrome-extension://lmhkpmbekcpmknklioeibfkpmmfibljd/devpanel.html';
describe('DevTools panel for Electron', function () { describe('DevTools panel for Electron', function () {
let driver; let driver;
@ -76,7 +76,7 @@ describe('DevTools panel for Electron', function () {
expect(className).not.toMatch(/hidden/); // not hidden 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 await driver
.switchTo() .switchTo()
.frame( .frame(
@ -87,7 +87,7 @@ describe('DevTools panel for Electron', function () {
await delay(1000); await delay(1000);
}); });
it.skip('should contain INIT action', async () => { it('should contain INIT action', async () => {
const element = await driver.wait( const element = await driver.wait(
webdriver.until.elementLocated( webdriver.until.elementLocated(
webdriver.By.xpath('//div[@data-testid="actionListRows"]'), webdriver.By.xpath('//div[@data-testid="actionListRows"]'),
@ -99,7 +99,7 @@ describe('DevTools panel for Electron', function () {
expect(val).toMatch(/@@INIT/); 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 const val = await driver
.findElement(webdriver.By.xpath('//div[@data-testid="inspector"]')) .findElement(webdriver.By.xpath('//div[@data-testid="inspector"]'))
.getText(); .getText();
@ -107,7 +107,7 @@ describe('DevTools panel for Electron', function () {
}); });
Object.keys(switchMonitorTests).forEach((description) => 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 () => { /* it('should be no logs in console of main window', async () => {