Skip to content

Commit

Permalink
refactor: extract feature based utils (#1672)
Browse files Browse the repository at this point in the history
Signed-off-by: Adam Setch <adam.setch@outlook.com>
  • Loading branch information
setchy authored Dec 12, 2024
1 parent e9c5293 commit 60c0ced
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 142 deletions.
6 changes: 2 additions & 4 deletions src/renderer/components/NotificationRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ import { AppContext } from '../context/App';
import { Opacity, Size } from '../types';
import type { Notification } from '../typesGitHub';
import { cn } from '../utils/cn';
import {
formatForDisplay,
isMarkAsDoneFeatureSupported,
} from '../utils/helpers';
import { isMarkAsDoneFeatureSupported } from '../utils/features';
import { formatForDisplay } from '../utils/helpers';
import {
getNotificationTypeIcon,
getNotificationTypeIconColor,
Expand Down
6 changes: 2 additions & 4 deletions src/renderer/components/RepositoryNotifications.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import { AppContext } from '../context/App';
import { Opacity, Size } from '../types';
import type { Notification } from '../typesGitHub';
import { cn } from '../utils/cn';
import {
getChevronDetails,
isMarkAsDoneFeatureSupported,
} from '../utils/helpers';
import { isMarkAsDoneFeatureSupported } from '../utils/features';
import { getChevronDetails } from '../utils/helpers';
import { openRepository } from '../utils/links';
import { HoverGroup } from './HoverGroup';
import { NotificationRow } from './NotificationRow';
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/hooks/useNotifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
markNotificationThreadAsDone,
markNotificationThreadAsRead,
} from '../utils/api/client';
import { isMarkAsDoneFeatureSupported } from '../utils/helpers';
import { isMarkAsDoneFeatureSupported } from '../utils/features';
import {
getAllNotifications,
setTrayIconColor,
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/utils/api/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import type {
Release,
UserDetails,
} from '../../typesGitHub';
import { isAnsweredDiscussionFeatureSupported } from '../helpers';
import { isAnsweredDiscussionFeatureSupported } from '../features';
import { QUERY_SEARCH_DISCUSSIONS } from './graphql/discussions';
import { formatAsGitHubSearchSyntax } from './graphql/utils';
import { apiRequestAuth } from './request';
Expand Down
97 changes: 97 additions & 0 deletions src/renderer/utils/features.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import {
mockGitHubCloudAccount,
mockGitHubEnterpriseServerAccount,
} from '../__mocks__/state-mocks';

import {
isAnsweredDiscussionFeatureSupported,
isMarkAsDoneFeatureSupported,
} from './features';

describe('renderer/utils/features.ts', () => {
describe('isMarkAsDoneFeatureSupported', () => {
it('should return true for GitHub Cloud', () => {
expect(isMarkAsDoneFeatureSupported(mockGitHubCloudAccount)).toBe(true);
});

it('should return false for GitHub Enterprise Server < v3.13', () => {
const account = {
...mockGitHubEnterpriseServerAccount,
version: '3.12.0',
};

expect(isMarkAsDoneFeatureSupported(account)).toBe(false);
});

it('should return true for GitHub Enterprise Server >= v3.13', () => {
const account = {
...mockGitHubEnterpriseServerAccount,
version: '3.13.3',
};

expect(isMarkAsDoneFeatureSupported(account)).toBe(true);
});

it('should return false for GitHub Enterprise Server when partial version available', () => {
const account = {
...mockGitHubEnterpriseServerAccount,
version: '3',
};

expect(isMarkAsDoneFeatureSupported(account)).toBe(false);
});

it('should return false for GitHub Enterprise Server when no version available', () => {
const account = {
...mockGitHubEnterpriseServerAccount,
version: null,
};

expect(isMarkAsDoneFeatureSupported(account)).toBe(false);
});
});

describe('isAnsweredDiscussionFeatureSupported', () => {
it('should return true for GitHub Cloud', () => {
expect(isAnsweredDiscussionFeatureSupported(mockGitHubCloudAccount)).toBe(
true,
);
});

it('should return false for GitHub Enterprise Server < v3.12', () => {
const account = {
...mockGitHubEnterpriseServerAccount,
version: '3.11.0',
};

expect(isAnsweredDiscussionFeatureSupported(account)).toBe(false);
});

it('should return true for GitHub Enterprise Server >= v3.12', () => {
const account = {
...mockGitHubEnterpriseServerAccount,
version: '3.12.3',
};

expect(isAnsweredDiscussionFeatureSupported(account)).toBe(true);
});

it('should return false for GitHub Enterprise Server when partial version available', () => {
const account = {
...mockGitHubEnterpriseServerAccount,
version: '3',
};

expect(isAnsweredDiscussionFeatureSupported(account)).toBe(false);
});

it('should return false for GitHub Enterprise Server when no version available', () => {
const account = {
...mockGitHubEnterpriseServerAccount,
version: null,
};

expect(isMarkAsDoneFeatureSupported(account)).toBe(false);
});
});
});
41 changes: 41 additions & 0 deletions src/renderer/utils/features.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import type { Account } from '../types';
import { isEnterpriseServerHost } from './helpers';

/**
* Check if the "Mark as done" feature is supported for the given account.
*
* GitHub Cloud or GitHub Enterprise Server 3.13 or newer is required to support this feature.
*/

export function isMarkAsDoneFeatureSupported(account: Account): boolean {
if (isEnterpriseServerHost(account.hostname)) {
if (account.version) {
const version = account?.version.split('.').map(Number);
return version[0] >= 3 && version[1] >= 13;
}

return false;
}

return true;
}
/**
* Check if the "answered" discussions are supported for the given account.
*
* GitHub Cloud or GitHub Enterprise Server 3.12 or newer is required to support this feature.
*/

export function isAnsweredDiscussionFeatureSupported(
account: Account,
): boolean {
if (isEnterpriseServerHost(account.hostname)) {
if (account.version) {
const version = account?.version.split('.').map(Number);
return version[0] >= 3 && version[1] >= 12;
}

return false;
}

return true;
}
95 changes: 2 additions & 93 deletions src/renderer/utils/helpers.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import type { AxiosPromise, AxiosResponse } from 'axios';
import {
mockGitHubCloudAccount,
mockGitHubEnterpriseServerAccount,
mockPersonalAccessTokenAccount,
} from '../__mocks__/state-mocks';
import { mockPersonalAccessTokenAccount } from '../__mocks__/state-mocks';

import {
ChevronDownIcon,
Expand All @@ -19,6 +15,7 @@ import {
mockSingleNotification,
} from './api/__mocks__/response-mocks';
import * as apiRequests from './api/request';

import {
formatForDisplay,
formatNotificationUpdatedAt,
Expand All @@ -27,9 +24,7 @@ import {
getChevronDetails,
getFilterCount,
getPlatformFromHostname,
isAnsweredDiscussionFeatureSupported,
isEnterpriseServerHost,
isMarkAsDoneFeatureSupported,
} from './helpers';

describe('renderer/utils/helpers.ts', () => {
Expand Down Expand Up @@ -69,92 +64,6 @@ describe('renderer/utils/helpers.ts', () => {
});
});

describe('isMarkAsDoneFeatureSupported', () => {
it('should return true for GitHub Cloud', () => {
expect(isMarkAsDoneFeatureSupported(mockGitHubCloudAccount)).toBe(true);
});

it('should return false for GitHub Enterprise Server < v3.13', () => {
const account = {
...mockGitHubEnterpriseServerAccount,
version: '3.12.0',
};

expect(isMarkAsDoneFeatureSupported(account)).toBe(false);
});

it('should return true for GitHub Enterprise Server >= v3.13', () => {
const account = {
...mockGitHubEnterpriseServerAccount,
version: '3.13.3',
};

expect(isMarkAsDoneFeatureSupported(account)).toBe(true);
});

it('should return false for GitHub Enterprise Server when partial version available', () => {
const account = {
...mockGitHubEnterpriseServerAccount,
version: '3',
};

expect(isMarkAsDoneFeatureSupported(account)).toBe(false);
});

it('should return false for GitHub Enterprise Server when no version available', () => {
const account = {
...mockGitHubEnterpriseServerAccount,
version: null,
};

expect(isMarkAsDoneFeatureSupported(account)).toBe(false);
});
});

describe('isAnsweredDiscussionFeatureSupported', () => {
it('should return true for GitHub Cloud', () => {
expect(isAnsweredDiscussionFeatureSupported(mockGitHubCloudAccount)).toBe(
true,
);
});

it('should return false for GitHub Enterprise Server < v3.12', () => {
const account = {
...mockGitHubEnterpriseServerAccount,
version: '3.11.0',
};

expect(isAnsweredDiscussionFeatureSupported(account)).toBe(false);
});

it('should return true for GitHub Enterprise Server >= v3.12', () => {
const account = {
...mockGitHubEnterpriseServerAccount,
version: '3.12.3',
};

expect(isAnsweredDiscussionFeatureSupported(account)).toBe(true);
});

it('should return false for GitHub Enterprise Server when partial version available', () => {
const account = {
...mockGitHubEnterpriseServerAccount,
version: '3',
};

expect(isAnsweredDiscussionFeatureSupported(account)).toBe(false);
});

it('should return false for GitHub Enterprise Server when no version available', () => {
const account = {
...mockGitHubEnterpriseServerAccount,
version: null,
};

expect(isMarkAsDoneFeatureSupported(account)).toBe(false);
});
});

describe('generateNotificationReferrerId', () => {
it('should generate the notification_referrer_id', () => {
const referrerId = generateNotificationReferrerId(mockSingleNotification);
Expand Down
40 changes: 1 addition & 39 deletions src/renderer/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
import { formatDistanceToNowStrict, parseISO } from 'date-fns';
import log from 'electron-log';
import { defaultSettings } from '../context/App';
import type { Account, Chevron, Hostname, Link, SettingsState } from '../types';
import type { Chevron, Hostname, Link, SettingsState } from '../types';
import type { Notification } from '../typesGitHub';
import { getHtmlUrl, getLatestDiscussion } from './api/client';
import type { PlatformType } from './auth/types';
Expand All @@ -27,44 +27,6 @@ export function isEnterpriseServerHost(hostname: Hostname): boolean {
return !hostname.endsWith(Constants.DEFAULT_AUTH_OPTIONS.hostname);
}

/**
* Check if the "Mark as done" feature is supported for the given account.
*
* GitHub Cloud or GitHub Enterprise Server 3.13 or newer is required to support this feature.
*/
export function isMarkAsDoneFeatureSupported(account: Account): boolean {
if (isEnterpriseServerHost(account.hostname)) {
if (account.version) {
const version = account?.version.split('.').map(Number);
return version[0] >= 3 && version[1] >= 13;
}

return false;
}

return true;
}

/**
* Check if the "answered" discussions are supported for the given account.
*
* GitHub Cloud or GitHub Enterprise Server 3.12 or newer is required to support this feature.
*/
export function isAnsweredDiscussionFeatureSupported(
account: Account,
): boolean {
if (isEnterpriseServerHost(account.hostname)) {
if (account.version) {
const version = account?.version.split('.').map(Number);
return version[0] >= 3 && version[1] >= 12;
}

return false;
}

return true;
}

export function generateNotificationReferrerId(
notification: Notification,
): string {
Expand Down

0 comments on commit 60c0ced

Please sign in to comment.