From eb00e63a4180e683f73686546ed0027a1f382d6b Mon Sep 17 00:00:00 2001 From: David Jay Date: Tue, 8 Nov 2016 15:21:52 -0500 Subject: [PATCH 01/13] Adding action summaries function to actions model. --- models/action.js | 32 +++++++++++++++++++++++++++++++- tests/models/action.js | 24 ++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/models/action.js b/models/action.js index 27c63747a..043699988 100644 --- a/models/action.js +++ b/models/action.js @@ -28,7 +28,7 @@ ActionSchema.statics.findById = function(id) { }; /** - * Finds users in an array of ids. + * Finds actions in an array of ids. * @param {String} ids array of user identifiers (uuid) */ ActionSchema.statics.findByItemIdArray = function(item_ids) { @@ -37,6 +37,36 @@ ActionSchema.statics.findByItemIdArray = function(item_ids) { }); }; +/** + * Returns summaries of actions for an array of ids + * @param {String} ids array of user identifiers (uuid) +*/ +ActionSchema.statics.getActionSummaries = function(item_ids) { + return ActionSchema.statics.findByItemIdArray(item_ids).then((rawActions) => { + // Create an object with a count of each action type for each item + const actionSummaries = rawActions.reduce((actionObj, action) => { + if (!actionObj[action.item_id]) { + actionObj[action.item_id] = { + type: action.action_type, + count: 1, + current_user: false //Corrent this later when we have authentication + }; + } else { + actionObj[action.item_id].count ++; + } + return actionObj; + }, {}); + + // Return an array extracted from the actionSummaries object + return Object.keys(actionSummaries).reduce((actions, key) => { + let actionSummary = actionSummaries[key]; + actionSummary.item_id = key; + actions.push(actionSummary); + return actions; + }, []); + }); +}; + const Action = mongoose.model('Action', ActionSchema); module.exports = Action; diff --git a/tests/models/action.js b/tests/models/action.js index 34a9f2dca..95d8dc29b 100644 --- a/tests/models/action.js +++ b/tests/models/action.js @@ -15,6 +15,9 @@ describe('Action: models', () => { }, { action_type: 'flag', item_id: '456' + }, { + action_type: 'flag', + item_id: '123' }]).then((actions) => { mockActions = actions; }); @@ -32,7 +35,28 @@ describe('Action: models', () => { describe('#findByItemIdArray()', () => { it('should find an array of actions from an array of item_ids', () => { return Action.findByItemIdArray(['123', '456']).then((result) => { + expect(result).to.have.length(3); + }); + }); + }); + + describe('#getActionSummaries()', () => { + it('should return properly formatted summaries from an array of item_ids', () => { + return Action.getActionSummaries(['123', '789']).then((result) => { expect(result).to.have.length(2); + const sorted = result.sort((a, b) => a.count - b.count); + expect(sorted[0]).to.deep.equal({ + type: 'like', + count: 1, + item_id: '789', + current_user: false + }); + expect(sorted[1]).to.deep.equal({ + type: 'flag', + count: 2, + item_id: '123', + current_user: false + }); }); }); }); From d3b8184ff43f376197deb8c9fc49b3f64ca8d93d Mon Sep 17 00:00:00 2001 From: David Jay Date: Tue, 8 Nov 2016 15:22:38 -0500 Subject: [PATCH 02/13] Removing randomness from comments api tests. --- tests/routes/api/comments/index.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/routes/api/comments/index.js b/tests/routes/api/comments/index.js index 4fbe52e94..b31fb522a 100644 --- a/tests/routes/api/comments/index.js +++ b/tests/routes/api/comments/index.js @@ -147,10 +147,11 @@ describe('Get /:comment_id', () => { .get('/api/v1/comments') .query({'comment_id': 'abc'}) .end(function(err, res){ + const sorted = res.body.sort((a, b) => a.body - b.body); expect(err).to.be.null; expect(res).to.have.status(200); - expect(res.body[0]).to.have.property('body'); - expect(res.body[0].body).to.equal('comment 10'); + expect(sorted[0]).to.have.property('body') + .and.to.equal('comment 10'); done(); }); }); From fcd65c18fd323a561a3a1044b1dfeeef12f24f9f Mon Sep 17 00:00:00 2001 From: David Jay Date: Tue, 8 Nov 2016 15:23:04 -0500 Subject: [PATCH 03/13] Switching stream to use action summaries function. --- routes/api/stream/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routes/api/stream/index.js b/routes/api/stream/index.js index 67216a252..8a51fc75d 100644 --- a/routes/api/stream/index.js +++ b/routes/api/stream/index.js @@ -14,7 +14,7 @@ router.get('/', (req, res, next) => { return Promise.all([ comments, User.findByIdArray(comments.map((comment) => comment.author_id)), - Action.findByItemIdArray(comments.map((comment) => comment.id)) + Action.getActionSummaries(comments.map((comment) => comment.id)) ]); }).then(([comments, users, actions]) => { res.json([...comments, ...users, ...actions]); From e8584b499b2f7281509bdf0d21b4daa12e4dd198 Mon Sep 17 00:00:00 2001 From: David Jay Date: Tue, 8 Nov 2016 15:32:52 -0500 Subject: [PATCH 04/13] Returning actions, items, and comments as seperate arrays. --- models/action.js | 3 ++- routes/api/stream/index.js | 6 +++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/models/action.js b/models/action.js index 043699988..cc310c5a8 100644 --- a/models/action.js +++ b/models/action.js @@ -47,9 +47,10 @@ ActionSchema.statics.getActionSummaries = function(item_ids) { const actionSummaries = rawActions.reduce((actionObj, action) => { if (!actionObj[action.item_id]) { actionObj[action.item_id] = { + id: action.id, type: action.action_type, count: 1, - current_user: false //Corrent this later when we have authentication + current_user: false //Update this later when we have authentication }; } else { actionObj[action.item_id].count ++; diff --git a/routes/api/stream/index.js b/routes/api/stream/index.js index 8a51fc75d..8908b27eb 100644 --- a/routes/api/stream/index.js +++ b/routes/api/stream/index.js @@ -17,7 +17,11 @@ router.get('/', (req, res, next) => { Action.getActionSummaries(comments.map((comment) => comment.id)) ]); }).then(([comments, users, actions]) => { - res.json([...comments, ...users, ...actions]); + res.json({ + comments, + users, + actions + }); }).catch(error => { next(error); }); From d7ead01180bf70887734a36dda621d0718dbd356 Mon Sep 17 00:00:00 2001 From: David Jay Date: Tue, 8 Nov 2016 17:53:38 -0500 Subject: [PATCH 05/13] Expecting stream endpoint to return items broken out by type. --- client/coral-framework/store/actions/items.js | 13 ++++++++++--- tests/models/action.js | 2 ++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/client/coral-framework/store/actions/items.js b/client/coral-framework/store/actions/items.js index 8b30e2548..956b70350 100644 --- a/client/coral-framework/store/actions/items.js +++ b/client/coral-framework/store/actions/items.js @@ -89,12 +89,19 @@ export function getStream (assetId) { ) .then((json) => { + /* Add items to the store */ + const itemTypes = Object.keys(json); + for (let i=0; i < itemTypes.length; i++ ) { + for (var j=0; j < json[itemTypes[i]].length; j++ ) { + dispatch(addItem(json[itemTypes[i]][j])); + } + } + /* Sort comments by date*/ let rootComments = [] let childComments = {} - json.sort((a,b) => new Date(b.created_at).getTime() - new Date(a.created_at).getTime()) - json.reduce((prev, item) => { - dispatch(addItem(item)) + json.comments.sort((a,b) => new Date(b.created_at).getTime() - new Date(a.created_at).getTime()) + json.comments.reduce((prev, item) => { /* Check for root and child comments. */ if ( diff --git a/tests/models/action.js b/tests/models/action.js index 95d8dc29b..e3ead7047 100644 --- a/tests/models/action.js +++ b/tests/models/action.js @@ -45,6 +45,8 @@ describe('Action: models', () => { return Action.getActionSummaries(['123', '789']).then((result) => { expect(result).to.have.length(2); const sorted = result.sort((a, b) => a.count - b.count); + delete sorted[0].id; + delete sorted[1].id; expect(sorted[0]).to.deep.equal({ type: 'like', count: 1, From 6616cf0c51df21ffc9e99f97ee7d67f9522e40c6 Mon Sep 17 00:00:00 2001 From: David Jay Date: Wed, 9 Nov 2016 13:42:03 -0500 Subject: [PATCH 06/13] Uncommenting flags and hydrating actions. --- .../coral-embed-stream/src/CommentStream.js | 28 ++++++++----------- client/coral-framework/store/actions/items.js | 12 ++++++-- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/client/coral-embed-stream/src/CommentStream.js b/client/coral-embed-stream/src/CommentStream.js index cda7ffd57..65e02e2ee 100644 --- a/client/coral-embed-stream/src/CommentStream.js +++ b/client/coral-embed-stream/src/CommentStream.js @@ -123,14 +123,12 @@ class CommentStream extends Component {
- { - // - } + @@ -153,14 +151,12 @@ class CommentStream extends Component {
- { - // - } + diff --git a/client/coral-framework/store/actions/items.js b/client/coral-framework/store/actions/items.js index 956b70350..c292426d1 100644 --- a/client/coral-framework/store/actions/items.js +++ b/client/coral-framework/store/actions/items.js @@ -121,9 +121,15 @@ export function getStream (assetId) { comments: rootComments })) - const keys = Object.keys(childComments) - for (var i=0; i < keys.length; i++ ) { - dispatch(updateItem(keys[i], 'children', childComments[keys[i]].reverse())) + const childKeys = Object.keys(childComments) + for (var i=0; i < childKeys.length; i++ ) { + dispatch(updateItem(childKeys[i], 'children', childComments[childKeys[i]].reverse())) + } + + /* Hydrate actions on comments */ + const actions = Object.keys(json.actions) + for (var i=0; i < actions.length; i++ ) { + dispatch(updateItem(actions[i].item_id, actions[i].type, actions[i].id)) } return (json) From 13aabb99744f0eaf33a6bc145864a800307f2e62 Mon Sep 17 00:00:00 2001 From: David Jay Date: Wed, 9 Nov 2016 13:54:44 -0500 Subject: [PATCH 07/13] Re-adding notification length to config. --- client/coral-embed-stream/src/CommentStream.js | 12 ++++++------ client/coral-framework/store/actions/config.js | 4 +++- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/client/coral-embed-stream/src/CommentStream.js b/client/coral-embed-stream/src/CommentStream.js index 65e02e2ee..0804530e8 100644 --- a/client/coral-embed-stream/src/CommentStream.js +++ b/client/coral-embed-stream/src/CommentStream.js @@ -151,12 +151,12 @@ class CommentStream extends Component {
- + diff --git a/client/coral-framework/store/actions/config.js b/client/coral-framework/store/actions/config.js index 243cb9049..c5368a04b 100644 --- a/client/coral-framework/store/actions/config.js +++ b/client/coral-framework/store/actions/config.js @@ -21,7 +21,9 @@ export const fetchConfig = () => async (dispatch) => { //TODO: Replace with fetching config from backend // const response = await fetch(`./talk.config.json`) // const json = await response.json() - dispatch({ type: FETCH_CONFIG_SUCCESS, config: fromJS({}) }) + dispatch({ type: FETCH_CONFIG_SUCCESS, config: fromJS({ + notifLength: 4500 + }) }) } catch (error) { dispatch({ type: FETCH_CONFIG_FAILED }) } From e0e80aa5cdf865820903cd6de4d60fd4832fc947 Mon Sep 17 00:00:00 2001 From: David Jay Date: Wed, 9 Nov 2016 14:10:01 -0500 Subject: [PATCH 08/13] Moving coral-framework tests over and updating mocha to transpile them. --- package.json | 6 ++++-- .../client/coral-framework}/store/authReducer.js | 4 ++-- .../client/coral-framework}/store/itemActions.spec.js | 2 +- .../client/coral-framework}/store/itemReducer.spec.js | 4 ++-- .../coral-framework}/store/notificationReducer.spec.js | 4 ++-- 5 files changed, 11 insertions(+), 9 deletions(-) rename {client/coral-framework/__tests__ => tests/client/coral-framework}/store/authReducer.js (82%) rename {client/coral-framework/__tests__ => tests/client/coral-framework}/store/itemActions.spec.js (98%) rename {client/coral-framework/__tests__ => tests/client/coral-framework}/store/itemReducer.spec.js (95%) rename {client/coral-framework/__tests__ => tests/client/coral-framework}/store/notificationReducer.spec.js (83%) diff --git a/package.json b/package.json index 979475b5b..94fb7d475 100644 --- a/package.json +++ b/package.json @@ -9,8 +9,8 @@ "build-watch": "webpack --config webpack.config.dev.js --watch", "lint": "eslint .", "pretest": "npm install", - "test": "mocha tests --recursive", - "test-watch": "mocha tests --recursive -w", + "test": "mocha --compilers js:babel-core/register --recursive tests", + "test-watch": "mocha --compilers js:babel-core/register --recursive -w tests", "embed-start": "npm run build && ./bin/www" }, "config": { @@ -69,6 +69,7 @@ "css-loader": "^0.25.0", "eslint": "^3.9.1", "exports-loader": "^0.6.3", + "fetch-mock": "^5.5.0", "hammerjs": "^2.0.8", "immutable": "^3.8.1", "imports-loader": "^0.6.5", @@ -89,6 +90,7 @@ "react-redux": "^4.4.5", "react-router": "^3.0.0", "redux": "^3.6.0", + "redux-mock-store": "^1.2.1", "redux-thunk": "^2.1.0", "regenerator": "^0.8.46", "style-loader": "^0.13.1", diff --git a/client/coral-framework/__tests__/store/authReducer.js b/tests/client/coral-framework/store/authReducer.js similarity index 82% rename from client/coral-framework/__tests__/store/authReducer.js rename to tests/client/coral-framework/store/authReducer.js index dd9f58501..3b2148f7e 100644 --- a/client/coral-framework/__tests__/store/authReducer.js +++ b/tests/client/coral-framework/store/authReducer.js @@ -1,7 +1,7 @@ import { Map } from 'immutable' import {expect} from 'chai' -import authReducer from '../../store/reducers/auth' -import * as actions from '../../store/actions/auth' +import authReducer from '../../../../client/coral-framework/store/reducers/auth' +import * as actions from '../../../../client/coral-framework/store/actions/auth' describe ('authReducer', () => { describe('SET_LOGGED_IN_USER', () => { diff --git a/client/coral-framework/__tests__/store/itemActions.spec.js b/tests/client/coral-framework/store/itemActions.spec.js similarity index 98% rename from client/coral-framework/__tests__/store/itemActions.spec.js rename to tests/client/coral-framework/store/itemActions.spec.js index 948aee32d..124174f36 100644 --- a/client/coral-framework/__tests__/store/itemActions.spec.js +++ b/tests/client/coral-framework/store/itemActions.spec.js @@ -2,7 +2,7 @@ import 'react' import 'redux' import {expect} from 'chai' import fetchMock from 'fetch-mock' -import * as actions from '../../store/actions/items' +import * as actions from '../../../../client/coral-framework/store/actions/items' import {Map} from 'immutable' import configureStore from 'redux-mock-store' diff --git a/client/coral-framework/__tests__/store/itemReducer.spec.js b/tests/client/coral-framework/store/itemReducer.spec.js similarity index 95% rename from client/coral-framework/__tests__/store/itemReducer.spec.js rename to tests/client/coral-framework/store/itemReducer.spec.js index 8a664ac8c..bd7b74b6c 100644 --- a/client/coral-framework/__tests__/store/itemReducer.spec.js +++ b/tests/client/coral-framework/store/itemReducer.spec.js @@ -1,7 +1,7 @@ import { Map, fromJS } from 'immutable' import {expect} from 'chai' -import itemsReducer from '../../store/reducers/items' -import * as actions from '../../store/actions/items' +import itemsReducer from '../../../../client/coral-framework/store/reducers/items' +import * as actions from '../../../../client/coral-framework/store/actions/items' describe ('itemsReducer', () => { describe('ADD_ITEM', () => { diff --git a/client/coral-framework/__tests__/store/notificationReducer.spec.js b/tests/client/coral-framework/store/notificationReducer.spec.js similarity index 83% rename from client/coral-framework/__tests__/store/notificationReducer.spec.js rename to tests/client/coral-framework/store/notificationReducer.spec.js index 4f37034a6..ee30b43bd 100644 --- a/client/coral-framework/__tests__/store/notificationReducer.spec.js +++ b/tests/client/coral-framework/store/notificationReducer.spec.js @@ -1,7 +1,7 @@ import { Map } from 'immutable' import {expect} from 'chai' -import notificationReducer from '../../store/reducers/notification' -import * as actions from '../../store/actions/notification' +import notificationReducer from '../../../../client/coral-framework/store/reducers/notification' +import * as actions from '../../../../client/coral-framework/store/actions/notification' describe ('notificationsReducer', () => { describe('ADD_NOTIFICATION', () => { From 6ab561b91be8183a5d68461d5ff4bf27fbd89d2a Mon Sep 17 00:00:00 2001 From: David Jay Date: Wed, 9 Nov 2016 16:39:54 -0500 Subject: [PATCH 09/13] Updating and passing coral-framework tests --- client/coral-framework/store/actions/items.js | 39 +++--- .../coral-framework/store/reducers/items.js | 21 ++- .../coral-framework/store/itemActions.spec.js | 128 +++++++++++------- .../coral-framework/store/itemReducer.spec.js | 115 +++++----------- 4 files changed, 137 insertions(+), 166 deletions(-) diff --git a/client/coral-framework/store/actions/items.js b/client/coral-framework/store/actions/items.js index c292426d1..e51e1573e 100644 --- a/client/coral-framework/store/actions/items.js +++ b/client/coral-framework/store/actions/items.js @@ -24,13 +24,14 @@ export const APPEND_ITEM_ARRAY = 'APPEND_ITEM_ARRAY' * */ -export const addItem = (item) => { +export const addItem = (item, item_type) => { if (!item.id) { console.warn('addItem called without an item id.') } return { type: ADD_ITEM, - item: item, + item, + item_type, id: item.id } } @@ -47,22 +48,24 @@ export const addItem = (item) => { */ -export const updateItem = (id, property, value) => { +export const updateItem = (id, property, value, item_type) => { return { type: UPDATE_ITEM, id, property, - value + value, + item_type } } -export const appendItemArray = (id, property, value, addToFront) => { +export const appendItemArray = (id, property, value, add_to_front, item_type) => { return { type: APPEND_ITEM_ARRAY, id, property, value, - addToFront + add_to_front, + item_type } } @@ -93,7 +96,7 @@ export function getStream (assetId) { const itemTypes = Object.keys(json); for (let i=0; i < itemTypes.length; i++ ) { for (var j=0; j < json[itemTypes[i]].length; j++ ) { - dispatch(addItem(json[itemTypes[i]][j])); + dispatch(addItem(json[itemTypes[i]][j], itemTypes[i])); } } @@ -118,18 +121,18 @@ export function getStream (assetId) { dispatch(addItem({ id: assetId, - comments: rootComments - })) + comments: rootComments, + }, 'assets')) const childKeys = Object.keys(childComments) for (var i=0; i < childKeys.length; i++ ) { - dispatch(updateItem(childKeys[i], 'children', childComments[childKeys[i]].reverse())) + dispatch(updateItem(childKeys[i], 'children', childComments[childKeys[i]].reverse(), 'comments')) } /* Hydrate actions on comments */ const actions = Object.keys(json.actions) for (var i=0; i < actions.length; i++ ) { - dispatch(updateItem(actions[i].item_id, actions[i].type, actions[i].id)) + dispatch(updateItem(actions[i].item_id, actions[i].type, actions[i].id, 'actions')) } return (json) @@ -195,7 +198,6 @@ export function postItem (item, type, id) { 'Content-Type':'application/json' } } - console.log('postItem', options); return fetch('/api/v1/' + type, options) .then( response => { @@ -204,7 +206,7 @@ export function postItem (item, type, id) { } ) .then((json) => { - dispatch(addItem({...item, id:json.id})) + dispatch(addItem({...item, id:json.id}, type)) return json.id }) } @@ -227,7 +229,7 @@ export function postItem (item, type, id) { * */ -export function postAction (id, type, user_id) { +export function postAction (item_id, type, user_id) { return (dispatch) => { const action = { type, @@ -238,13 +240,14 @@ export function postAction (id, type, user_id) { body: JSON.stringify(action) } - dispatch(appendItemArray(id, type, user_id)) - return fetch('/api/v1/comments/' + id + '/actions', options) + return fetch('/api/v1/comments/' + item_id + '/actions', options) .then( response => { - return response.ok ? response.text() + return response.ok ? response.json() : Promise.reject(response.status + ' ' + response.statusText) } - ) + ).then((json)=>{ + return json.id + }) } } diff --git a/client/coral-framework/store/reducers/items.js b/client/coral-framework/store/reducers/items.js index d3eb4d3af..d1fcd6b43 100644 --- a/client/coral-framework/store/reducers/items.js +++ b/client/coral-framework/store/reducers/items.js @@ -3,26 +3,23 @@ import { Map, fromJS } from 'immutable' import * as actions from '../actions/items' -const initialState = fromJS({}) +const initialState = Map({}); export default (state = initialState, action) => { switch (action.type) { case actions.ADD_ITEM: - return state.set(action.id, fromJS(action.item)) + return state.setIn([action.item_type, action.id], fromJS(action.item)); case actions.UPDATE_ITEM: - return state.updateIn([action.id, action.property], () => - fromJS(action.value) - ) + return state.setIn([action.item_type, action.id, action.property], fromJS(action.value)); case actions.APPEND_ITEM_ARRAY: - return state.updateIn([action.id, action.property], (prop) => { - if (action.addToFront) { - return prop ? prop.unshift(action.value) : fromJS([action.value]) + return state.updateIn([action.item_type, action.id, action.property], (prop) => { + console.log(prop); + if (action.add_to_front) { + return prop ? prop.unshift(fromJS(action.value)) : fromJS([action.value]); } else { - return prop ? prop.push(action.value) : fromJS([action.value]) + return prop ? prop.push(fromJS(action.value)) : fromJS([action.value]); } - - } - ) + }); default: return state } diff --git a/tests/client/coral-framework/store/itemActions.spec.js b/tests/client/coral-framework/store/itemActions.spec.js index 124174f36..360f6b79a 100644 --- a/tests/client/coral-framework/store/itemActions.spec.js +++ b/tests/client/coral-framework/store/itemActions.spec.js @@ -11,74 +11,88 @@ const mockStore = configureStore() describe('itemActions', () => { let store - const host = 'http://test.host' beforeEach(() => { store = mockStore(new Map({})) fetchMock.restore() }) - describe('getItemsQuery', () => { - const query = 'all' - const rootId = '1234' - const view = 'testView' - const response = {results: [ - {Docs: [ - {type: 'comment', data: {content: 'stuff'}, item_id: '123'}, - {type: 'comment', data: {content: 'morestuff'}, item_id: '456'} - ]} - ]} + describe('getStream', () => { + const rootId = '1234'; + const response = { + comments: [ + { body: 'stuff', id: '123'}, + { body: 'morestuff', id: '456'} + ], + actions: [ + { + type: 'like', + id: '123', + count: 1, + current_user: false + }, + { + type: 'flag', + id: '456', + count: 5, + current_user: true + } + ] + }; - it('should get an item from a query and send the appropriate dispatches', () => { - fetchMock.get('*', JSON.stringify(response)) - return actions.getItemsQuery(query, rootId, view, host)(store.dispatch) + it('should get an stream from an asset_id and send the appropriate dispatches', () => { + fetchMock.get('*', JSON.stringify(response)); + return actions.getStream(rootId)(store.dispatch) .then((res) => { - expect(fetchMock.calls().matched[0][0]).to.equal('http://test.host/v1/exec/all/view/testView/1234') - expect(res).to.deep.equal(response.results[0].Docs) + expect(fetchMock.calls().matched[0][0]).to.equal('/api/v1/stream?asset_id=1234') + expect(res).to.deep.equal(response); expect(store.getActions()[0]).to.deep.equal({ type: actions.ADD_ITEM, - item: response.results[0].Docs[0], - item_id: '123' - }) + item: response.comments[0], + item_type: 'comments', + id: '123' + }); expect(store.getActions()[1]).to.deep.equal({ type: actions.ADD_ITEM, - item: response.results[0].Docs[1], - item_id: '456' - }) - }) + item: response.comments[1], + item_type: 'comments', + id: '456' + }); + }); }) it('should handle an error', () => { fetchMock.get('*', 404) - return actions.getItemsQuery(query, rootId, view, host)(store.dispatch) + return actions.getStream(rootId)(store.dispatch) .catch((err) => { - expect(err).to.be.truthy - }) - }) - }) + expect(err).to.be.truthy; + }); + }); + }); - describe('getItemsArray', () => { - const response = {items: [{type: 'comment', item_id: '123'}, {type: 'comment', item_id: '456'}]} + //Disabling tests for this function until is is used again. + xdescribe('getItemsArray', () => { + const response = {items: [{type: 'comment', id: '123'}, {type: 'comment', id: '456'}]} const ids = [1, 2] it('should get an item from an array of ids and send the appropriate dispatches', () => { fetchMock.get('*', JSON.stringify(response)) - return actions.getItemsArray(ids, host)(store.dispatch) + return actions.getItemsArray(ids)(store.dispatch) .then((res) => { expect(res).to.deep.equal(response.items) expect(store.getActions()[0]).to.deep.equal({ type: actions.ADD_ITEM, item: { type: 'comment', - item_id: '123' + id: '123' }, - item_id: '123' + id: '123' }) expect(store.getActions()[1]).to.deep.equal({ type: actions.ADD_ITEM, item: { - type: 'comment', item_id: '456' + type: 'comment', id: '456' }, - item_id: '456' + id: '456' }) }) }) @@ -93,37 +107,38 @@ describe('itemActions', () => { describe('postItem', () => { const item = { - type: 'comment', - data:{content: 'stuff'} + type: 'comments', + data: {body: 'stuff'} } it ('should post an item, return an id, then dispatch that item to the store', () => { - fetchMock.post('*', {item_id: '123', type: 'comment', data: {content: 'stuff'}}) - return actions.postItem(item.data, item.type, undefined, host)(store.dispatch) + fetchMock.post('*', {id: '123'}) + return actions.postItem(item.data, item.type, undefined)(store.dispatch) .then((id) => { expect(fetchMock.calls().matched[0][1]).to.deep.equal( { method: 'POST', - body: JSON.stringify({...item, version: 1}) + headers: { + 'Content-Type':'application/json' + }, + body: JSON.stringify(item.data) } ) expect(id).to.equal('123') expect(store.getActions()[0]).to.deep.equal({ type: actions.ADD_ITEM, item: { - type: 'comment', - data: { - content: 'stuff' - }, - item_id: '123' + body: 'stuff', + id: '123' }, - item_id: '123' + item_type: 'comments', + id: '123' }) }) }) it('should handle an error', () => { fetchMock.post('*', 404) - return actions.postItem(item, host)(store.dispatch) + return actions.postItem(item)(store.dispatch) .catch((err) => { expect(err).to.be.truthy }) @@ -132,17 +147,26 @@ describe('itemActions', () => { describe('postAction', () => { it ('should post an action', () => { - fetchMock.post('*', 200) - return actions.postAction('abc', 'flag', '123', host)(store.dispatch) + fetchMock.post('*', {id: '456'}) + return actions.postAction('abc', 'flag', '123')(store.dispatch) .then(response => { - expect(fetchMock.calls().matched[0][0]).to.equal('http://test.host/v1/action/flag/user/123/on/item/abc') - expect(response).to.equal('') + expect(fetchMock.calls().matched[0][0]).to.equal('/api/v1/comments/abc/actions') + expect(response).to.equal('456') + // expect(store.getActions()[0]).to.deep.equal({ + // type: actions.ADD_ITEM, + // item: { + // type: 'flag', + // item_id: 'abc', + // id: '123' + // }, + // id: '123' + // }) }) }) it('should handle an error', () => { fetchMock.post('*', 404) - return actions.postItem('abc', 'flag', '123', host)(store.dispatch) + return actions.postAction('abc', 'flag', '123')(store.dispatch) .catch((err) => { expect(err).to.be.truthy }) diff --git a/tests/client/coral-framework/store/itemReducer.spec.js b/tests/client/coral-framework/store/itemReducer.spec.js index bd7b74b6c..97482137c 100644 --- a/tests/client/coral-framework/store/itemReducer.spec.js +++ b/tests/client/coral-framework/store/itemReducer.spec.js @@ -9,22 +9,17 @@ describe ('itemsReducer', () => { const action = { type: 'ADD_ITEM', item: { - type: 'comment', - data: { - content: 'stuff' - }, - item_id: '123' + body: 'stuff', + id: '123' }, - item_id: '123' + item_type: 'comments', + id: '123' } const store = new Map({}) const result = itemsReducer(store, action) - expect(result.get('123').toJS()).to.deep.equal({ - type: 'comment', - data: { - content: 'stuff' - }, - item_id: '123' + expect(result.getIn(['comments','123']).toJS()).to.deep.equal({ + body: 'stuff', + id: '123' }) }) }) @@ -35,22 +30,21 @@ describe ('itemsReducer', () => { type: 'UPDATE_ITEM', property: 'stuff', value: 'things', - item_id: '123' + item_type: 'comments', + id: '123' } const store = fromJS({ - '123': { - item_id: '123', - data: { + 'comments': { + '123': { + id: '123', stuff: 'morestuff' } } - }) + }); const result = itemsReducer(store, action) - expect(result.get('123').toJS()).to.deep.equal({ - item_id: '123', - data: { - stuff: 'things' - } + expect(result.getIn(['comments','123']).toJS()).to.deep.equal({ + id: '123', + stuff: 'things' }) }) }) @@ -64,12 +58,13 @@ describe ('itemsReducer', () => { type: 'APPEND_ITEM_ARRAY', property: 'stuff', value: 'things', - item_id: '123' + id: '123', + item_type: 'comments' } store = fromJS({ - '123': { - item_id: '123', - data: { + 'comments': { + '123': { + id: '123', stuff: ['morestuff'] } } @@ -77,73 +72,25 @@ describe ('itemsReducer', () => { }) it ('should append to an existing array', () => { const result = itemsReducer(store, action) - expect(result.get('123').toJS()).to.deep.equal({ - item_id: '123', - data: { - stuff: ['morestuff', 'things'] - } + expect(result.getIn(['comments','123']).toJS()).to.deep.equal({ + id: '123', + stuff: ['morestuff', 'things'] }) }) it ('should create a new array', () => { store = fromJS({ - '123': { - item_id: '123', - data: {} - } - }) - const result = itemsReducer(store, action) - expect(result.get('123').toJS()).to.deep.equal({ - item_id: '123', - data: { - stuff: ['things'] - } - }) - }) - }) - - describe('APPEND_ITEM_RELATED', () => { - let action - let store - - beforeEach (() => { - action = { - type: 'APPEND_ITEM_RELATED', - property: 'stuff', - value: 'things', - item_id: '123' - } - store = fromJS({ - '123': { - item_id: '123', - related: { - stuff: ['morestuff'] + 'comments': { + '123': { + id: '123' } } }) - }) - it ('should append to an existing array', () => { const result = itemsReducer(store, action) - expect(result.get('123').toJS()).to.deep.equal({ - item_id: '123', - related: { - stuff: ['morestuff', 'things'] - } - }) - }) - it ('should create a new array', () => { - store = fromJS({ - '123': { - item_id: '123', - related: {} - } - }) - const result = itemsReducer(store, action) - expect(result.get('123').toJS()).to.deep.equal({ - item_id: '123', - related: { - stuff: ['things'] - } + expect(result.getIn(['comments','123']).toJS()).to.deep.equal({ + id: '123', + stuff: ['things'] }) }) }) + }) From b7ee23d000f0f03352825897a62fddae45624002 Mon Sep 17 00:00:00 2001 From: David Jay Date: Wed, 9 Nov 2016 17:01:50 -0500 Subject: [PATCH 10/13] Updating commentstream to reflect new store structure. --- client/coral-embed-stream/src/CommentStream.js | 18 +++++++++--------- client/coral-plugin-commentbox/CommentBox.js | 9 ++++++--- client/coral-plugin-replies/ReplyButton.js | 2 +- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/client/coral-embed-stream/src/CommentStream.js b/client/coral-embed-stream/src/CommentStream.js index 0804530e8..3a1acbbc6 100644 --- a/client/coral-embed-stream/src/CommentStream.js +++ b/client/coral-embed-stream/src/CommentStream.js @@ -30,11 +30,11 @@ const {setLoggedInUser} = authActions }, (dispatch) => { return { - addItem: (item) => { - return dispatch(addItem(item)) + addItem: (item, itemType) => { + return dispatch(addItem(item, itemType)) }, - updateItem: (id, property, value) => { - return dispatch(updateItem(id, property, value)) + updateItem: (id, property, value, itemType) => { + return dispatch(updateItem(id, property, value, itemType)) }, postItem: (data, type, id) => { return dispatch(postItem(data, type, id)) @@ -54,8 +54,8 @@ const {setLoggedInUser} = authActions postAction: (item, action, user) => { return dispatch(postAction(item, action, user)) }, - appendItemArray: (item, property, value, addToFront) => { - return dispatch(appendItemArray(item, property, value, addToFront)) + appendItemArray: (item, property, value, addToFront, itemType) => { + return dispatch(appendItemArray(item, property, value, addToFront, itemType)) } } } @@ -97,7 +97,7 @@ class CommentStream extends Component { const rootItemId = 'assetTest' - const rootItem = this.props.items[rootItemId] + const rootItem = this.props.items.assets && this.props.items.assets[rootItemId] return
{ rootItem ? @@ -116,7 +116,7 @@ class CommentStream extends Component {
{ rootItem.comments.map((commentId) => { - const comment = this.props.items[commentId] + const comment = this.props.items.comments[commentId] return

@@ -144,7 +144,7 @@ class CommentStream extends Component { { comment.children && comment.children.map((replyId) => { - let reply = this.props.items[replyId] + let reply = this.props.items.comments[replyId] return

diff --git a/client/coral-plugin-commentbox/CommentBox.js b/client/coral-plugin-commentbox/CommentBox.js index 0f1578859..f6f8a7276 100644 --- a/client/coral-plugin-commentbox/CommentBox.js +++ b/client/coral-plugin-commentbox/CommentBox.js @@ -25,17 +25,20 @@ class CommentBox extends Component { asset_id: id, username: this.state.username } - let related + let related; + let parent_type; if (parent_id) { comment.parent_id = parent_id related = 'children' + parent_type = 'comments' } else { related = 'comments' + parent_type = 'assets' } - updateItem(parent_id, 'showReply', false) + updateItem(parent_id, 'showReply', false, 'comments') postItem(comment, 'comments') .then((comment_id) => { - appendItemArray(parent_id || id, related, comment_id, parent_id ? false : true) + appendItemArray(parent_id || id, related, comment_id, parent_id ? false : true, parent_type) addNotification('success', 'Your comment has been posted.') }).catch((err) => console.error(err)) this.setState({body: ''}) diff --git a/client/coral-plugin-replies/ReplyButton.js b/client/coral-plugin-replies/ReplyButton.js index 157d24aaf..d0c10355b 100644 --- a/client/coral-plugin-replies/ReplyButton.js +++ b/client/coral-plugin-replies/ReplyButton.js @@ -5,7 +5,7 @@ const name = 'coral-plugin-replies' const ReplyButton = (props) =>