From 18710e2345c5cfbb9cfe7c0b2d981a265d8dd6f5 Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Wed, 14 Feb 2018 15:56:04 -0700 Subject: [PATCH] added tests --- graph/loaders/actions.js | 15 ++-- test/server/graph/loaders/actions.js | 122 +++++++++++++++++++++++++++ 2 files changed, 132 insertions(+), 5 deletions(-) create mode 100644 test/server/graph/loaders/actions.js diff --git a/graph/loaders/actions.js b/graph/loaders/actions.js index 99aaa6015..b5ea31a3f 100644 --- a/graph/loaders/actions.js +++ b/graph/loaders/actions.js @@ -1,13 +1,15 @@ const DataLoader = require('dataloader'); const util = require('./util'); -const ActionsService = require('../../services/actions'); const { first, get, merge, remove, groupBy, reduce } = require('lodash'); /** * Gets actions based on their item id's. */ -const genActionsByItemID = (_, item_ids) => { - return ActionsService.findByItemIdArray(item_ids).then( +const genActionsByItemID = ( + { connectors: { services: { Actions } } }, + item_ids +) => { + return Actions.findByItemIdArray(item_ids).then( util.arrayJoinBy(item_ids, 'item_id') ); }; @@ -18,8 +20,11 @@ const genActionsByItemID = (_, item_ids) => { * @param {Object} ctx the graph context of the request * @param {Array} itemIDs the items that we need to get the actions for */ -const genActionsAuthoredWithID = ({ user = {} }, itemIDs) => - ActionsService.getUserActions(user.id, itemIDs).then( +const genActionsAuthoredWithID = ( + { user = {}, connectors: { services: { Actions } } }, + itemIDs +) => + Actions.getUserActions(user.id, itemIDs).then( util.arrayJoinBy(itemIDs, 'item_id') ); diff --git a/test/server/graph/loaders/actions.js b/test/server/graph/loaders/actions.js new file mode 100644 index 000000000..f8e96175b --- /dev/null +++ b/test/server/graph/loaders/actions.js @@ -0,0 +1,122 @@ +const chai = require('chai'); +chai.use(require('chai-as-promised')); +const { expect } = chai; +const sinon = require('sinon'); +const { find } = require('lodash'); +const loaders = require('../../../../graph/loaders/actions'); + +describe('graph.loaders.Actions', () => { + describe('#getAuthoredByID', () => { + it('loads the correct entries', async () => { + const spy = sinon.spy(async () => [ + { item_id: 'comment_1' }, + { item_id: 'comment_2' }, + ]); + const { Actions: { getAuthoredByID } } = loaders({ + user: { id: 'user_1' }, + connectors: { services: { Actions: { getUserActions: spy } } }, + }); + + const actions = await getAuthoredByID.loadMany([ + 'comment_2', + 'comment_1', + ]); + + expect(spy.calledWith('user_1', ['comment_2', 'comment_1'])); + expect(actions).to.have.length(2); + expect(actions[0]).to.have.length(1); + expect(actions[0][0]).to.have.property('item_id', 'comment_2'); + expect(actions[1]).to.have.length(1); + expect(actions[1][0]).to.have.property('item_id', 'comment_1'); + }); + }); + + describe('#getSummariesByItem', () => { + describe('logged out user', () => { + it('does not include any user data', async () => { + const { Actions: { getSummariesByItem } } = loaders({ + loaders: { + Actions: { + getAuthoredByID: { + load: () => Promise.reject(new Error('should not be called')), + }, + }, + }, + user: null, + }); + + const summaries = await getSummariesByItem.load({ + id: '1', + action_counts: { flag: 1, flag_comment_offensive: 1, respect: 2 }, + }); + + expect(summaries).to.have.length(2); + + const flag = find(summaries, { action_type: 'FLAG' }); + expect(flag).to.be.defined; + + expect(flag).to.have.property('current_user', null); + expect(flag).to.have.property('action_type', 'FLAG'); + expect(flag).to.have.property('group_id', 'COMMENT_OFFENSIVE'); + expect(flag).to.have.property('count', 1); + + const respect = find(summaries, { action_type: 'RESPECT' }); + expect(respect).to.be.defined; + + expect(respect).to.have.property('current_user', null); + expect(respect).to.have.property('action_type', 'RESPECT'); + expect(respect).to.have.property('group_id', null); + expect(respect).to.have.property('count', 2); + }); + }); + + describe('logged in user', () => { + it('does include user', async () => { + const { Actions: { getSummariesByItem } } = loaders({ + loaders: { + Actions: { + getAuthoredByID: { + load: commentID => { + expect(commentID).to.equal('comment_1'); + return [ + { + id: 'action_1', + action_type: 'FLAG', + group_id: 'COMMENT_OFFENSIVE', + }, + ]; + }, + }, + }, + }, + user: { id: 'user_1' }, + }); + + const summaries = await getSummariesByItem.load({ + id: 'comment_1', + action_counts: { flag: 1, flag_comment_offensive: 1, respect: 2 }, + }); + + expect(summaries).to.have.length(2); + + const flag = find(summaries, { action_type: 'FLAG' }); + expect(flag).to.be.defined; + + expect(flag).to.have.property('action_type', 'FLAG'); + expect(flag).to.have.property('group_id', 'COMMENT_OFFENSIVE'); + expect(flag).to.have.property('count', 1); + + expect(flag).to.have.property('current_user').not.null; + expect(flag.current_user).to.have.property('id', 'action_1'); + + const respect = find(summaries, { action_type: 'RESPECT' }); + expect(respect).to.be.defined; + + expect(respect).to.have.property('current_user', null); + expect(respect).to.have.property('action_type', 'RESPECT'); + expect(respect).to.have.property('group_id', null); + expect(respect).to.have.property('count', 2); + }); + }); + }); +});