From 2037ce7247debf0c94c79f39aad7e47f87e047ba Mon Sep 17 00:00:00 2001 From: Adam Setch Date: Tue, 28 May 2024 20:29:46 -0400 Subject: [PATCH 1/5] refactor: remove ipc for fetching platform type --- main.js | 2 -- src/routes/Settings.tsx | 14 ++++---------- src/utils/helpers.ts | 8 ++++++++ 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/main.js b/main.js index 99bb36006..409f01cbc 100644 --- a/main.js +++ b/main.js @@ -76,8 +76,6 @@ mb.on('ready', () => { } }); - ipc.handle('get-platform', () => process.platform); - ipc.handle('get-app-version', () => app.getVersion()); ipc.on('reopen-window', () => mb.showWindow()); diff --git a/src/routes/Settings.tsx b/src/routes/Settings.tsx index 7a0aec680..9eca9ae61 100644 --- a/src/routes/Settings.tsx +++ b/src/routes/Settings.tsx @@ -27,6 +27,8 @@ import { } from '../utils/comms'; import Constants from '../utils/constants'; import { + isLinux, + isMacOS, isOAuthAppLoggedIn, isPersonalAccessTokenLoggedIn, } from '../utils/helpers'; @@ -41,8 +43,6 @@ export const SettingsRoute: FC = () => { } = useContext(AppContext); const navigate = useNavigate(); - const [isLinux, setIsLinux] = useState(false); - const [isMacOS, setIsMacOS] = useState(false); const [appVersion, setAppVersion] = useState(null); const openGitHubReleaseNotes = useCallback((version) => { @@ -61,12 +61,6 @@ export const SettingsRoute: FC = () => { }; useEffect(() => { - (async () => { - const result = await ipcRenderer.invoke('get-platform'); - setIsLinux(result === 'linux'); - setIsMacOS(result === 'darwin'); - })(); - (async () => { const result = await ipcRenderer.invoke('get-app-version'); setAppVersion(result); @@ -253,7 +247,7 @@ export const SettingsRoute: FC = () => { System - {isMacOS && ( + {isMacOS() && ( { checked={settings.playSound} onChange={(evt) => updateSetting('playSound', evt.target.checked)} /> - {!isLinux && ( + {!isLinux() && ( Date: Tue, 28 May 2024 20:37:49 -0400 Subject: [PATCH 2/5] platform-refactor --- src/__mocks__/@electron/remote.js | 3 - src/__mocks__/utils.ts | 5 + src/routes/Settings.test.tsx | 51 +- .../__snapshots__/Settings.test.tsx.snap | 1189 ++++++++++++++++- src/utils/helpers.test.ts | 33 + 5 files changed, 1273 insertions(+), 8 deletions(-) create mode 100644 src/__mocks__/utils.ts diff --git a/src/__mocks__/@electron/remote.js b/src/__mocks__/@electron/remote.js index c2250c249..6e148d15c 100644 --- a/src/__mocks__/@electron/remote.js +++ b/src/__mocks__/@electron/remote.js @@ -28,9 +28,6 @@ const dialog = { module.exports = { BrowserWindow: BrowserWindow, dialog: dialog, - process: { - platform: 'darwin', - }, app: { getLoginItemSettings: jest.fn(), setLoginItemSettings: () => {}, diff --git a/src/__mocks__/utils.ts b/src/__mocks__/utils.ts new file mode 100644 index 000000000..22149c55f --- /dev/null +++ b/src/__mocks__/utils.ts @@ -0,0 +1,5 @@ +export function mockPlatform(platform: NodeJS.Platform) { + Object.defineProperty(process, 'platform', { + value: platform, + }); +} diff --git a/src/routes/Settings.test.tsx b/src/routes/Settings.test.tsx index ce3d69a51..3fedd0496 100644 --- a/src/routes/Settings.test.tsx +++ b/src/routes/Settings.test.tsx @@ -2,6 +2,7 @@ import { act, fireEvent, render, screen } from '@testing-library/react'; import { ipcRenderer, shell } from 'electron'; import { MemoryRouter } from 'react-router-dom'; import { mockAuth, mockSettings } from '../__mocks__/state-mocks'; +import { mockPlatform } from '../__mocks__/utils'; import { AppContext } from '../context/App'; import { SettingsRoute } from './Settings'; @@ -12,15 +13,61 @@ jest.mock('react-router-dom', () => ({ })); describe('routes/Settings.tsx', () => { + let originalPlatform: PropertyDescriptor; const updateSetting = jest.fn(); + beforeAll(() => { + // Save the original process.platform + originalPlatform = Object.getOwnPropertyDescriptor(process, 'platform'); + }); + beforeEach(() => { jest.clearAllMocks(); }); + afterEach(() => { + // Restore the original process.platform after each test + Object.defineProperty(process, 'platform', originalPlatform); + }); + describe('General', () => { - it('should render itself & its children', async () => { + it('should render itself & its children - macOS', async () => { await act(async () => { + mockPlatform('darwin'); + render( + + + + + , + ); + }); + + expect(screen.getByTestId('settings')).toMatchSnapshot(); + }); + + it('should render itself & its children - linux', async () => { + await act(async () => { + mockPlatform('linux'); + render( + + + + + , + ); + }); + + expect(screen.getByTestId('settings')).toMatchSnapshot(); + }); + + it('should render itself & its children - windows', async () => { + await act(async () => { + mockPlatform('win32'); render( { }); it('should toggle the openAtStartup checkbox', async () => { + mockPlatform('darwin'); + await act(async () => { render( + +
+ +
+ + +
+
+
+ +
+
+ +
+
+
+ + +
+ +
+ + + + +
+
+ +`; + +exports[`routes/Settings.tsx General should render itself & its children - macOS 1`] = ` +
+
+ +

+ Settings +

+
+
+
+ + Appearance + +
+
+
+ +
+
+
+ + +
+
+ + +
+
+ + +
+
+
+
+
+
+
+ +
+
+ +
+
+
+
+
+
+ +
+
+ +
+
+
+
+
+ + Notifications + +
+
+
+ +
+
+ +
+
+
+
+
+
+ +
+
+ +
+
+
+
+
+
+ +
+
+ +
+
+
+
+
+
+ +
+
+ +
+
+
+
+
+ + System + +
+
+
+ +
+
+ +
+
+
+
+
+
+ +
+
+ +
+
+
+
+
+
+ +
+
+ +
+
+
+
+
+
+ +
+
+ +
+
+
+
+
+
+ +
+ + + + +
+
+
+`; + +exports[`routes/Settings.tsx General should render itself & its children - windows 1`] = ` +
+
+ +

+ Settings +

+
+
+
+ + Appearance + +
+
+
+ +
+
+
+ + +
+
+ + +
+
+ + +
+
+
+
+
+
+
+
@@ -407,13 +1352,249 @@ exports[`routes/Settings.tsx General should render itself & its children 1`] = ` > +
+
+
+
+
+
+ +
+
+ +
+
+
+ +
+ + Notifications + +
+
+
+ +
+
+ +
+
+
+
+
+
+ +
+
+ +
+
+
+
+
+
+ +
+
+ +
+
+
+
+
+
+ +
+
+
+
+
+ + System +
diff --git a/src/utils/helpers.test.ts b/src/utils/helpers.test.ts index 42bd831cf..ded597118 100644 --- a/src/utils/helpers.test.ts +++ b/src/utils/helpers.test.ts @@ -1,5 +1,6 @@ import type { AxiosPromise, AxiosResponse } from 'axios'; import { mockAuth, mockUser } from '../__mocks__/state-mocks'; +import { mockPlatform } from '../__mocks__/utils'; import type { SubjectType } from '../typesGitHub'; import { mockGraphQLResponse, @@ -11,6 +12,8 @@ import { generateGitHubWebUrl, generateNotificationReferrerId, isEnterpriseHost, + isLinux, + isMacOS, isOAuthAppLoggedIn, isPersonalAccessTokenLoggedIn, } from './helpers'; @@ -533,4 +536,34 @@ describe('utils/helpers.ts', () => { ); }); }); + + describe('platforms', () => { + let originalPlatform: PropertyDescriptor; + + beforeAll(() => { + // Save the original process.platform + originalPlatform = Object.getOwnPropertyDescriptor(process, 'platform'); + }); + + afterEach(() => { + // Restore the original process.platform after each test + Object.defineProperty(process, 'platform', originalPlatform); + }); + + it('isMacOS', () => { + mockPlatform('darwin'); + expect(isMacOS()).toBe(true); + + mockPlatform('linux'); + expect(isMacOS()).toBe(false); + }); + + it('isLinux', () => { + mockPlatform('darwin'); + expect(isLinux()).toBe(false); + + mockPlatform('linux'); + expect(isLinux()).toBe(true); + }); + }); }); From 558caa058b52c753b9e38e357b3618a5d0ba6433 Mon Sep 17 00:00:00 2001 From: Adam Setch Date: Wed, 29 May 2024 07:38:11 -0400 Subject: [PATCH 3/5] refactor: remove ipc for fetching platform type --- src/__mocks__/utils.ts | 5 - src/routes/Settings.test.tsx | 51 +- src/routes/Settings.tsx | 3 +- .../__snapshots__/Settings.test.tsx.snap | 1189 +---------------- src/utils/helpers.test.ts | 33 - src/utils/helpers.ts | 8 - src/utils/notifications.ts | 3 +- src/utils/platform.ts | 11 + 8 files changed, 19 insertions(+), 1284 deletions(-) delete mode 100644 src/__mocks__/utils.ts create mode 100644 src/utils/platform.ts diff --git a/src/__mocks__/utils.ts b/src/__mocks__/utils.ts deleted file mode 100644 index 22149c55f..000000000 --- a/src/__mocks__/utils.ts +++ /dev/null @@ -1,5 +0,0 @@ -export function mockPlatform(platform: NodeJS.Platform) { - Object.defineProperty(process, 'platform', { - value: platform, - }); -} diff --git a/src/routes/Settings.test.tsx b/src/routes/Settings.test.tsx index 3fedd0496..ce3d69a51 100644 --- a/src/routes/Settings.test.tsx +++ b/src/routes/Settings.test.tsx @@ -2,7 +2,6 @@ import { act, fireEvent, render, screen } from '@testing-library/react'; import { ipcRenderer, shell } from 'electron'; import { MemoryRouter } from 'react-router-dom'; import { mockAuth, mockSettings } from '../__mocks__/state-mocks'; -import { mockPlatform } from '../__mocks__/utils'; import { AppContext } from '../context/App'; import { SettingsRoute } from './Settings'; @@ -13,61 +12,15 @@ jest.mock('react-router-dom', () => ({ })); describe('routes/Settings.tsx', () => { - let originalPlatform: PropertyDescriptor; const updateSetting = jest.fn(); - beforeAll(() => { - // Save the original process.platform - originalPlatform = Object.getOwnPropertyDescriptor(process, 'platform'); - }); - beforeEach(() => { jest.clearAllMocks(); }); - afterEach(() => { - // Restore the original process.platform after each test - Object.defineProperty(process, 'platform', originalPlatform); - }); - describe('General', () => { - it('should render itself & its children - macOS', async () => { + it('should render itself & its children', async () => { await act(async () => { - mockPlatform('darwin'); - render( - - - - - , - ); - }); - - expect(screen.getByTestId('settings')).toMatchSnapshot(); - }); - - it('should render itself & its children - linux', async () => { - await act(async () => { - mockPlatform('linux'); - render( - - - - - , - ); - }); - - expect(screen.getByTestId('settings')).toMatchSnapshot(); - }); - - it('should render itself & its children - windows', async () => { - await act(async () => { - mockPlatform('win32'); render( { }); it('should toggle the openAtStartup checkbox', async () => { - mockPlatform('darwin'); - await act(async () => { render( { diff --git a/src/routes/__snapshots__/Settings.test.tsx.snap b/src/routes/__snapshots__/Settings.test.tsx.snap index cd0c09c29..c0eb68f2a 100644 --- a/src/routes/__snapshots__/Settings.test.tsx.snap +++ b/src/routes/__snapshots__/Settings.test.tsx.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`routes/Settings.tsx General should render itself & its children - linux 1`] = ` +exports[`routes/Settings.tsx General should render itself & its children 1`] = `
System -
-
-
- -
-
- -
-
-
-
-
-
- -
-
- -
-
-
-
-
-
- -
- - - - -
-
- -`; - -exports[`routes/Settings.tsx General should render itself & its children - macOS 1`] = ` -
-
- -

- Settings -

-
-
-
- - Appearance - -
-
-
- -
-
-
- - -
-
- - -
-
- - -
-
-
-
-
-
-
- -
-
- -
-
-
-
-
-
- -
-
- -
-
-
-
-
- - Notifications - -
-
-
- -
-
- -
-
-
-
-
-
- -
-
- -
-
-
-
-
-
- -
-
- -
-
-
-
-
-
- -
-
- -
-
-
-
-
- - System - -
-
-
- -
-
- -
-
-
-
-
-
- -
-
- -
-
-
-
-
-
- -
-
- -
-
-
-
-
-
- -
-
- -
-
-
-
-
-
- -
- - - - -
-
-
-`; - -exports[`routes/Settings.tsx General should render itself & its children - windows 1`] = ` -
-
- -

- Settings -

-
-
-
- - Appearance - -
-
-
- -
-
-
- - -
-
- - -
-
- - -
-
-
-
-
-
-
- -
-
- -
-
-
-
-
-
- -
-
- -
-
-
-
-
- - Notifications - -
-
-
- -
-
- -
-
-
-
-
-
- -
-
- -
-
-
-
-
-
- -
-
- -
-
-
@@ -1549,7 +398,7 @@ exports[`routes/Settings.tsx General should render itself & its children - windo >
@@ -1558,43 +407,13 @@ exports[`routes/Settings.tsx General should render itself & its children - windo >
- -
- - System -
diff --git a/src/utils/helpers.test.ts b/src/utils/helpers.test.ts index ded597118..42bd831cf 100644 --- a/src/utils/helpers.test.ts +++ b/src/utils/helpers.test.ts @@ -1,6 +1,5 @@ import type { AxiosPromise, AxiosResponse } from 'axios'; import { mockAuth, mockUser } from '../__mocks__/state-mocks'; -import { mockPlatform } from '../__mocks__/utils'; import type { SubjectType } from '../typesGitHub'; import { mockGraphQLResponse, @@ -12,8 +11,6 @@ import { generateGitHubWebUrl, generateNotificationReferrerId, isEnterpriseHost, - isLinux, - isMacOS, isOAuthAppLoggedIn, isPersonalAccessTokenLoggedIn, } from './helpers'; @@ -536,34 +533,4 @@ describe('utils/helpers.ts', () => { ); }); }); - - describe('platforms', () => { - let originalPlatform: PropertyDescriptor; - - beforeAll(() => { - // Save the original process.platform - originalPlatform = Object.getOwnPropertyDescriptor(process, 'platform'); - }); - - afterEach(() => { - // Restore the original process.platform after each test - Object.defineProperty(process, 'platform', originalPlatform); - }); - - it('isMacOS', () => { - mockPlatform('darwin'); - expect(isMacOS()).toBe(true); - - mockPlatform('linux'); - expect(isMacOS()).toBe(false); - }); - - it('isLinux', () => { - mockPlatform('darwin'); - expect(isLinux()).toBe(false); - - mockPlatform('linux'); - expect(isLinux()).toBe(true); - }); - }); }); diff --git a/src/utils/helpers.ts b/src/utils/helpers.ts index 5baaee19a..7101c0638 100644 --- a/src/utils/helpers.ts +++ b/src/utils/helpers.ts @@ -186,11 +186,3 @@ export async function openInBrowser( openExternalLink(url); } - -export function isMacOS(): boolean { - return process.platform === 'darwin'; -} - -export function isLinux(): boolean { - return process.platform === 'linux'; -} diff --git a/src/utils/notifications.ts b/src/utils/notifications.ts index 4856b6c2b..c8ee96765 100644 --- a/src/utils/notifications.ts +++ b/src/utils/notifications.ts @@ -9,6 +9,7 @@ import { import { listNotificationsForAuthenticatedUser } from './api/client'; import { updateTrayIcon } from './comms'; import Constants from './constants'; +import { isWindows } from './platform'; import { getGitifySubjectDetails } from './subject'; export const setTrayIconColor = (notifications: AccountNotifications[]) => { @@ -76,7 +77,7 @@ export const raiseNativeNotification = ( if (notifications.length === 1) { const notification = notifications[0]; - title = `${process.platform !== 'win32' ? 'Gitify - ' : ''}${ + title = `${isWindows() ? '' : 'Gitify - '}${ notification.repository.full_name }`; body = notification.subject.title; diff --git a/src/utils/platform.ts b/src/utils/platform.ts new file mode 100644 index 000000000..bef160c9e --- /dev/null +++ b/src/utils/platform.ts @@ -0,0 +1,11 @@ +export function isLinux(): boolean { + return process.platform === 'linux'; +} + +export function isMacOS(): boolean { + return process.platform === 'darwin'; +} + +export function isWindows(): boolean { + return process.platform === 'win32'; +} From 67af038700aeb988adbc7c7639c7fb2fb86aa1b1 Mon Sep 17 00:00:00 2001 From: Adam Setch Date: Wed, 29 May 2024 07:41:49 -0400 Subject: [PATCH 4/5] refactor: remove ipc for fetching platform type --- src/__mocks__/@electron/remote.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/__mocks__/@electron/remote.js b/src/__mocks__/@electron/remote.js index 6e148d15c..c2250c249 100644 --- a/src/__mocks__/@electron/remote.js +++ b/src/__mocks__/@electron/remote.js @@ -28,6 +28,9 @@ const dialog = { module.exports = { BrowserWindow: BrowserWindow, dialog: dialog, + process: { + platform: 'darwin', + }, app: { getLoginItemSettings: jest.fn(), setLoginItemSettings: () => {}, From d190dfe08bae37499adf8a163989ca53ee2ca243 Mon Sep 17 00:00:00 2001 From: Adam Setch Date: Wed, 29 May 2024 07:47:54 -0400 Subject: [PATCH 5/5] refactor: remove ipc for fetching platform type --- src/__mocks__/@electron/remote.js | 3 --- src/__mocks__/utils.ts | 5 +++++ src/routes/Settings.test.tsx | 15 +++++++++++++++ 3 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 src/__mocks__/utils.ts diff --git a/src/__mocks__/@electron/remote.js b/src/__mocks__/@electron/remote.js index c2250c249..6e148d15c 100644 --- a/src/__mocks__/@electron/remote.js +++ b/src/__mocks__/@electron/remote.js @@ -28,9 +28,6 @@ const dialog = { module.exports = { BrowserWindow: BrowserWindow, dialog: dialog, - process: { - platform: 'darwin', - }, app: { getLoginItemSettings: jest.fn(), setLoginItemSettings: () => {}, diff --git a/src/__mocks__/utils.ts b/src/__mocks__/utils.ts new file mode 100644 index 000000000..22149c55f --- /dev/null +++ b/src/__mocks__/utils.ts @@ -0,0 +1,5 @@ +export function mockPlatform(platform: NodeJS.Platform) { + Object.defineProperty(process, 'platform', { + value: platform, + }); +} diff --git a/src/routes/Settings.test.tsx b/src/routes/Settings.test.tsx index ce3d69a51..e2891033d 100644 --- a/src/routes/Settings.test.tsx +++ b/src/routes/Settings.test.tsx @@ -2,6 +2,7 @@ import { act, fireEvent, render, screen } from '@testing-library/react'; import { ipcRenderer, shell } from 'electron'; import { MemoryRouter } from 'react-router-dom'; import { mockAuth, mockSettings } from '../__mocks__/state-mocks'; +import { mockPlatform } from '../__mocks__/utils'; import { AppContext } from '../context/App'; import { SettingsRoute } from './Settings'; @@ -12,12 +13,26 @@ jest.mock('react-router-dom', () => ({ })); describe('routes/Settings.tsx', () => { + let originalPlatform: NodeJS.Platform; const updateSetting = jest.fn(); + beforeAll(() => { + // Save the original platform value + originalPlatform = process.platform; + mockPlatform('darwin'); + }); + beforeEach(() => { jest.clearAllMocks(); }); + afterAll(() => { + // Restore the original platform value + Object.defineProperty(process, 'platform', { + value: originalPlatform, + }); + }); + describe('General', () => { it('should render itself & its children', async () => { await act(async () => {