-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(like-dislike):users can like/dislike articles (#20)
- creat like and dislike action dispensers - add like and dislike button images to styles folder - add like and dislike buttons to the article view page - format getArticlesreducer to also get likes and dislikes of an article - write tests for the like/dislike functionality [Finishes #164798260]
- Loading branch information
1 parent
cc0a5ad
commit 51cadd7
Showing
11 changed files
with
439 additions
and
19 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 |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import axios from "axios"; | ||
import { | ||
LIKE_ARTICLE_FAILURE, | ||
LIKE_ARTICLE_SUCCESS, | ||
DISLIKE_ARTICLE_FAILURE, | ||
DISLIKE_ARTICLE_SUCCESS | ||
} from "./types"; | ||
|
||
export const likeArticle = slug => { | ||
return async dispatch => { | ||
const body = {}; | ||
const headers = { | ||
Authorization: `Bearer ${sessionStorage.getItem("token")}`, | ||
"Content-type": "application/json" | ||
}; | ||
return await axios | ||
.post( | ||
`https://ah-backend-prime-staging.herokuapp.com/api/v1/articles/${slug}/like/`, | ||
body, | ||
{ headers: headers } | ||
) | ||
.then(data => { | ||
if (data.data.author) { | ||
dispatch({ | ||
type: LIKE_ARTICLE_SUCCESS, | ||
payload: data.data | ||
}); | ||
} else { | ||
dispatch({ | ||
type: LIKE_ARTICLE_FAILURE, | ||
payload: "Failed to like article" | ||
}); | ||
} | ||
}); | ||
}; | ||
}; | ||
|
||
export const dislikeArticle = slug => { | ||
return async dispatch => { | ||
const body = {}; | ||
const headers = { | ||
Authorization: `Bearer ${sessionStorage.getItem("token")}`, | ||
"Content-type": "application/json" | ||
}; | ||
return await axios | ||
.post( | ||
`https://ah-backend-prime-staging.herokuapp.com/api/v1/articles/${slug}/dislike/`, | ||
body, | ||
{ headers: headers } | ||
) | ||
.then(data => { | ||
if (data.data.author) { | ||
dispatch({ | ||
type: DISLIKE_ARTICLE_SUCCESS, | ||
payload: data.data | ||
}); | ||
} else { | ||
dispatch({ | ||
type: DISLIKE_ARTICLE_FAILURE, | ||
payload: "Failed to dislike article" | ||
}); | ||
} | ||
}); | ||
}; | ||
}; |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,156 @@ | ||
import configureMockStore from "redux-mock-store"; | ||
import thunk from "redux-thunk"; | ||
import moxios from "moxios"; | ||
import { likeArticle, dislikeArticle } from "../../actions/LikeDislikeActions"; | ||
|
||
import { | ||
LIKE_ARTICLE_SUCCESS, | ||
DISLIKE_ARTICLE_SUCCESS, | ||
LIKE_ARTICLE_FAILURE, | ||
DISLIKE_ARTICLE_FAILURE | ||
} from "../../actions/types"; | ||
|
||
const middlewares = [thunk]; | ||
const mockStore = configureMockStore(middlewares); | ||
|
||
describe("LikeDislike action", () => { | ||
beforeEach(function() { | ||
moxios.install(); | ||
}); | ||
afterEach(function() { | ||
moxios.uninstall(); | ||
}); | ||
it("should call the like action", () => { | ||
const store = mockStore({}); | ||
moxios.wait(() => { | ||
const requestM = moxios.requests.mostRecent(); | ||
requestM.respondWith({ | ||
status: 200, | ||
response: { | ||
id: 64, | ||
author: { | ||
username: "patritsfitz", | ||
bio: | ||
"This is my favorite song, I just dunno the words, but I still mess with you, you just ain't never heard. It goes like drop that thing. The day you don't drop that thing" | ||
}, | ||
title: "kuhcf", | ||
average_rating: 0, | ||
slug: "kuhcf-0x3f", | ||
likes: 1, | ||
dislikes: 0 | ||
} | ||
}); | ||
}); | ||
const expectedActions = [ | ||
{ | ||
type: LIKE_ARTICLE_SUCCESS, | ||
payload: { | ||
id: 64, | ||
author: { | ||
username: "patritsfitz", | ||
bio: | ||
"This is my favorite song, I just dunno the words, but I still mess with you, you just ain't never heard. It goes like drop that thing. The day you don't drop that thing" | ||
}, | ||
title: "kuhcf", | ||
average_rating: 0, | ||
slug: "kuhcf-0x3f", | ||
likes: 1, | ||
dislikes: 0 | ||
} | ||
} | ||
]; | ||
const slug = "kuhcf-0x3f"; | ||
return store.dispatch(likeArticle(slug)).then(() => { | ||
expect(store.getActions()).toEqual(expectedActions); | ||
}); | ||
}); | ||
|
||
it("should call the dislike action", () => { | ||
const store = mockStore({}); | ||
moxios.wait(() => { | ||
const requestM = moxios.requests.mostRecent(); | ||
requestM.respondWith({ | ||
status: 200, | ||
response: { | ||
id: 64, | ||
author: { | ||
username: "patritsfitz", | ||
bio: | ||
"This is my favorite song, I just dunno the words, but I still mess with you, you just ain't never heard. It goes like drop that thing. The day you don't drop that thing" | ||
}, | ||
title: "kuhcf", | ||
average_rating: 0, | ||
slug: "kuhcf-0x3f", | ||
likes: 1, | ||
dislikes: 0 | ||
} | ||
}); | ||
}); | ||
const expectedActions = [ | ||
{ | ||
type: DISLIKE_ARTICLE_SUCCESS, | ||
payload: { | ||
id: 64, | ||
author: { | ||
username: "patritsfitz", | ||
bio: | ||
"This is my favorite song, I just dunno the words, but I still mess with you, you just ain't never heard. It goes like drop that thing. The day you don't drop that thing" | ||
}, | ||
title: "kuhcf", | ||
average_rating: 0, | ||
slug: "kuhcf-0x3f", | ||
likes: 1, | ||
dislikes: 0 | ||
} | ||
} | ||
]; | ||
const slug = "kuhcf-0x3f"; | ||
return store.dispatch(dislikeArticle(slug)).then(() => { | ||
expect(store.getActions()).toEqual(expectedActions); | ||
}); | ||
}); | ||
|
||
it("should fail to call the dislike action", () => { | ||
const store = mockStore({}); | ||
moxios.wait(() => { | ||
const requestM = moxios.requests.mostRecent(); | ||
requestM.respondWith({ | ||
response: { | ||
error: "Failed to dislike article" | ||
} | ||
}); | ||
}); | ||
const expectedActions = [ | ||
{ | ||
type: DISLIKE_ARTICLE_FAILURE, | ||
payload: "Failed to dislike article" | ||
} | ||
]; | ||
const slug = "kuhcf-0x3f"; | ||
return store.dispatch(dislikeArticle(slug)).then(() => { | ||
expect(store.getActions()).toEqual(expectedActions); | ||
}); | ||
}); | ||
|
||
it("should fail to call the like action", () => { | ||
const store = mockStore({}); | ||
moxios.wait(() => { | ||
const requestM = moxios.requests.mostRecent(); | ||
requestM.respondWith({ | ||
response: { | ||
error: "Failed to like article" | ||
} | ||
}); | ||
}); | ||
const expectedActions = [ | ||
{ | ||
type: LIKE_ARTICLE_FAILURE, | ||
payload: "Failed to like article" | ||
} | ||
]; | ||
const slug = "kuhcf-0x3f"; | ||
return store.dispatch(likeArticle(slug)).then(() => { | ||
expect(store.getActions()).toEqual(expectedActions); | ||
}); | ||
}); | ||
}); |
3 changes: 3 additions & 0 deletions
3
src/tests/components/__snapshots__/singleArticle.test.js.snap
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,3 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Component for getting single Articles should receive props 1`] = `ShallowWrapper {}`; |
Oops, something went wrong.