Repository: manusa/electronim
Pull request #230 (2022-11-27 08:58:30)

🐛 fix race condition on check for updates

sonarcloud[bot]: Kudos, SonarCloud Quality Gate passed!&nbsp; &nbsp; [![Quality Gate passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/passed-16px.png 'Quality Gate passed')](https://sonarcloud.io/dashboard?id=manusa_electronim&pullRequest=230)

[![Bug](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/bug-16px.png 'Bug')](https://sonarcloud.io/project/issues?id=manusa_electronim&pullRequest=230&resolved=false&types=BUG) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=manusa_electronim&pullRequest=230&resolved=false&types=BUG) [0 Bugs](https://sonarcloud.io/project/issues?id=manusa_electronim&pullRequest=230&resolved=false&types=BUG)  
[![Vulnerability](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/vulnerability-16px.png 'Vulnerability')](https://sonarcloud.io/project/issues?id=manusa_electronim&pullRequest=230&resolved=false&types=VULNERABILITY) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=manusa_electronim&pullRequest=230&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=manusa_electronim&pullRequest=230&resolved=false&types=VULNERABILITY)  
[![Security Hotspot](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/security_hotspot-16px.png 'Security Hotspot')](https://sonarcloud.io/project/security_hotspots?id=manusa_electronim&pullRequest=230&resolved=false&types=SECURITY_HOTSPOT) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/security_hotspots?id=manusa_electronim&pullRequest=230&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=manusa_electronim&pullRequest=230&resolved=false&types=SECURITY_HOTSPOT)  
[![Code Smell](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/code_smell-16px.png 'Code Smell')](https://sonarcloud.io/project/issues?id=manusa_electronim&pullRequest=230&resolved=false&types=CODE_SMELL) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=manusa_electronim&pullRequest=230&resolved=false&types=CODE_SMELL) [0 Code Smells](https://sonarcloud.io/project/issues?id=manusa_electronim&pullRequest=230&resolved=false&types=CODE_SMELL)

[![100.0%](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/CoverageChart/100-16px.png '100.0%')](https://sonarcloud.io/component_measures?id=manusa_electronim&pullRequest=230&metric=new_coverage&view=list) [100.0% Coverage](https://sonarcloud.io/component_measures?id=manusa_electronim&pullRequest=230&metric=new_coverage&view=list)  
[![0.0%](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/Duplications/3-16px.png '0.0%')](https://sonarcloud.io/component_measures?id=manusa_electronim&pullRequest=230&metric=new_duplicated_lines_density&view=list) [0.0% Duplication](https://sonarcloud.io/component_measures?id=manusa_electronim&pullRequest=230&metric=new_duplicated_lines_density&view=list)



Patch:
diff --git a/src/chrome-tabs/index.js b/src/chrome-tabs/index.js
index 86b5a5b..24ce66b 100644
--- a/src/chrome-tabs/index.js
+++ b/src/chrome-tabs/index.js
@@ -67,7 +67,7 @@ const newTabContainer = () => {
   tabContainer.webContents.loadURL(`file://${__dirname}/index.html`,
     {extraHeaders: 'pragma: no-cache\nCache-control: no-cache'});
   tabContainer.webContents.on('context-menu', handleContextMenu);
-  checkForUpdates(tabContainer.webContents);
+  eventBus.once(APP_EVENTS.tabsReady, () => checkForUpdates(tabContainer.webContents));
   setInterval(() => checkForUpdates(tabContainer.webContents), 1000 * 60 * 30).unref();
   return tabContainer;
 };


Test patch:
diff --git a/src/__tests__/electron.js b/src/__tests__/electron.js
index 8f6e2d3..0ec96df 100644
--- a/src/__tests__/electron.js
+++ b/src/__tests__/electron.js
@@ -84,13 +84,21 @@ const mockElectronInstance = ({...overriddenProps} = {}) => {
     },
     ipcMain: {
       listeners: {},
-      emit: jest.fn(),
-      handle: jest.fn((eventName, func) => {
-        instance.ipcMain.listeners[eventName] = func;
-      }),
-      on: jest.fn((eventName, func) => {
+      _listen: (eventName, func) => {
+        if (instance.ipcMain.listeners[eventName]) {
+          const oldFunc = instance.ipcMain.listeners[eventName];
+          const newFunc = func;
+          func = (event, args) => {
+            oldFunc(event, args);
+            newFunc(event, args);
+          };
+        }
         instance.ipcMain.listeners[eventName] = func;
-      }),
+      },
+      emit: jest.fn(),
+      handle: jest.fn((eventName, func) => instance.ipcMain._listen(eventName, func)),
+      on: jest.fn((eventName, func) => instance.ipcMain._listen(eventName, func)),
+      once: jest.fn((eventName, func) => instance.ipcMain._listen(eventName, func)),
       removeHandler: jest.fn(eventName => {
         delete instance.ipcMain.listeners[eventName];
       })
diff --git a/src/chrome-tabs/__tests__/index.test.js b/src/chrome-tabs/__tests__/index.test.js
index 7e11e3b..9bd516f 100644
--- a/src/chrome-tabs/__tests__/index.test.js
+++ b/src/chrome-tabs/__tests__/index.test.js
@@ -45,8 +45,9 @@ describe('Chrome Tabs Module module test suite', () => {
         resolveSend = resolve;
       });
       browserViewInstance.webContents.send = jest.fn(() => resolveSend(true));
-      // When
       chromeTabs.newTabContainer();
+      // When
+      require('electron').ipcMain.listeners.tabsReady();
       // Then
       await expect(isSent).resolves.toBe(true);
       expect(browserViewInstance.webContents.send).toHaveBeenCalledWith('electronimNewVersionAvailable', true);