Repository: gitify-app/gitify
Pull request #790 (2024-02-18 21:27:18)

test: add notification color coverage
Also sort switch by alphabetic state type


Patch:
diff --git a/src/hooks/useNotifications.ts b/src/hooks/useNotifications.ts
index 5634d259..20d1ae88 100644
--- a/src/hooks/useNotifications.ts
+++ b/src/hooks/useNotifications.ts
@@ -8,6 +8,7 @@ import { apiRequestAuth } from '../utils/api-requests';
 import {
   getEnterpriseAccountToken,
   generateGitHubAPIUrl,
+  isEnterpriseHost,
 } from '../utils/helpers';
 import { removeNotification } from '../utils/remove-notification';
 import {
@@ -130,9 +131,9 @@ export const useNotifications = (colors: boolean): NotificationsState => {
                           ) {
                             return notification;
                           }
-                          const isEnterprise =
-                            accountNotifications.hostname !==
-                            Constants.DEFAULT_AUTH_OPTIONS.hostname;
+                          const isEnterprise = isEnterpriseHost(
+                            accountNotifications.hostname,
+                          );
                           const token = isEnterprise
                             ? getEnterpriseAccountToken(
                                 accountNotifications.hostname,
@@ -192,7 +193,7 @@ export const useNotifications = (colors: boolean): NotificationsState => {
     async (accounts, id, hostname) => {
       setIsFetching(true);
 
-      const isEnterprise = hostname !== Constants.DEFAULT_AUTH_OPTIONS.hostname;
+      const isEnterprise = isEnterpriseHost(hostname);
       const token = isEnterprise
         ? getEnterpriseAccountToken(hostname, accounts.enterpriseAccounts)
         : accounts.token;
@@ -225,7 +226,7 @@ export const useNotifications = (colors: boolean): NotificationsState => {
     async (accounts, id, hostname) => {
       setIsFetching(true);
 
-      const isEnterprise = hostname !== Constants.DEFAULT_AUTH_OPTIONS.hostname;
+      const isEnterprise = isEnterpriseHost(hostname);
       const token = isEnterprise
         ? getEnterpriseAccountToken(hostname, accounts.enterpriseAccounts)
         : accounts.token;
@@ -258,7 +259,7 @@ export const useNotifications = (colors: boolean): NotificationsState => {
     async (accounts, id, hostname) => {
       setIsFetching(true);
 
-      const isEnterprise = hostname !== Constants.DEFAULT_AUTH_OPTIONS.hostname;
+      const isEnterprise = isEnterpriseHost(hostname);
       const token = isEnterprise
         ? getEnterpriseAccountToken(hostname, accounts.enterpriseAccounts)
         : accounts.token;
@@ -284,7 +285,7 @@ export const useNotifications = (colors: boolean): NotificationsState => {
     async (accounts, repoSlug, hostname) => {
       setIsFetching(true);
 
-      const isEnterprise = hostname !== Constants.DEFAULT_AUTH_OPTIONS.hostname;
+      const isEnterprise = isEnterpriseHost(hostname);
       const token = isEnterprise
         ? getEnterpriseAccountToken(hostname, accounts.enterpriseAccounts)
         : accounts.token;
diff --git a/src/typesGithub.ts b/src/typesGithub.ts
index 3f4ac637..98dfa7f5 100644
--- a/src/typesGithub.ts
+++ b/src/typesGithub.ts
@@ -28,8 +28,11 @@ export type IssueStateType =
   | 'completed'
   | 'reopened'
   | 'not_planned';
+
 export type PullRequestStateType = 'closed' | 'open' | 'merged' | 'draft';
+
 export type StateType = IssueStateType | PullRequestStateType;
+
 export type ViewerSubscription = 'IGNORED' | 'SUBSCRIBED' | 'UNSUBSCRIBED';
 
 export interface Notification {
diff --git a/src/utils/auth.ts b/src/utils/auth.ts
index 96d83242..f46ccc7e 100644
--- a/src/utils/auth.ts
+++ b/src/utils/auth.ts
@@ -1,6 +1,6 @@
 import { BrowserWindow } from '@electron/remote';
 
-import { generateGitHubAPIUrl } from './helpers';
+import { generateGitHubAPIUrl, isEnterpriseHost } from './helpers';
 import { apiRequest, apiRequestAuth } from '../utils/api-requests';
 import { AuthResponse, AuthState, AuthTokenResponse } from '../types';
 import { Constants } from '../utils/constants';
@@ -114,7 +114,7 @@ export const addAccount = (
   hostname,
   user?: User,
 ): AuthState => {
-  if (hostname === Constants.DEFAULT_AUTH_OPTIONS.hostname) {
+  if (!isEnterpriseHost(hostname)) {
     return {
       ...accounts,
       token,
diff --git a/src/utils/github-api.ts b/src/utils/github-api.ts
index 462f39e7..ea1adca0 100644
--- a/src/utils/github-api.ts
+++ b/src/utils/github-api.ts
@@ -119,18 +119,18 @@ export function getNotificationTypeIconColor(state: StateType): string {
   switch (state) {
     case 'closed':
       return 'text-red-500';
-    case 'open':
-      return 'text-green-500';
-    case 'merged':
-      return 'text-purple-500';
-    case 'reopened':
-      return 'text-green-500';
-    case 'not_planned':
-      return 'text-gray-300';
     case 'completed':
       return 'text-purple-500';
     case 'draft':
       return 'text-gray-600';
+    case 'merged':
+      return 'text-purple-500';
+    case 'not_planned':
+      return 'text-gray-300';
+    case 'open':
+      return 'text-green-500';
+    case 'reopened':
+      return 'text-green-500';
     default:
       return 'text-gray-300';
   }
diff --git a/src/utils/helpers.ts b/src/utils/helpers.ts
index 708aac3a..e84fd3a5 100644
--- a/src/utils/helpers.ts
+++ b/src/utils/helpers.ts
@@ -15,8 +15,12 @@ export function getEnterpriseAccountToken(
   return accounts.find((obj) => obj.hostname === hostname).token;
 }
 
+export function isEnterpriseHost(hostname: string): boolean {
+  return !hostname.endsWith(Constants.DEFAULT_AUTH_OPTIONS.hostname);
+}
+
 export function generateGitHubAPIUrl(hostname) {
-  const isEnterprise = hostname !== Constants.DEFAULT_AUTH_OPTIONS.hostname;
+  const isEnterprise = isEnterpriseHost(hostname);
   return isEnterprise
     ? `https://${hostname}/api/v3/`
     : `https://api.${hostname}/`;
@@ -39,8 +43,7 @@ export function generateGitHubWebUrl(
   comment: string = '',
 ) {
   const { hostname } = new URL(url);
-  const isEnterprise =
-    hostname !== `api.${Constants.DEFAULT_AUTH_OPTIONS.hostname}`;
+  const isEnterprise = isEnterpriseHost(hostname);
 
   let newUrl: string = isEnterprise
     ? url.replace(`${hostname}/api/v3/repos`, hostname)


Test patch:
diff --git a/src/utils/__snapshots__/github-api.test.ts.snap b/src/utils/__snapshots__/github-api.test.ts.snap
index 8b1b1033..327ef366 100644
--- a/src/utils/__snapshots__/github-api.test.ts.snap
+++ b/src/utils/__snapshots__/github-api.test.ts.snap
@@ -1,5 +1,26 @@
 // Jest Snapshot v1, https://goo.gl/fbAQLP
 
+exports[`./utils/github-api.ts should format the notification color 1`] = `"text-red-500"`;
+
+exports[`./utils/github-api.ts should format the notification color 2`] = `"text-purple-500"`;
+
+exports[`./utils/github-api.ts should format the notification color 3`] = `"text-gray-600"`;
+
+exports[`./utils/github-api.ts should format the notification color 4`] = `"text-purple-500"`;
+
+exports[`./utils/github-api.ts should format the notification color 5`] = `"text-gray-300"`;
+
+exports[`./utils/github-api.ts should format the notification color 6`] = `"text-green-500"`;
+
+exports[`./utils/github-api.ts should format the notification color 7`] = `"text-green-500"`;
+
+exports[`./utils/github-api.ts should format the notification color 8`] = `
+{
+  "description": "The reason for this notification is not supported by the app.",
+  "type": "Unknown",
+}
+`;
+
 exports[`./utils/github-api.ts should format the notification reason 1`] = `
 {
   "description": "You were assigned to the issue.",
diff --git a/src/utils/github-api.test.ts b/src/utils/github-api.test.ts
index fec6cca3..25ccac95 100644
--- a/src/utils/github-api.test.ts
+++ b/src/utils/github-api.test.ts
@@ -1,4 +1,8 @@
-import { formatReason, getNotificationTypeIcon } from './github-api';
+import {
+  formatReason,
+  getNotificationTypeIcon,
+  getNotificationTypeIconColor,
+} from './github-api';
 import { Reason, SubjectType } from '../typesGithub';
 
 describe('./utils/github-api.ts', () => {
@@ -62,4 +66,15 @@ describe('./utils/github-api.ts', () => {
       'QuestionIcon',
     );
   });
+
+  it('should format the notification color', () => {
+    expect(getNotificationTypeIconColor('closed')).toMatchSnapshot();
+    expect(getNotificationTypeIconColor('completed')).toMatchSnapshot();
+    expect(getNotificationTypeIconColor('draft')).toMatchSnapshot();
+    expect(getNotificationTypeIconColor('merged')).toMatchSnapshot();
+    expect(getNotificationTypeIconColor('not_planned')).toMatchSnapshot();
+    expect(getNotificationTypeIconColor('open')).toMatchSnapshot();
+    expect(getNotificationTypeIconColor('reopened')).toMatchSnapshot();
+    expect(formatReason('something_else_unknown' as Reason)).toMatchSnapshot();
+  });
 });
diff --git a/src/utils/helpers.test.ts b/src/utils/helpers.test.ts
index f9a60b45..ebaa908e 100644
--- a/src/utils/helpers.test.ts
+++ b/src/utils/helpers.test.ts
@@ -4,6 +4,7 @@ import {
   generateNotificationReferrerId,
   getCommentId,
   getLatestDiscussionCommentId,
+  isEnterpriseHost,
 } from './helpers';
 import {
   mockedSingleNotification,
@@ -23,6 +24,18 @@ const URL = {
 };
 
 describe('utils/helpers.ts', () => {
+  describe('isEnterpriseHost', () => {
+    it('should return true for enterprise host', () => {
+      expect(isEnterpriseHost('github.manos.im')).toBe(true);
+      expect(isEnterpriseHost('api.github.manos.im')).toBe(true);
+    });
+
+    it('should return false for non-enterprise host', () => {
+      expect(isEnterpriseHost('github.com')).toBe(false);
+      expect(isEnterpriseHost('api.github.com')).toBe(false);
+    });
+  });
+
   describe('generateNotificationReferrerId', () => {
     it('should generate the notification_referrer_id', () => {
       const referrerId = generateNotificationReferrerId(