Repository: gitify-app/gitify
Pull request #653 (2023-10-04 09:14:34)

chore: Replace remote for setting auto-open pref
One of the last usages of remote, which generally isn't recommended


bmulholland: Current dependencies on/for this PR:
* main
  * **PR #613** <a href="https://app.graphite.dev/github/pr/gitify-app/gitify/613" target="_blank"><img src="https://static.graphite.dev/graphite-32x32-black.png" alt="Graphite" width="10px" height="10px"/></a> 
    * **PR #649** <a href="https://app.graphite.dev/github/pr/gitify-app/gitify/649" target="_blank"><img src="https://static.graphite.dev/graphite-32x32-black.png" alt="Graphite" width="10px" height="10px"/></a> 
      * **PR #651** <a href="https://app.graphite.dev/github/pr/gitify-app/gitify/651" target="_blank"><img src="https://static.graphite.dev/graphite-32x32-black.png" alt="Graphite" width="10px" height="10px"/></a> 
        * **PR #652** <a href="https://app.graphite.dev/github/pr/gitify-app/gitify/652" target="_blank"><img src="https://static.graphite.dev/graphite-32x32-black.png" alt="Graphite" width="10px" height="10px"/></a> 
          * **PR #653** <a href="https://app.graphite.dev/github/pr/gitify-app/gitify/653" target="_blank"><img src="https://static.graphite.dev/graphite-32x32-black.png" alt="Graphite" width="10px" height="10px"/></a>  👈
            * **PR #654** <a href="https://app.graphite.dev/github/pr/gitify-app/gitify/654" target="_blank"><img src="https://static.graphite.dev/graphite-32x32-black.png" alt="Graphite" width="10px" height="10px"/></a> 

This comment was auto-generated by [Graphite](https://app.graphite.dev/github/pr/gitify-app/gitify/653?utm_source=stack-comment).
bmulholland: Coveralls is right, we don't test the code pulled into main.js for IPC communication. Is it worth it, though? The unit test would just mock out the call, so I'm not really sure what it would test.

Patch:
diff --git a/main.js b/main.js
index dd980310..56ffc84c 100644
--- a/main.js
+++ b/main.js
@@ -66,8 +66,16 @@ menubarApp.on('ready', () => {
     }
   });
 
+  ipcMain.handle('get-platform', async () => {
+    return process.platform;
+  });
+  ipcMain.handle('get-app-version', async () => {
+    return app.getVersion();
+  });
+
   ipcMain.on('reopen-window', () => menubarApp.showWindow());
   ipcMain.on('hide-window', () => menubarApp.hideWindow());
+
   ipcMain.on('app-quit', () => menubarApp.app.quit());
   ipcMain.on('update-icon', (_, arg) => {
     if (!menubarApp.tray.isDestroyed()) {
@@ -78,12 +86,8 @@ menubarApp.on('ready', () => {
       }
     }
   });
-  ipcMain.handle('get-platform', async () => {
-    return process.platform;
-  });
-
-  ipcMain.handle('get-app-version', async () => {
-    return app.getVersion();
+  ipcMain.on('set-login-item-settings', (event, settings) => {
+    app.setLoginItemSettings(settings);
   });
 
   menubarApp.window.webContents.on('devtools-opened', () => {
diff --git a/src/utils/comms.ts b/src/utils/comms.ts
index 513073b0..09fe3a30 100644
--- a/src/utils/comms.ts
+++ b/src/utils/comms.ts
@@ -1,12 +1,11 @@
 import { ipcRenderer, shell } from 'electron';
-import remote from '@electron/remote';
 
 export function openExternalLink(url: string): void {
   shell.openExternal(url);
 }
 
 export function setAutoLaunch(value: boolean): void {
-  remote.app.setLoginItemSettings({
+  ipcRenderer.send('set-login-item-settings', {
     openAtLogin: value,
     openAsHidden: value,
   });


Test patch:
diff --git a/src/utils/comms.test.ts b/src/utils/comms.test.ts
index 23e3c1fe..166f5d93 100644
--- a/src/utils/comms.test.ts
+++ b/src/utils/comms.test.ts
@@ -1,5 +1,4 @@
 import { ipcRenderer, shell } from 'electron';
-import remote from '@electron/remote';
 import {
   updateTrayIcon,
   openExternalLink,
@@ -43,24 +42,20 @@ describe('utils/comms.ts', () => {
   });
 
   it('should setAutoLaunch (true)', () => {
-    jest.spyOn(remote.app, 'setLoginItemSettings');
-
     setAutoLaunch(true);
-    expect(remote.app.setLoginItemSettings).toHaveBeenCalledTimes(1);
-    expect(remote.app.setLoginItemSettings).toHaveBeenCalledWith({
+
+    expect(ipcRenderer.send).toHaveBeenCalledWith('set-login-item-settings', {
       openAtLogin: true,
       openAsHidden: true,
     });
   });
 
   it('should setAutoLaunch (false)', () => {
-    jest.spyOn(remote.app, 'setLoginItemSettings');
-
     setAutoLaunch(false);
-    expect(remote.app.setLoginItemSettings).toHaveBeenCalledTimes(1);
-    expect(remote.app.setLoginItemSettings).toHaveBeenCalledWith({
-      openAtLogin: false,
+
+    expect(ipcRenderer.send).toHaveBeenCalledWith('set-login-item-settings', {
       openAsHidden: false,
+      openAtLogin: false,
     });
   });
 });