Skip to content

refactor: process platform #1153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
3 changes: 0 additions & 3 deletions src/__mocks__/@electron/remote.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ const dialog = {
module.exports = {
BrowserWindow: BrowserWindow,
dialog: dialog,
process: {
platform: 'darwin',
},
app: {
getLoginItemSettings: jest.fn(),
setLoginItemSettings: () => {},
Expand Down
5 changes: 5 additions & 0 deletions src/__mocks__/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export function mockPlatform(platform: NodeJS.Platform) {
Object.defineProperty(process, 'platform', {
value: platform,
});
}
15 changes: 15 additions & 0 deletions src/routes/Settings.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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 () => {
Expand Down
13 changes: 3 additions & 10 deletions src/routes/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand All @@ -41,8 +42,6 @@ export const SettingsRoute: FC = () => {
} = useContext(AppContext);
const navigate = useNavigate();

const [isLinux, setIsLinux] = useState<boolean>(false);
const [isMacOS, setIsMacOS] = useState<boolean>(false);
const [appVersion, setAppVersion] = useState<string | null>(null);

const openGitHubReleaseNotes = useCallback((version) => {
Expand All @@ -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);
Expand Down Expand Up @@ -253,7 +246,7 @@ export const SettingsRoute: FC = () => {
<legend id="system" className="font-semibold mt-2 mb-1">
System
</legend>
{isMacOS && (
{isMacOS() && (
<Checkbox
name="showNotificationsCountInTray"
label="Show notifications count in tray"
Expand All @@ -280,7 +273,7 @@ export const SettingsRoute: FC = () => {
checked={settings.playSound}
onChange={(evt) => updateSetting('playSound', evt.target.checked)}
/>
{!isLinux && (
{!isLinux() && (
<Checkbox
name="openAtStartUp"
label="Open at startup"
Expand Down
3 changes: 2 additions & 1 deletion src/utils/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[]) => {
Expand Down Expand Up @@ -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;
Expand Down
11 changes: 11 additions & 0 deletions src/utils/platform.ts
Original file line number Diff line number Diff line change
@@ -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';
}