Skip to content

Commit

Permalink
Merge fbc46a9 into 291f35b
Browse files Browse the repository at this point in the history
  • Loading branch information
setchy committed Jun 8, 2024
2 parents 291f35b + fbc46a9 commit 99c4bad
Show file tree
Hide file tree
Showing 13 changed files with 117 additions and 69 deletions.
48 changes: 25 additions & 23 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,37 +73,39 @@ mb.on('ready', () => {

nativeTheme.on('updated', () => {
if (nativeTheme.shouldUseDarkColors) {
mb.window.webContents.send('update-native-theme', 'DARK');
mb.window.webContents.send('gitify:update-theme', 'DARK');
} else {
mb.window.webContents.send('update-native-theme', 'LIGHT');
mb.window.webContents.send('gitify:update-theme', 'LIGHT');
}
});
});

ipc.handle('get-app-version', () => app.getVersion());
ipc.handle('gitify:version', () => app.getVersion());

ipc.on('reopen-window', () => mb.showWindow());
ipc.on('gitify:window-show', () => mb.showWindow());

ipc.on('hide-window', () => mb.hideWindow());
ipc.on('gitify:window-hide', () => mb.hideWindow());

ipc.on('app-quit', () => mb.app.quit());
ipc.on('gitify:quit', () => mb.app.quit());

ipc.on('update-icon', (_, arg) => {
if (!mb.tray.isDestroyed()) {
if (arg === 'TrayActive') {
mb.tray.setImage(iconActive);
} else {
mb.tray.setImage(iconIdle);
}
}
});
ipc.on('gitify:icon-active', () => {
if (!mb.tray.isDestroyed()) {
mb.tray.setImage(iconActive);
}
});

ipc.on('update-title', (_, title) => {
if (!mb.tray.isDestroyed()) {
mb.tray.setTitle(title);
}
});
ipc.on('gitify:icon-idle', () => {
if (!mb.tray.isDestroyed()) {
mb.tray.setImage(iconIdle);
}
});

ipc.on('set-login-item-settings', (_, settings) => {
app.setLoginItemSettings(settings);
});
ipc.on('gitify:update-title', (_, title) => {
if (!mb.tray.isDestroyed()) {
mb.tray.setTitle(title);
}
});

ipc.on('gitify:update-auto-launch', (_, settings) => {
app.setLoginItemSettings(settings);
});
2 changes: 1 addition & 1 deletion src/__mocks__/electron.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ module.exports = {
switch (channel) {
case 'get-platform':
return Promise.resolve('darwin');
case 'get-app-version':
case 'gitify:version':
return Promise.resolve('0.0.1');
default:
return Promise.reject(new Error(`Unknown channel: ${channel}`));
Expand Down
9 changes: 5 additions & 4 deletions src/components/Sidebar.test.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { fireEvent, render, screen } from '@testing-library/react';
import { ipcRenderer, shell } from 'electron';
import { shell } from 'electron';
import { MemoryRouter } from 'react-router-dom';
import { mockAccountNotifications } from '../__mocks__/notifications-mocks';
import { mockSettings } from '../__mocks__/state-mocks';
import { AppContext } from '../context/App';
import * as comms from '../utils/comms';
import { Sidebar } from './Sidebar';

const mockNavigate = jest.fn();
Expand All @@ -18,7 +19,6 @@ describe('components/Sidebar.tsx', () => {
beforeEach(() => {
fetchNotifications.mockReset();

jest.spyOn(ipcRenderer, 'send');
jest.spyOn(shell, 'openExternal');
jest.spyOn(window, 'clearInterval');
});
Expand Down Expand Up @@ -147,6 +147,8 @@ describe('components/Sidebar.tsx', () => {
});

it('should quit the app', () => {
const quitAppMock = jest.spyOn(comms, 'quitApp');

render(
<AppContext.Provider value={{ isLoggedIn: false, notifications: [] }}>
<MemoryRouter>
Expand All @@ -155,8 +157,7 @@ describe('components/Sidebar.tsx', () => {
</AppContext.Provider>,
);
fireEvent.click(screen.getByTitle('Quit Gitify'));
expect(ipcRenderer.send).toHaveBeenCalledTimes(1);
expect(ipcRenderer.send).toHaveBeenCalledWith('app-quit');
expect(quitAppMock).toHaveBeenCalledTimes(1);
});

it('should open the gitify repository', () => {
Expand Down
7 changes: 1 addition & 6 deletions src/components/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import {
SyncIcon,
XCircleIcon,
} from '@primer/octicons-react';
import { ipcRenderer } from 'electron';
import { type FC, useCallback, useContext, useMemo } from 'react';
import { useLocation, useNavigate } from 'react-router-dom';
import { Logo } from '../components/Logo';
import { AppContext } from '../context/App';
import { openExternalLink } from '../utils/comms';
import { openExternalLink, quitApp } from '../utils/comms';
import { Constants } from '../utils/constants';
import { getNotificationCount } from '../utils/notifications';

Expand All @@ -28,10 +27,6 @@ export const Sidebar: FC = () => {
openExternalLink('https://github.com/notifications');
}, []);

const quitApp = useCallback(() => {
ipcRenderer.send('app-quit');
}, []);

const toggleSettings = () => {
if (location.pathname.startsWith('/settings')) {
navigate('/', { replace: true });
Expand Down
13 changes: 9 additions & 4 deletions src/routes/Accounts.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { act, fireEvent, render, screen } from '@testing-library/react';
import { ipcRenderer, shell } from 'electron';
import { shell } from 'electron';
import { MemoryRouter } from 'react-router-dom';
import {
mockAuth,
Expand All @@ -9,6 +9,7 @@ import {
mockSettings,
} from '../__mocks__/state-mocks';
import { AppContext } from '../context/App';
import * as comms from '../utils/comms';
import { AccountsRoute } from './Accounts';

const mockNavigate = jest.fn();
Expand Down Expand Up @@ -148,6 +149,9 @@ describe('routes/Accounts.tsx', () => {

it('should logout', async () => {
const logoutFromAccountMock = jest.fn();
const updateTrayIconMock = jest.spyOn(comms, 'updateTrayIcon');
const updateTrayTitleMock = jest.spyOn(comms, 'updateTrayTitle');

await act(async () => {
render(
<AppContext.Provider
Expand All @@ -170,9 +174,10 @@ describe('routes/Accounts.tsx', () => {

expect(logoutFromAccountMock).toHaveBeenCalledTimes(1);

expect(ipcRenderer.send).toHaveBeenCalledTimes(2);
expect(ipcRenderer.send).toHaveBeenCalledWith('update-icon');
expect(ipcRenderer.send).toHaveBeenCalledWith('update-title', '');
expect(updateTrayIconMock).toHaveBeenCalledTimes(1);
expect(updateTrayIconMock).toHaveBeenCalledWith();
expect(updateTrayTitleMock).toHaveBeenCalledTimes(1);
expect(updateTrayTitleMock).toHaveBeenCalledWith();
expect(mockNavigate).toHaveBeenNthCalledWith(1, -1);
});
});
Expand Down
8 changes: 4 additions & 4 deletions src/routes/Login.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { fireEvent, render, screen } from '@testing-library/react';
import { ipcRenderer } from 'electron';
import { MemoryRouter } from 'react-router-dom';
import { AppContext } from '../context/App';
import * as comms from '../utils/comms';
import { LoginRoute } from './Login';

const mockNavigate = jest.fn();
Expand All @@ -13,7 +13,6 @@ jest.mock('react-router-dom', () => ({
describe('routes/Login.tsx', () => {
beforeEach(() => {
mockNavigate.mockReset();
jest.spyOn(ipcRenderer, 'send');
});

it('should render itself & its children', () => {
Expand All @@ -27,6 +26,8 @@ describe('routes/Login.tsx', () => {
});

it('should redirect to notifications once logged in', () => {
const showWindowMock = jest.spyOn(comms, 'showWindow');

const { rerender } = render(
<AppContext.Provider value={{ isLoggedIn: false }}>
<MemoryRouter>
Expand All @@ -43,8 +44,7 @@ describe('routes/Login.tsx', () => {
</AppContext.Provider>,
);

expect(ipcRenderer.send).toHaveBeenCalledTimes(1);
expect(ipcRenderer.send).toHaveBeenCalledWith('reopen-window');
expect(showWindowMock).toHaveBeenCalledTimes(1);
expect(mockNavigate).toHaveBeenNthCalledWith(1, '/', { replace: true });
});

Expand Down
4 changes: 2 additions & 2 deletions src/routes/Login.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { KeyIcon, PersonIcon } from '@primer/octicons-react';
import { ipcRenderer } from 'electron';
import { type FC, useContext, useEffect } from 'react';
import { useNavigate } from 'react-router-dom';
import { Logo } from '../components/Logo';
import { Button } from '../components/fields/Button';
import { AppContext } from '../context/App';
import { showWindow } from '../utils/comms';

export const LoginRoute: FC = () => {
const navigate = useNavigate();
const { isLoggedIn } = useContext(AppContext);

useEffect(() => {
if (isLoggedIn) {
ipcRenderer.send('reopen-window');
showWindow();
navigate('/', { replace: true });
}
}, [isLoggedIn]);
Expand Down
7 changes: 5 additions & 2 deletions src/routes/Settings.test.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { act, fireEvent, render, screen } from '@testing-library/react';
import { ipcRenderer, shell } from 'electron';
import { 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 * as comms from '../utils/comms';
import { SettingsRoute } from './Settings';

const mockNavigate = jest.fn();
Expand Down Expand Up @@ -524,6 +525,8 @@ describe('routes/Settings.tsx', () => {
});

it('should quit the app', async () => {
const quitAppMock = jest.spyOn(comms, 'quitApp');

await act(async () => {
render(
<AppContext.Provider
Expand All @@ -540,7 +543,7 @@ describe('routes/Settings.tsx', () => {
});

fireEvent.click(screen.getByTitle('Quit Gitify'));
expect(ipcRenderer.send).toHaveBeenCalledWith('app-quit');
expect(quitAppMock).toHaveBeenCalledTimes(1);
});
});
});
10 changes: 3 additions & 7 deletions src/routes/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { Checkbox } from '../components/fields/Checkbox';
import { RadioGroup } from '../components/fields/RadioGroup';
import { AppContext } from '../context/App';
import { Theme } from '../types';
import { openExternalLink } from '../utils/comms';
import { getAppVersion, openExternalLink, quitApp } from '../utils/comms';
import Constants from '../utils/constants';
import { isLinux, isMacOS } from '../utils/platform';
import { setTheme } from '../utils/theme';
Expand Down Expand Up @@ -50,21 +50,17 @@ export const SettingsRoute: FC = () => {

useEffect(() => {
(async () => {
const result = await ipcRenderer.invoke('get-app-version');
const result = await getAppVersion();
setAppVersion(result);
})();

ipcRenderer.on('update-native-theme', (_, updatedTheme: Theme) => {
ipcRenderer.on('gitify:update-theme', (_, updatedTheme: Theme) => {
if (settings.theme === Theme.SYSTEM) {
setTheme(updatedTheme);
}
});
}, []);

const quitApp = useCallback(() => {
ipcRenderer.send('app-quit');
}, []);

const footerButtonClass =
'hover:text-gray-500 py-1 px-2 my-1 mx-2 focus:outline-none';

Expand Down
37 changes: 33 additions & 4 deletions src/utils/comms.test.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,61 @@
import { ipcRenderer, shell } from 'electron';
import {
getAppVersion,
hideWindow,
openExternalLink,
quitApp,
restoreSetting,
setAutoLaunch,
showWindow,
updateTrayIcon,
} from './comms';

describe('utils/comms.ts', () => {
beforeEach(() => {
jest.spyOn(ipcRenderer, 'send');
jest.spyOn(ipcRenderer, 'invoke');
});

afterEach(() => {
jest.clearAllMocks();
});

it('should get app version', async () => {
await getAppVersion();
expect(ipcRenderer.invoke).toHaveBeenCalledTimes(1);
expect(ipcRenderer.invoke).toHaveBeenCalledWith('gitify:version');
});

it('should quit the app', () => {
quitApp();
expect(ipcRenderer.send).toHaveBeenCalledTimes(1);
expect(ipcRenderer.send).toHaveBeenCalledWith('gitify:quit');
});

it('should show the window', () => {
showWindow();
expect(ipcRenderer.send).toHaveBeenCalledTimes(1);
expect(ipcRenderer.send).toHaveBeenCalledWith('gitify:window-show');
});

it('should hide the window', () => {
hideWindow();
expect(ipcRenderer.send).toHaveBeenCalledTimes(1);
expect(ipcRenderer.send).toHaveBeenCalledWith('gitify:window-hide');
});

it('should send mark the icons as active', () => {
const notificationsLength = 3;
updateTrayIcon(notificationsLength);
expect(ipcRenderer.send).toHaveBeenCalledTimes(1);
expect(ipcRenderer.send).toHaveBeenCalledWith('update-icon', 'TrayActive');
expect(ipcRenderer.send).toHaveBeenCalledWith('gitify:icon-active');
});

it('should send mark the icons as idle', () => {
const notificationsLength = 0;
updateTrayIcon(notificationsLength);
expect(ipcRenderer.send).toHaveBeenCalledTimes(1);
expect(ipcRenderer.send).toHaveBeenCalledWith('update-icon');
expect(ipcRenderer.send).toHaveBeenCalledWith('gitify:icon-idle');
});

it('should restore a setting', () => {
Expand All @@ -49,7 +78,7 @@ describe('utils/comms.ts', () => {
it('should setAutoLaunch (true)', () => {
setAutoLaunch(true);

expect(ipcRenderer.send).toHaveBeenCalledWith('set-login-item-settings', {
expect(ipcRenderer.send).toHaveBeenCalledWith('gitify:update-auto-launch', {
openAtLogin: true,
openAsHidden: true,
});
Expand All @@ -58,7 +87,7 @@ describe('utils/comms.ts', () => {
it('should setAutoLaunch (false)', () => {
setAutoLaunch(false);

expect(ipcRenderer.send).toHaveBeenCalledWith('set-login-item-settings', {
expect(ipcRenderer.send).toHaveBeenCalledWith('gitify:update-auto-launch', {
openAsHidden: false,
openAtLogin: false,
});
Expand Down
Loading

0 comments on commit 99c4bad

Please sign in to comment.