Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions src/renderer/utils/notifications/native.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { waitFor } from '@testing-library/react';

import {
mockAccountNotifications,
mockSingleAccountNotifications,
Expand All @@ -15,7 +17,7 @@ describe('renderer/utils/notifications/native.ts', () => {
});

describe('triggerNativeNotifications', () => {
it('should raise a native notification and play sound for a single new notification', () => {
it('should raise a native notification and play sound for a single new notification', async () => {
const settings: SettingsState = {
...defaultSettings,
playSound: true,
Expand All @@ -26,8 +28,11 @@ describe('renderer/utils/notifications/native.ts', () => {
auth: mockAuth,
settings,
});
// wait for async native handling (generateGitHubWebUrl) to complete
await waitFor(() =>
expect(window.gitify.raiseNativeNotification).toHaveBeenCalledTimes(1),
);

expect(window.gitify.raiseNativeNotification).toHaveBeenCalledTimes(1);
expect(window.gitify.raiseNativeNotification).toHaveBeenCalledWith(
expect.stringContaining(
mockSingleAccountNotifications[0].notifications[0].repository
Expand All @@ -36,14 +41,19 @@ describe('renderer/utils/notifications/native.ts', () => {
expect.stringContaining(
mockSingleAccountNotifications[0].notifications[0].subject.title,
),
null,
expect.stringContaining(
mockSingleAccountNotifications[0].notifications[0].repository
.html_url,
),
);

expect(raiseSoundNotificationMock).toHaveBeenCalledTimes(1);
await waitFor(() =>
expect(raiseSoundNotificationMock).toHaveBeenCalledTimes(1),
);
expect(raiseSoundNotificationMock).toHaveBeenCalledWith(0.2);
});

it('should raise a native notification and play sound for multiple new notifications', () => {
it('should raise a native notification and play sound for multiple new notifications', async () => {
const settings: SettingsState = {
...defaultSettings,
playSound: true,
Expand All @@ -54,15 +64,19 @@ describe('renderer/utils/notifications/native.ts', () => {
auth: mockAuth,
settings,
});
await waitFor(() =>
expect(window.gitify.raiseNativeNotification).toHaveBeenCalledTimes(1),
);

expect(window.gitify.raiseNativeNotification).toHaveBeenCalledTimes(1);
expect(window.gitify.raiseNativeNotification).toHaveBeenCalledWith(
'Gitify',
'You have 4 notifications',
null,
);

expect(raiseSoundNotificationMock).toHaveBeenCalledTimes(1);
await waitFor(() =>
expect(raiseSoundNotificationMock).toHaveBeenCalledTimes(1),
);
expect(raiseSoundNotificationMock).toHaveBeenCalledWith(0.2);
});

Expand Down
9 changes: 6 additions & 3 deletions src/renderer/utils/notifications/native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { APPLICATION } from '../../../shared/constants';
import type { AccountNotifications, GitifyState } from '../../types';
import type { Notification } from '../../typesGitHub';
import { getAccountUUID } from '../auth/utils';
import { generateGitHubWebUrl } from '../helpers';
import { setTrayIconColor } from './notifications';

export const triggerNativeNotifications = (
Expand Down Expand Up @@ -50,18 +51,20 @@ export const triggerNativeNotifications = (
}
};

export const raiseNativeNotification = (notifications: Notification[]) => {
export const raiseNativeNotification = async (
notifications: Notification[],
) => {
let title: string;
let body: string;
const url: string = null;
let url: string = null;

if (notifications.length === 1) {
const notification = notifications[0];
title = window.gitify.platform.isWindows()
? ''
: notification.repository.full_name;
body = notification.subject.title;
// url intentionally left null (no direct subject URL available)
url = await generateGitHubWebUrl(notification);
} else {
title = APPLICATION.NAME;
body = `You have ${notifications.length} notifications`;
Expand Down