mirror of
https://github.com/wassname/talk.git
synced 2026-07-17 11:33:39 +08:00
added tests
This commit is contained in:
@@ -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<String>} 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')
|
||||
);
|
||||
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user