Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: discussions-url #538

Merged
merged 9 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,4 @@ temp/

dist/
build/
.vscode/settings.json
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
"react-transition-group": "^4.4.1",
"tailwindcss": "^2.0.2",
"ts-loader": "^8.0.12",
"typescript": "^4.1.3"
"typescript": "^4.6.2"
},
"devDependencies": {
"@testing-library/react": "^11.2.2",
Expand Down
28 changes: 18 additions & 10 deletions src/__mocks__/electron.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,26 @@ window.localStorage = {

window.alert = jest.fn();

const browserWindow = {
loadURL: jest.fn(),
webContents: {
let instance;

class BrowserWindow {
constructor() {
if(!instance){
instance = this;
}
return instance;
}
loadURL = jest.fn();
webContents = {
on: () => {},
session: {
clearStorageData: jest.fn(),
},
},
on: () => {},
close: jest.fn(),
hide: jest.fn(),
destroy: jest.fn(),
};
on(){};
close = jest.fn();
hide = jest.fn();
destroy = jest.fn();
};

const dialog = {
Expand All @@ -47,7 +55,7 @@ const dialog = {

module.exports = {
remote: {
BrowserWindow: () => browserWindow,
BrowserWindow: BrowserWindow,
dialog: dialog,
process: {
platform: 'darwin',
Expand All @@ -57,7 +65,7 @@ module.exports = {
getLoginItemSettings: jest.fn(),
setLoginItemSettings: () => {},
},
getCurrentWindow: jest.fn(() => browserWindow),
getCurrentWindow: jest.fn(() => instance || new BrowserWindow),
},
ipcRenderer: {
send: jest.fn(),
Expand Down
115 changes: 114 additions & 1 deletion src/__mocks__/mockedData.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AccountNotifications, EnterpriseAccount } from '../types';
import { Notification, Repository, User } from '../typesGithub';
import { Notification, Repository, User, GraphQLSearch } from '../typesGithub';

export const mockedEnterpriseAccounts: EnterpriseAccount[] = [
{
Expand Down Expand Up @@ -274,3 +274,116 @@ export const mockedSingleAccountNotifications: AccountNotifications[] = [
notifications: [mockedSingleNotification],
},
];

export const mockedGraphQLResponse: GraphQLSearch = {
"data": {
"data": {
"search": {
"edges": [
{
"node": {
"viewerSubscription": "SUBSCRIBED",
"title": "1.16.0",
"url": "https://github.com/manosim/notifications-test/discussions/612",
"comments": {
"edges": [
{
"node": {
"databaseId": 2215656,
"createdAt": "2022-02-20T18:33:39Z",
"replies": {
"edges": []
}
}
},
{
"node": {
"databaseId": 2217789,
"createdAt": "2022-02-21T03:30:42Z",
"replies": {
"edges": []
}
}
},
{
"node": {
"databaseId": 2223243,
"createdAt": "2022-02-21T18:26:27Z",
"replies": {
"edges": [
{
"node": {
"databaseId": 2232922,
"createdAt": "2022-02-23T00:57:58Z"
}
}
]
}
}
},
{
"node": {
"databaseId": 2232921,
"createdAt": "2022-02-23T00:57:49Z",
"replies": {
"edges": []
}
}
},
{
"node": {
"databaseId": 2258799,
"createdAt": "2022-02-27T01:22:20Z",
"replies": {
"edges": [
{
"node": {
"databaseId": 2300902,
"createdAt": "2022-03-05T17:43:52Z"
}
}
]
}
}
},
{
"node": {
"databaseId": 2297637,
"createdAt": "2022-03-04T20:39:44Z",
"replies": {
"edges": [
{
"node": {
"databaseId": 2300893,
"createdAt": "2022-03-05T17:41:04Z"
}
}
]
}
}
},
{
"node": {
"databaseId": 2299763,
"createdAt": "2022-03-05T11:05:42Z",
"replies": {
"edges": [
{
"node": {
"databaseId": 2300895,
"createdAt": "2022-03-05T17:41:44Z"
}
}
]
}
}
}
]
}
}
}
]
}
}
}
}
24 changes: 8 additions & 16 deletions src/components/NotificationRow.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
const { shell } = require('electron');

import React, { useCallback, useContext } from 'react';
import { formatDistanceToNow, parseISO } from 'date-fns';
import { CheckIcon, MuteIcon } from '@primer/octicons-react';

import { formatReason, getNotificationTypeIcon } from '../utils/github-api';
import { generateGitHubWebUrl } from '../utils/helpers';
import { openInBrowser } from '../utils/helpers';
import { Notification } from '../typesGithub';
import { AppContext } from '../context/App';

Expand All @@ -18,8 +16,12 @@ export const NotificationRow: React.FC<IProps> = ({
notification,
hostname,
}) => {
const { settings, accounts } = useContext(AppContext);
const { markNotification, unsubscribeNotification } = useContext(AppContext);
const {
settings,
accounts,
markNotification,
unsubscribeNotification,
} = useContext(AppContext);

const pressTitle = useCallback(() => {
openBrowser();
Expand All @@ -29,17 +31,7 @@ export const NotificationRow: React.FC<IProps> = ({
}
}, [settings]);

const openBrowser = useCallback(() => {
// Some Notification types from GitHub are missing urls in their subjects.
if (notification.subject.url) {
const url = generateGitHubWebUrl(
notification.subject.url,
notification.id,
accounts.user?.id
);
shell.openExternal(url);
}
}, [notification]);
const openBrowser = useCallback(() => openInBrowser(notification, accounts), [notification]);

const unsubscribe = (event: React.MouseEvent<HTMLElement>) => {
// Don't trigger onClick of parent element.
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useNotifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export const useNotifications = (): NotificationsState => {
notifications,
data,
settings,
accounts.user
accounts
);
setNotifications(data);
setIsFetching(false);
Expand Down
2 changes: 1 addition & 1 deletion src/routes/__snapshots__/LoginWithToken.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ exports[`routes/LoginWithToken.js renders correctly 1`] = `
<span
className="underline font-extrabold text-yellow-500"
>
read:user, notifications
read:user, notifications, repo

</span>
scopes.
Expand Down
43 changes: 43 additions & 0 deletions src/typesGithub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ export type SubjectType =
| 'Release'
| 'RepositoryVulnerabilityAlert';

export type ViewerSubscription =
| 'IGNORED'
| 'SUBSCRIBED'
| 'UNSUBSCRIBED'

export interface Notification {
id: string;
unread: boolean;
Expand Down Expand Up @@ -115,3 +120,41 @@ export interface Subject {
latest_comment_url?: string;
type: SubjectType;
}

export interface GraphQLSearch {
data: {
data: {
search: {
edges: DiscussionEdge[]
}
}
}
}

export interface DiscussionEdge {
node: {
viewerSubscription: ViewerSubscription;
title: string;
url: string;
comments: {
edges: DiscussionCommentEdge[]
}
}
}

export interface DiscussionCommentEdge {
node: {
databaseId: string|number;
createdAt: string;
replies: {
edges: DiscussionSubcommentEdge[]
}
}
}

export interface DiscussionSubcommentEdge {
node: {
databaseId: string|number;
createdAt: string;
}
}
16 changes: 8 additions & 8 deletions src/utils/auth.test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { AxiosPromise, AxiosResponse } from 'axios';

const { remote } = require('electron');
const BrowserWindow = remote.BrowserWindow;
afonsojramos marked this conversation as resolved.
Show resolved Hide resolved
import { remote } from 'electron';
const browserWindow = new remote.BrowserWindow

import * as auth from './auth';
import * as apiRequests from './api-requests';
import { AuthState } from '../types';

describe('utils/auth.tsx', () => {
describe('authGitHub', () => {
const loadURLMock = jest.spyOn(new BrowserWindow(), 'loadURL');
const loadURLMock = jest.spyOn(browserWindow, 'loadURL');

beforeEach(() => {
loadURLMock.mockReset();
});

it('should call authGithub - success', async () => {
spyOn(new BrowserWindow().webContents, 'on').and.callFake(
spyOn(browserWindow.webContents, 'on').and.callFake(
(event, callback) => {
if (event === 'will-redirect') {
const event = new Event('will-redirect');
Expand All @@ -30,19 +30,19 @@ describe('utils/auth.tsx', () => {
expect(res.authCode).toBe('123-456');

expect(
new BrowserWindow().webContents.session.clearStorageData
browserWindow.webContents.session.clearStorageData
).toHaveBeenCalledTimes(1);

expect(loadURLMock).toHaveBeenCalledTimes(1);
expect(loadURLMock).toHaveBeenCalledWith(
'https://github.com/login/oauth/authorize?client_id=FAKE_CLIENT_ID_123&scope=read:user,notifications'
'https://github.com/login/oauth/authorize?client_id=FAKE_CLIENT_ID_123&scope=read:user,notifications,repo'
);

expect(new BrowserWindow().destroy).toHaveBeenCalledTimes(1);
expect(browserWindow.destroy).toHaveBeenCalledTimes(1);
});

it('should call authGithub - failure', async () => {
spyOn(new BrowserWindow().webContents, 'on').and.callFake(
spyOn(browserWindow.webContents, 'on').and.callFake(
(event, callback) => {
if (event === 'will-redirect') {
const event = new Event('will-redirect');
Expand Down
2 changes: 1 addition & 1 deletion src/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export const Constants = {
// GitHub OAuth
AUTH_SCOPE: ['read:user', 'notifications'],
AUTH_SCOPE: ['read:user', 'notifications', 'repo'],

DEFAULT_AUTH_OPTIONS: {
hostname: 'github.com',
Expand Down
Loading