Skip to content

Commit

Permalink
test: add unit tests for keyboardshotcut. reorganize tests
Browse files Browse the repository at this point in the history
Signed-off-by: Adam Setch <[email protected]>
  • Loading branch information
setchy committed Sep 8, 2024
1 parent 6bc85ed commit fa77c99
Showing 1 changed file with 75 additions and 41 deletions.
116 changes: 75 additions & 41 deletions src/utils/comms.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import {
quitApp,
setAlternateIdleIcon,
setAutoLaunch,
setKeyboardShortcut,
showWindow,
updateTrayIcon,
} from './comms';
import { Constants } from './constants';
import * as storage from './storage';

describe('utils/comms.ts', () => {
Expand All @@ -23,6 +25,41 @@ describe('utils/comms.ts', () => {
jest.clearAllMocks();
});

describe('openExternalLink', () => {
it('should open an external link', () => {
jest
.spyOn(storage, 'loadState')
.mockReturnValue({ settings: mockSettings });

openExternalLink('https://www.gitify.io/' as Link);
expect(shell.openExternal).toHaveBeenCalledTimes(1);
expect(shell.openExternal).toHaveBeenCalledWith(
'https://www.gitify.io/',
{
activate: true,
},
);
});

it('should use default open preference if user settings not found', () => {
jest.spyOn(storage, 'loadState').mockReturnValue({ settings: null });

openExternalLink('https://www.gitify.io/' as Link);
expect(shell.openExternal).toHaveBeenCalledTimes(1);
expect(shell.openExternal).toHaveBeenCalledWith(
'https://www.gitify.io/',
{
activate: true,
},
);
});

it('should ignore opening external local links file:///', () => {
openExternalLink('file:///Applications/SomeApp.app' as Link);
expect(shell.openExternal).toHaveBeenCalledTimes(0);
});
});

it('should get app version', async () => {
await getAppVersion();
expect(ipcRenderer.invoke).toHaveBeenCalledTimes(1);
Expand All @@ -47,47 +84,6 @@ describe('utils/comms.ts', () => {
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('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('gitify:icon-idle');
});

it('should open an external link', () => {
jest
.spyOn(storage, 'loadState')
.mockReturnValue({ settings: mockSettings });

openExternalLink('https://www.gitify.io/' as Link);
expect(shell.openExternal).toHaveBeenCalledTimes(1);
expect(shell.openExternal).toHaveBeenCalledWith('https://www.gitify.io/', {
activate: true,
});
});

it('should use default open preference if user settings not found', () => {
jest.spyOn(storage, 'loadState').mockReturnValue({ settings: null });

openExternalLink('https://www.gitify.io/' as Link);
expect(shell.openExternal).toHaveBeenCalledTimes(1);
expect(shell.openExternal).toHaveBeenCalledWith('https://www.gitify.io/', {
activate: true,
});
});

it('should ignore opening external local links file:///', () => {
openExternalLink('file:///Applications/SomeApp.app' as Link);
expect(shell.openExternal).toHaveBeenCalledTimes(0);
});

it('should setAutoLaunch (true)', () => {
setAutoLaunch(true);

Expand All @@ -114,4 +110,42 @@ describe('utils/comms.ts', () => {
true,
);
});

it('should enable keyboard shortcut', () => {
setKeyboardShortcut(true);
expect(ipcRenderer.send).toHaveBeenCalledTimes(1);
expect(ipcRenderer.send).toHaveBeenCalledWith(
'gitify:update-keyboard-shortcut',
{
enabled: true,
keyboardShortcut: Constants.DEFAULT_KEYBOARD_SHORTCUT,
},
);
});

it('should disable keyboard shortcut', () => {
setKeyboardShortcut(false);
expect(ipcRenderer.send).toHaveBeenCalledTimes(1);
expect(ipcRenderer.send).toHaveBeenCalledWith(
'gitify:update-keyboard-shortcut',
{
enabled: false,
keyboardShortcut: Constants.DEFAULT_KEYBOARD_SHORTCUT,
},
);
});

it('should send mark the icons as active', () => {
const notificationsLength = 3;
updateTrayIcon(notificationsLength);
expect(ipcRenderer.send).toHaveBeenCalledTimes(1);
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('gitify:icon-idle');
});
});

0 comments on commit fa77c99

Please sign in to comment.