-
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.
- rate rendered individual articles - set the initial rating to the one from the backend [Delivers #161966573]
- Loading branch information
1 parent
05ba569
commit a70da43
Showing
12 changed files
with
711 additions
and
374 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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,94 @@ | ||
import thunk from 'redux-thunk'; | ||
import expect from 'expect'; | ||
import configureMockStore from 'redux-mock-store'; | ||
import moxios from 'moxios'; | ||
import * as actions from '../ratingActions'; | ||
import * as types from '../actionTypes'; | ||
|
||
const middlewares = [thunk]; | ||
const mockStore = configureMockStore(middlewares); | ||
|
||
describe('Rating Actions', () => { | ||
describe('Successful Rating', () => { | ||
it('should create a request action.', () => { | ||
const ratingData = { | ||
request: { | ||
slug: '', | ||
rating: '', | ||
}, | ||
}; | ||
const expectedAction = { | ||
type: types.RATE_ARTICLE, | ||
payload: ratingData, | ||
}; | ||
expect(actions.rate(ratingData)).toEqual(expectedAction); | ||
}); | ||
|
||
it('should rate successfuly action.', () => { | ||
const message = 'rated'; | ||
const expectedAction = { | ||
type: types.RATE_ARTICLE_SUCCESS, | ||
rating: message, | ||
}; | ||
expect(actions.ratingSuccess(message)).toEqual(expectedAction); | ||
}); | ||
|
||
it('should create action for failed rating.', () => { | ||
const message = 'Failed to rate'; | ||
const expectedAction = { | ||
type: types.RATE_ARTICLE_FAIL, | ||
error: message, | ||
}; | ||
expect(actions.ratingFail(message)).toEqual(expectedAction); | ||
}); | ||
}); | ||
}); | ||
|
||
// Mock data | ||
const ratingData = { | ||
slug: 'hello', | ||
rating: { rating: 2 }, | ||
}; | ||
|
||
describe('Rating actions', () => { | ||
beforeEach(() => moxios.install()); | ||
afterEach(() => moxios.uninstall()); | ||
|
||
it('successful async action', () => { | ||
const message = 'Successfully rated article'; | ||
moxios.wait(() => { | ||
const request = moxios.requests.mostRecent(); | ||
request.respondWith({ | ||
status: 201, | ||
response: message, | ||
}); | ||
}); | ||
const expectedActions = [ | ||
{ type: types.RATE_ARTICLE, payload: ratingData }, | ||
{ type: types.RATE_ARTICLE_SUCCESS, rating: message }, | ||
]; | ||
const store = mockStore(message); | ||
return store.dispatch(actions.rateArticle(ratingData)).then(() => { | ||
expect(store.getActions()).toEqual(expectedActions); | ||
}); | ||
}); | ||
|
||
it('failing async action', () => { | ||
const message = 'error'; | ||
moxios.wait(() => { | ||
const request = moxios.requests.mostRecent(); | ||
request.respondWith({ | ||
status: 403, | ||
response: message, | ||
}); | ||
}); | ||
const expectedActions = [ | ||
{ type: types.RATE_ARTICLE, payload: ratingData }, | ||
{ type: types.RATE_ARTICLE_FAIL, error: message }, | ||
]; | ||
const store = mockStore(message); | ||
return store.dispatch(actions.rateArticle(ratingData)).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
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,41 @@ | ||
import axios from 'axios'; | ||
import Cookies from 'js-cookie'; | ||
import * as types from './actionTypes'; | ||
|
||
export const rate = payload => ( | ||
{ | ||
type: types.RATE_ARTICLE, | ||
payload, | ||
} | ||
); | ||
|
||
export const ratingSuccess = rating => ( | ||
{ | ||
type: types.RATE_ARTICLE_SUCCESS, | ||
rating, | ||
} | ||
); | ||
|
||
export const ratingFail = error => ( | ||
{ | ||
type: types.RATE_ARTICLE_FAIL, | ||
error, | ||
} | ||
); | ||
|
||
const token = Cookies.get('access_token'); | ||
|
||
export const rateArticle = payload => (dispatch) => { | ||
const { slug, rating } = payload; | ||
dispatch(rate(payload)); | ||
return axios.post(`https://ah-technocrats.herokuapp.com/api/articles/${slug}/rate/`, rating, { | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Authorization: `Token ${token}`, | ||
}, | ||
}) | ||
.then(res => dispatch(ratingSuccess(res.data))) | ||
.catch((error) => { | ||
dispatch(ratingFail(error.response.data)); | ||
}); | ||
}; |
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.