From b98201afa3269c1bae428367effd03a33f4d6b08 Mon Sep 17 00:00:00 2001 From: David Jay Date: Wed, 14 Dec 2016 19:03:07 -0500 Subject: [PATCH] Adding and testing function to fetch assets in talk reducer. --- client/coral-admin/src/actions/assets.js | 0 client/coral-admin/src/constants/comments.js | 2 +- client/coral-admin/src/reducers/comments.js | 2 +- .../coral-admin/src/services/talk-adapter.js | 34 ++++++--- .../coral-admin/services/talk-adapter.js | 75 +++++++++++++++++++ .../{itemActions.spec.js => itemActions.js} | 0 .../{itemReducer.spec.js => itemReducer.js} | 0 7 files changed, 100 insertions(+), 13 deletions(-) create mode 100644 client/coral-admin/src/actions/assets.js create mode 100644 tests/client/coral-admin/services/talk-adapter.js rename tests/client/coral-framework/store/{itemActions.spec.js => itemActions.js} (100%) rename tests/client/coral-framework/store/{itemReducer.spec.js => itemReducer.js} (100%) diff --git a/client/coral-admin/src/actions/assets.js b/client/coral-admin/src/actions/assets.js new file mode 100644 index 000000000..e69de29bb diff --git a/client/coral-admin/src/constants/comments.js b/client/coral-admin/src/constants/comments.js index 856f619d0..16d72e889 100644 --- a/client/coral-admin/src/constants/comments.js +++ b/client/coral-admin/src/constants/comments.js @@ -1,3 +1,3 @@ export const SHOW_BANUSER_DIALOG = 'SHOW_BANUSER_DIALOG'; export const HIDE_BANUSER_DIALOG = 'HIDE_BANUSER_DIALOG'; -export const USER_BAN_SUCESS = 'USER_BAN_SUCESS'; +export const USER_BAN_SUCCESS = 'USER_BAN_SUCCESS'; diff --git a/client/coral-admin/src/reducers/comments.js b/client/coral-admin/src/reducers/comments.js index dca726b5e..d5fd85ab6 100644 --- a/client/coral-admin/src/reducers/comments.js +++ b/client/coral-admin/src/reducers/comments.js @@ -32,7 +32,7 @@ export default (state = initialState, action) => { case 'COMMENT_STREAM_FETCH_SUCCESS': return replaceComments(action, state); case actions.SHOW_BANUSER_DIALOG: return setBanUser(state, true, action); case actions.HIDE_BANUSER_DIALOG: return setBanUser(state, false, action); - case actions.USER_BAN_SUCESS: return setBanUser(state, false, action); + case actions.USER_BAN_SUCCESS: return setBanUser(state, false, action); default: return state; } }; diff --git a/client/coral-admin/src/services/talk-adapter.js b/client/coral-admin/src/services/talk-adapter.js index ab542b4a0..6dc4d5827 100644 --- a/client/coral-admin/src/services/talk-adapter.js +++ b/client/coral-admin/src/services/talk-adapter.js @@ -11,22 +11,20 @@ import coralApi from '../../../coral-framework/helpers/response'; // Intercept redux actions and act over the ones we are interested export default store => next => action => { + next(action); + switch (action.type) { case 'COMMENTS_MODERATION_QUEUE_FETCH': - fetchModerationQueueComments(store); - break; + return fetchModerationQueueComments(store); case 'COMMENT_UPDATE': - updateComment(store, action.comment); - break; + return updateComment(store, action.comment); case 'COMMENT_CREATE': - createComment(store, action.name, action.body); - break; + return createComment(store, action.name, action.body); case 'USER_BAN': - userStatusUpdate(store, action.status, action.userId, action.commentId); - break; + return userStatusUpdate(store, action.status, action.userId, action.commentId); + case 'ASSETS_FETCH': + return fetchAssets(store, action); } - - next(action); }; // Get comments to fill each of the three lists on the mod queue @@ -88,6 +86,20 @@ const createComment = (store, name, comment) => { // Ban a user const userStatusUpdate = (store, status, userId, commentId) => { return coralApi(`/users/${userId}/status`, {method: 'POST', body: {status: status, comment_id: commentId}}) - .then(res => store.dispatch({type: 'USER_BAN_SUCESS', res})) + .then(res => store.dispatch({type: 'USER_BAN_SUCCESS', res})) .catch(error => store.dispatch({type: 'USER_BAN_FAILED', error})); }; + +// Fetch a page of assets +// Get comments to fill each of the three lists on the mod queue +const fetchAssets = (store, action) => { + const {skip, limit, search} = action; + return coralApi(`/assets?skip=${skip}&limit=${limit}&search=${search}`) + .then(({result, count}) => + /* Post comments and users to redux store. Actions will be posted when they are needed. */ + store.dispatch({type: 'ASSETS_FETCH_SUCCESS', + assets: result, + count + })) + .catch(error => store.dispatch({type: 'ASSETS_FETCH_FAILED', error})); +}; diff --git a/tests/client/coral-admin/services/talk-adapter.js b/tests/client/coral-admin/services/talk-adapter.js new file mode 100644 index 000000000..27ae8efcb --- /dev/null +++ b/tests/client/coral-admin/services/talk-adapter.js @@ -0,0 +1,75 @@ +import 'react'; +import 'redux'; +import {expect} from 'chai'; +import fetchMock from 'fetch-mock'; +import adapter from '../../../../client/coral-admin/src/services/talk-adapter'; +import {Map} from 'immutable'; + +import configureStore from 'redux-mock-store'; + +const mockStore = configureStore(); + +describe('talk-adapter.js', () => { + let store; + + beforeEach(() => { + store = mockStore(new Map({})); + fetchMock.restore(); + }); + + describe('ASSETS_FETCH', () => { + + const assets = [ + { + url: 'http://test.com', + id: '123', + status: 'closed' + }, + { + url: 'http://test.org', + id: '456', + status: 'open' + } + ]; + + it('should fetch a list of assets', () => { + + const action = { + type: 'ASSETS_FETCH', + skip: 2, + limit: 20, + search: '' + }; + + fetchMock.get('*', JSON.stringify({ + result: assets, + count: 2 + })); + + return adapter(store)(()=>{})(action) + .then(() => { + expect(store.getActions()[0]).to.have.property('type', 'ASSETS_FETCH_SUCCESS'); + expect(store.getActions()[0]).to.have.property('count', 2); + expect(store.getActions()[0]).to.have.property('assets'). + and.to.deep.equal(assets); + }); + }); + + it('should return an error appropriatly', () => { + + const action = { + type: 'ASSETS_FETCH', + skip: 2, + limit: 20, + search: '' + }; + + fetchMock.get('*', 404); + + return adapter(store)(()=>{})(action) + .then(() => { + expect(store.getActions()[0]).to.have.property('type', 'ASSETS_FETCH_FAILED'); + }); + }); + }); +}); diff --git a/tests/client/coral-framework/store/itemActions.spec.js b/tests/client/coral-framework/store/itemActions.js similarity index 100% rename from tests/client/coral-framework/store/itemActions.spec.js rename to tests/client/coral-framework/store/itemActions.js diff --git a/tests/client/coral-framework/store/itemReducer.spec.js b/tests/client/coral-framework/store/itemReducer.js similarity index 100% rename from tests/client/coral-framework/store/itemReducer.spec.js rename to tests/client/coral-framework/store/itemReducer.js