diff --git a/src/js/__tests__/actions/index.js b/src/js/__tests__/actions/index.js index 33b206b7c..d76a2543b 100644 --- a/src/js/__tests__/actions/index.js +++ b/src/js/__tests__/actions/index.js @@ -1,7 +1,9 @@ import { expect } from 'chai'; import nock from 'nock'; -import { apiMiddleware } from 'redux-api-middleware'; +import { apiMiddleware, ApiError } from 'redux-api-middleware'; import configureMockStore from 'redux-mock-store'; + +import Constants from '../../utils/constants'; import * as actions from '../../actions'; const middlewares = [ apiMiddleware ]; @@ -186,7 +188,6 @@ describe('actions/index.js', () => { }); - it('should mark a repository\'s notifications as read with success', () => { const loginId = 'ekonstantinidis'; const repoId = 'gitify'; @@ -249,6 +250,44 @@ describe('actions/index.js', () => { }); + it('should check if the user has starred the repository', () => { + nock('https://api.github.com/') + .get(`/user/starred/${Constants.REPO_SLUG}`) + .reply(200); + + const expectedActions = [ + { type: actions.HAS_STARRED_REQUEST, payload: undefined, meta: undefined }, + { type: actions.HAS_STARRED_SUCCESS, payload: undefined, meta: undefined } + ]; + + const store = createMockStore({ response: [] }, expectedActions); + + return store.dispatch(actions.checkHasStarred()) + .then(() => { // return of async actions + expect(store.getActions()).to.eql(expectedActions); + }); + + }); + + it('should check if the user has starred the repository', () => { + nock('https://api.github.com/') + .get(`/user/starred/${Constants.REPO_SLUG}`) + .reply(404); + + const apiError = new ApiError(404, 'Not Found', undefined); + const expectedActions = [ + { type: actions.HAS_STARRED_REQUEST, payload: undefined, meta: undefined }, + { type: actions.HAS_STARRED_FAILURE, payload: apiError, error: true, meta: undefined } + ]; + + const store = createMockStore({ response: [] }, expectedActions); + + return store.dispatch(actions.checkHasStarred()) + .then(() => { // return of async actions + expect(store.getActions()).to.eql(expectedActions); + }); + + }); it('should search the notifications with a query', () => { diff --git a/src/js/__tests__/reducers/settings.js b/src/js/__tests__/reducers/settings.js index 4d143554f..300931e9f 100644 --- a/src/js/__tests__/reducers/settings.js +++ b/src/js/__tests__/reducers/settings.js @@ -1,6 +1,6 @@ import { expect } from 'chai'; import reducer from '../../reducers/settings'; -import { UPDATE_SETTING } from '../../actions'; +import { UPDATE_SETTING, HAS_STARRED_SUCCESS, HAS_STARRED_FAILURE } from '../../actions'; describe('reducers/settings.js', () => { const initialState = { @@ -8,7 +8,8 @@ describe('reducers/settings.js', () => { playSound: true, showNotifications: true, markOnClick: false, - openAtStartup: false + openAtStartup: false, + hasStarred: false }; it('should return the initial state', () => { @@ -42,4 +43,31 @@ describe('reducers/settings.js', () => { }); }); + + it('should handle HAS_STARRED_SUCCESS', () => { + + const actionParticipating = { + type: HAS_STARRED_SUCCESS + }; + + expect(reducer(undefined, actionParticipating)).to.eql({ + ...initialState, + hasStarred: true + }); + + }); + + it('should handle HAS_STARRED_SUCCESS', () => { + + const actionParticipating = { + type: HAS_STARRED_FAILURE + }; + + expect(reducer(undefined, actionParticipating)).to.eql({ + ...initialState, + hasStarred: false + }); + + }); + }); diff --git a/src/js/actions/index.js b/src/js/actions/index.js index c221fc66b..855c17ed2 100644 --- a/src/js/actions/index.js +++ b/src/js/actions/index.js @@ -115,6 +115,27 @@ export function markRepoNotifications(loginId, repoId, repoFullName) { }; +// Starred + +export const HAS_STARRED_REQUEST = 'HAS_STARRED_REQUEST'; +export const HAS_STARRED_SUCCESS = 'HAS_STARRED_SUCCESS'; +export const HAS_STARRED_FAILURE = 'HAS_STARRED_FAILURE'; +export function checkHasStarred() { + return { + [CALL_API]: { + endpoint: `https://api.github.com/user/starred/${Constants.REPO_SLUG}`, + method: 'GET', + headers: { + 'Accept': 'application/json', + 'Cache-Control': 'no-cache', + 'Content-Type': 'application/json' + }, + types: [HAS_STARRED_REQUEST, HAS_STARRED_SUCCESS, HAS_STARRED_FAILURE] + } + }; +}; + + // Search export const SEARCH_NOTIFICATIONS = 'SEARCH_NOTIFICATIONS'; diff --git a/src/js/components/login.js b/src/js/components/login.js index 09d4f9765..3d87b75e5 100644 --- a/src/js/components/login.js +++ b/src/js/components/login.js @@ -75,7 +75,7 @@ export class LoginPage extends React.Component { return (