-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ft(like): A user should be able to like or dislike an article (#36)
- A user provides sentiment on the article through likes and dislikes [Maintains #164798172]
- Loading branch information
1 parent
8a6bcac
commit 6deac09
Showing
27 changed files
with
558 additions
and
12,368 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ node_modules/ | |
coverage/ | ||
src/.DS_Store | ||
.env | ||
package-lock.json |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { notify } from 'react-notify-toast'; | ||
import axios from '../helpers/axiosInstance'; | ||
import { | ||
LIKE_AN_ARTICLE, DISLIKE_AN_ARTICLE, | ||
ERROR, ALREADY_LIKE_AN_ARTICLE, | ||
ALREADY_DISLIKE_AN_ARTICLE, | ||
} from './types'; | ||
import { getSingleArticle } from './articlesActions'; | ||
|
||
export const likeAnArticle = slug => dispatch => axios | ||
.post(`${process.env.baseURL}/articles/${slug}/like`, { | ||
likes: true, | ||
}) | ||
.then((response) => { | ||
if (response.data.msg !== 'You have already liked this article') { | ||
dispatch({ type: LIKE_AN_ARTICLE, payload: 1 }); | ||
notify.show('You have LIKED this Article', 'success', 2000); | ||
getSingleArticle(slug); | ||
} else { | ||
dispatch({ type: ALREADY_LIKE_AN_ARTICLE, payload: 0 }); | ||
notify.show('You have ALREADY LIKED this Article', 'error', 2000); | ||
} | ||
}) | ||
.catch(() => { | ||
dispatch({ | ||
type: ERROR, | ||
payload: 'Can not fetch your data, please login again', | ||
}); | ||
notify.show('You have been logged out, Please Login', 'error', 2000); | ||
}); | ||
|
||
export const dislikeAnArticle = slug => dispatch => axios | ||
.post(`${process.env.baseURL}/articles/${slug}/like`, { | ||
likes: false, | ||
}) | ||
.then((response) => { | ||
if (response.data.msg !== 'You have already disliked this article') { | ||
dispatch({ type: DISLIKE_AN_ARTICLE, payload: 1 }); | ||
notify.show('You have DISLIKED this Article', 'success', 2000); | ||
} else { | ||
dispatch({ type: ALREADY_DISLIKE_AN_ARTICLE, payload: 0 }); | ||
notify.show('You have ALREADY DISLIKED this Article', 'error', 2000); | ||
} | ||
}) | ||
.catch(() => { | ||
dispatch({ | ||
type: ERROR, | ||
payload: 'Can not fetch your data, please login again', | ||
}); | ||
notify.show('You have been logged out, Please Login', 'error', 2000); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
import configureMockStore from 'redux-mock-store'; | ||
import thunk from 'redux-thunk'; | ||
import moxios from 'moxios'; | ||
import instance from '../../helpers/axiosInstance'; | ||
import { likeAnArticle, dislikeAnArticle } from '../likeDislikeActions'; | ||
import { | ||
LIKE_AN_ARTICLE, DISLIKE_AN_ARTICLE, | ||
ALREADY_DISLIKE_AN_ARTICLE, | ||
ALREADY_LIKE_AN_ARTICLE, | ||
ERROR, | ||
} from '../types'; | ||
|
||
|
||
const middlewares = [thunk]; | ||
const mockStore = configureMockStore(middlewares); | ||
jest.mock('react-notify-toast'); | ||
|
||
const localStorageMock = { | ||
getItem: jest.fn(), | ||
setItem: jest.fn(), | ||
clear: jest.fn(), | ||
}; | ||
global.localStorage = localStorageMock; | ||
|
||
describe('like and dislike', () => { | ||
beforeEach(() => { | ||
moxios.install(instance); | ||
}); | ||
|
||
afterEach(() => { | ||
moxios.uninstall(instance); | ||
}); | ||
it('likes an article successfully', () => { | ||
const store = mockStore({}); | ||
moxios.wait(() => { | ||
const requestM = moxios.requests.mostRecent(); | ||
requestM.respondWith({ | ||
status: 200, | ||
response: { | ||
article: 'this is a test article', | ||
username: 'test_user', | ||
details: { | ||
likes: true, | ||
}, | ||
}, | ||
}); | ||
}); | ||
const expectedActions = [{ type: LIKE_AN_ARTICLE, payload: 1 }]; | ||
return store.dispatch(likeAnArticle('testSlug')).then(() => { | ||
expect(store.getActions()).toEqual(expectedActions); | ||
}); | ||
}); | ||
it('dislikes an article successfully', () => { | ||
const store = mockStore({}); | ||
moxios.wait(() => { | ||
const requestM = moxios.requests.mostRecent(); | ||
requestM.respondWith({ | ||
status: 200, | ||
response: { | ||
article: 'this is a test article', | ||
username: 'test_user', | ||
details: { | ||
likes: false, | ||
}, | ||
}, | ||
}); | ||
}); | ||
const expectedActions = [{ type: DISLIKE_AN_ARTICLE, payload: 1 }]; | ||
return store.dispatch(dislikeAnArticle('testSlug')).then(() => { | ||
expect(store.getActions()).toEqual(expectedActions); | ||
}); | ||
}); | ||
it('already dislike an article', () => { | ||
const store = mockStore({}); | ||
moxios.wait(() => { | ||
const requestM = moxios.requests.mostRecent(); | ||
requestM.respondWith({ | ||
status: 200, | ||
response: { | ||
msg: 'You have already disliked this article', | ||
}, | ||
}); | ||
}); | ||
const expectedActions = [{ type: ALREADY_DISLIKE_AN_ARTICLE, payload: 0 }]; | ||
return store.dispatch(dislikeAnArticle('testSlug')).then(() => { | ||
expect(store.getActions()).toEqual(expectedActions); | ||
}); | ||
}); | ||
it('already liked an article', () => { | ||
const store = mockStore({}); | ||
moxios.wait(() => { | ||
const requestM = moxios.requests.mostRecent(); | ||
requestM.respondWith({ | ||
status: 200, | ||
response: { | ||
msg: 'You have already liked this article', | ||
}, | ||
}); | ||
}); | ||
const expectedActions = [{ type: ALREADY_LIKE_AN_ARTICLE, payload: 0 }]; | ||
return store.dispatch(likeAnArticle('testSlug')).then(() => { | ||
expect(store.getActions()).toEqual(expectedActions); | ||
}); | ||
}); | ||
it('log in required, at liking article', () => { | ||
const store = mockStore({}); | ||
moxios.wait(() => { | ||
const requestM = moxios.requests.mostRecent(); | ||
requestM.respondWith({ | ||
status: 403, | ||
response: { | ||
}, | ||
}); | ||
}); | ||
const expectedActions = [{ | ||
type: ERROR, | ||
payload: 'Can not fetch your data, please login again', | ||
}]; | ||
return store.dispatch(likeAnArticle('testSlug')).then(() => { | ||
expect(store.getActions()).toEqual(expectedActions); | ||
}); | ||
}); | ||
it('log in required, at disliking article', () => { | ||
const store = mockStore({}); | ||
moxios.wait(() => { | ||
const requestM = moxios.requests.mostRecent(); | ||
requestM.respondWith({ | ||
status: 403, | ||
response: { | ||
}, | ||
}); | ||
}); | ||
const expectedActions = [{ | ||
type: ERROR, | ||
payload: 'Can not fetch your data, please login again', | ||
}]; | ||
return store.dispatch(dislikeAnArticle('testSlug')).then(() => { | ||
expect(store.getActions()).toEqual(expectedActions); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { | ||
LIKE_AN_ARTICLE, | ||
ALREADY_DISLIKE_AN_ARTICLE, | ||
DISLIKE_AN_ARTICLE, | ||
ALREADY_LIKE_AN_ARTICLE, | ||
} from '../types'; | ||
import likeDislikeReducer from '../../reducers/likeDislikeReducer'; | ||
|
||
describe('the like dislike reducer', () => { | ||
const initialState = { | ||
likes: null, | ||
}; | ||
it('should like an article', () => { | ||
const theDispatchedAction = { | ||
type: LIKE_AN_ARTICLE, | ||
}; | ||
const newState = { | ||
likes: 1, | ||
}; | ||
expect(likeDislikeReducer(initialState, | ||
theDispatchedAction)).toEqual(newState); | ||
}); | ||
it('should dislike an article', () => { | ||
const theDispatchedAction = { | ||
type: DISLIKE_AN_ARTICLE, | ||
}; | ||
const newState = { | ||
likes: 0, | ||
}; | ||
expect(likeDislikeReducer(initialState, | ||
theDispatchedAction)).toEqual(newState); | ||
}); | ||
it('should already dislike an article', () => { | ||
const theDispatchedAction = { | ||
type: ALREADY_DISLIKE_AN_ARTICLE, | ||
}; | ||
const newState = { | ||
likes: null, | ||
}; | ||
expect(likeDislikeReducer(initialState, | ||
theDispatchedAction)).toEqual(newState); | ||
}); | ||
it('should already liked an article', () => { | ||
const theDispatchedAction = { | ||
type: ALREADY_LIKE_AN_ARTICLE, | ||
}; | ||
const newState = { | ||
likes: null, | ||
}; | ||
expect(likeDislikeReducer(initialState, | ||
theDispatchedAction)).toEqual(newState); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.