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/__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 1060e9adc..768443c60 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 () => { diff --git a/src/routes/Settings.tsx b/src/routes/Settings.tsx index 7a0aec680..553fa864c 100644 --- a/src/routes/Settings.tsx +++ b/src/routes/Settings.tsx @@ -30,6 +30,7 @@ import { isOAuthAppLoggedIn, isPersonalAccessTokenLoggedIn, } from '../utils/helpers'; +import { isLinux, isMacOS } from '../utils/platform'; import { setTheme } from '../utils/theme'; export const SettingsRoute: FC = () => { @@ -41,8 +42,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 +60,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 +246,7 @@ export const SettingsRoute: FC = () => { System - {isMacOS && ( + {isMacOS() && ( { checked={settings.playSound} onChange={(evt) => updateSetting('playSound', evt.target.checked)} /> - {!isLinux && ( + {!isLinux() && ( { @@ -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'; +}