Skip to content

#207 - Click on notification(1) goes straight to GitHub #213

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

Merged
merged 1 commit into from
Aug 26, 2016
Merged
Show file tree
Hide file tree
Changes from all 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
49 changes: 47 additions & 2 deletions src/js/__tests__/utils/notifications.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { expect } from 'chai';
import sinon from 'sinon';
import NotificationsUtils from '../../utils/notifications';
import Helpers from '../../utils/helpers';

const ipcRenderer = window.require('electron').ipcRenderer;
const shell = window.require('electron').shell;


describe('utils/notifications.js', () => {

beforeEach(function() {
ipcRenderer.send.reset();
shell.openExternal.reset();
sinon.spy(NotificationsUtils, 'raiseNativeNotification');
sinon.spy(NotificationsUtils, 'raiseSoundNotification');
});
Expand Down Expand Up @@ -160,12 +164,13 @@ describe('utils/notifications.js', () => {

});

it('should click on a native notification', () => {
it('should click on a native notification (with 1 notification)', () => {

const notification = {
subject: {
title: 'Hello. This is a notification',
type: 'Issue'
type: 'Issue',
url: 'https://api.github.com/repos/ekonstantinidis/notifications-test/issues/3'
},
repository: {
full_name: 'ekonstantinidis/gitify'
Expand All @@ -177,6 +182,46 @@ describe('utils/notifications.js', () => {

const nativeNotification = NotificationsUtils.raiseNativeNotification([notification]);
nativeNotification.onclick();

const newUrl = Helpers.generateGitHubUrl(notification.subject.url);
expect(shell.openExternal).to.have.been.calledOnce;
expect(shell.openExternal).to.have.been.calledWith(newUrl);

// Put the spy back
sinon.spy(NotificationsUtils, 'raiseNativeNotification');
});

it('should click on a native notification (with more than 1 notification)', () => {

const notifications = [
{
subject: {
title: 'Hello. This is a notification',
type: 'Issue',
url: 'https://api.github.com/repos/ekonstantinidis/notifications-test/issues/3'
},
repository: {
full_name: 'ekonstantinidis/gitify'
}
},
{
subject: {
title: 'Hello. This is another notification',
type: 'Issue',
url: 'https://api.github.com/repos/ekonstantinidis/notifications-test/issues/3'
},
repository: {
full_name: 'ekonstantinidis/gitify'
}
},
];

// Restore functionality so we can test further
NotificationsUtils.raiseNativeNotification.restore();

const nativeNotification = NotificationsUtils.raiseNativeNotification(notifications);
nativeNotification.onclick();

expect(ipcRenderer.send).to.have.been.calledOnce;
expect(ipcRenderer.send).to.have.been.calledWith('reopen-window');

Expand Down
7 changes: 3 additions & 4 deletions src/js/components/notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import React from 'react';
import { connect } from 'react-redux';

import { markNotification } from '../actions';
import Helpers from '../utils/helpers';


export class SingleNotification extends React.Component {

Expand All @@ -16,10 +18,7 @@ export class SingleNotification extends React.Component {
}

openBrowser() {
var url = this.props.notification.subject.url.replace('api.github.com/repos', 'www.github.com');
if (url.indexOf('/pulls/') !== -1) {
url = url.replace('/pulls/', '/pull/');
}
var url = Helpers.generateGitHubUrl(this.props.notification.subject.url);
shell.openExternal(url);
}

Expand Down
8 changes: 8 additions & 0 deletions src/js/utils/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,13 @@ export default {
} else {
ipcRenderer.send('update-icon', 'TrayIdle');
}
},

generateGitHubUrl(url) {
var newUrl = url.replace('api.github.com/repos', 'www.github.com');
if (newUrl.indexOf('/pulls/') !== -1) {
newUrl = newUrl.replace('/pulls/', '/pull/');
}
return newUrl;
}
};
10 changes: 9 additions & 1 deletion src/js/utils/notifications.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
const ipcRenderer = window.require('electron').ipcRenderer;
const shell = window.require('electron').shell;

import Helpers from '../utils/helpers';

export default {
setup(notifications, settings) {
Expand Down Expand Up @@ -46,7 +49,12 @@ export default {
});

nativeNotification.onclick = function () {
ipcRenderer.send('reopen-window');
if (newCount === 1) {
var url = Helpers.generateGitHubUrl(notifications[0].subject.url);
shell.openExternal(url);
} else {
ipcRenderer.send('reopen-window');
}
};

return nativeNotification;
Expand Down