Skip to content
This repository was archived by the owner on Jun 27, 2019. It is now read-only.

Commit 16edec6

Browse files
committed
contribution / comment tests and optimization
1 parent 792df32 commit 16edec6

File tree

8 files changed

+271
-86
lines changed

8 files changed

+271
-86
lines changed

server/helper/alter-items.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module.exports = func => hook => {
1414
}
1515
let items = getItems(hook);
1616
if (Array.isArray(items)) {
17-
items.map(item => func(item, hook));
17+
items = items.map(item => func(item, hook));
1818
} else if (items) {
1919
items = func(items, hook);
2020
}

server/services/comments/hooks/create-notifications.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module.exports = function (options = {}) { // eslint-disable-line no-unused-vars
1414
const contributionId = hook.result.contributionId;
1515
const creatorId = hook.result.userId;
1616

17-
contributionService.get(contributionId)
17+
contributionService.get(contributionId, { _populate: 'skip' })
1818
.then(result => {
1919
const userId = result.userId;
2020

server/services/contributions/hooks/get-associated-can-dos.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module.exports = function (options = {}) { // eslint-disable-line no-unused-vars
1010
}
1111

1212
// Stop, if it was a find method and $limit was not set
13-
if (hook.method === 'find' && (!hook.params.query || hook.params.query.$limit !== 1)) {
13+
if (hook.method === 'find' && (!hook.params.query || (hook.params.query.$limit !== 1 && !hook.params.query.slug))) {
1414
return resolve(hook);
1515
}
1616

@@ -21,7 +21,8 @@ module.exports = function (options = {}) { // eslint-disable-line no-unused-vars
2121
}
2222

2323
let currentData = isArray ? hook.result.data[0] : hook.result;
24-
if (!currentData.categoryIds || !currentData.categoryIds.length) {
24+
let categoryIds = currentData.categoryIds;
25+
if (!categoryIds || !categoryIds.length) {
2526
return resolve(hook);
2627
}
2728

@@ -32,16 +33,15 @@ module.exports = function (options = {}) { // eslint-disable-line no-unused-vars
3233
})
3334
.then(({data}) => {
3435
let associatedCanDos = [];
35-
let categoryIds = currentData.categoryIds;
36-
if (categoryIds && categoryIds.length) {
37-
while (associatedCanDos.length < limit && data.length) {
38-
let item = data.shift();
39-
let check = categoryIds.some(category => {
40-
return item.categoryIds.includes(category);
36+
while (associatedCanDos.length < limit && data.length) {
37+
let item = data.shift();
38+
let check = categoryIds.some(id => {
39+
return item.categoryIds.some(innerId => {
40+
return innerId.toString() == id.toString();
4141
});
42-
if (check && item._id.toString() !== currentData._id.toString()) {
43-
associatedCanDos.push(item);
44-
}
42+
});
43+
if (check && item._id.toString() !== currentData._id.toString()) {
44+
associatedCanDos.push(item);
4545
}
4646
}
4747
if (isArray) {

test/assets/categories.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const categoryData = {
2+
title: 'a',
3+
icon: 'a'
4+
};
5+
6+
module.exports = {
7+
categoryData
8+
};

test/assets/comments.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const commentData = {
2+
content: 'My comment content',
3+
};
4+
5+
module.exports = {
6+
commentData
7+
};

test/assets/contributions.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const contributionData = {
2+
title: 'a',
3+
type: 'post',
4+
content: 'My contribution content',
5+
language: 'en'
6+
};
7+
8+
const contributionData2 = {
9+
title: 'b',
10+
type: 'post',
11+
content: 'My contribution content',
12+
language: 'en'
13+
};
14+
15+
const contributionCandoData = {
16+
title: 'c',
17+
type: 'cando',
18+
content: 'My contribution content',
19+
language: 'en',
20+
cando: {
21+
difficulty: 'easy',
22+
reasonTitle: 'a',
23+
reason: 'My cando reason'
24+
}
25+
};
26+
27+
module.exports = {
28+
contributionData,
29+
contributionData2,
30+
contributionCandoData
31+
};
32+

test/services/comments.test.js

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,84 @@
11
const assert = require('assert');
22
const app = require('../../server/app');
3+
const service = app.service('comments');
4+
const contributionService = app.service('contributions');
5+
const userService = app.service('users');
6+
7+
const { userData } = require('../assets/users');
8+
const { contributionData } = require('../assets/contributions');
9+
const { commentData } = require('../assets/comments');
310

411
describe('\'comments\' service', () => {
5-
it('registered the service', () => {
6-
const service = app.service('comments');
12+
let user;
13+
let params;
14+
let contribution;
15+
16+
before(function(done) {
17+
this.server = app.listen(3031);
18+
this.server.once('listening', () => done());
19+
});
20+
21+
after(function(done) {
22+
this.server.close(done);
23+
});
24+
25+
beforeEach(async () => {
26+
await app.get('mongooseClient').connection.dropDatabase();
27+
user = await userService.create(userData);
28+
params = {
29+
user
30+
};
31+
contribution = await contributionService.create(contributionData, params);
32+
commentData.userId = user._id;
33+
commentData.contributionId = contribution._id;
34+
});
35+
36+
afterEach(async () => {
37+
await app.get('mongooseClient').connection.dropDatabase();
38+
user = null;
39+
params = null;
40+
contribution = null;
41+
delete commentData.userId;
42+
delete commentData.contributionId;
43+
});
744

45+
it('registered the service', () => {
846
assert.ok(service, 'Registered the service');
947
});
10-
});
48+
49+
describe('comments create', () => {
50+
it('runs create', async () => {
51+
const comment = await service.create(commentData, params);
52+
assert.ok(comment, 'created comment');
53+
});
54+
});
55+
56+
describe('comments patch', () => {
57+
let comment;
58+
59+
beforeEach(async () => {
60+
comment = await service.create(commentData, params);
61+
});
62+
63+
it('runs patch', async () => {
64+
const result = await service.patch(
65+
comment._id,
66+
{ content: 'Test' },
67+
params
68+
);
69+
assert.ok(result, 'returns result');
70+
assert.equal(result.content, 'Test', 'patched content');
71+
});
72+
});
73+
74+
describe('comments find', () => {
75+
beforeEach(async () => {
76+
await service.create(commentData, params);
77+
});
78+
79+
it('runs find', async () => {
80+
const result = await service.find();
81+
assert.ok(result.data[0], 'returns data');
82+
});
83+
});
84+
});

0 commit comments

Comments
 (0)