Skip to content

Commit 6b072d8

Browse files
sinchangJacksonTian
authored andcommitted
test: add service at unit test (#57)
* test: add service at unit test * chore: fix lint
1 parent cb9ad33 commit 6b072d8

File tree

3 files changed

+83
-1
lines changed

3 files changed

+83
-1
lines changed

app/service/at.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,10 @@ class AtService extends Service {
4545
* @param {String} text 文本内容
4646
* @param {String} topicId 主题ID
4747
* @param {String} authorId 作者ID
48+
* @param {String} type 回复类型
4849
* @param {String} reply_id 回复ID
4950
*/
50-
async sendMessageToMentionUsers(text, topicId, authorId, reply_id = null) {
51+
async sendMessageToMentionUsers(text, topicId, authorId, type, reply_id = null) {
5152
let users = await this.service.user.getUsersByNames(this.fetchUsers(text));
5253

5354
users = users.filter(user => {
@@ -60,6 +61,7 @@ class AtService extends Service {
6061
user._id,
6162
authorId,
6263
topicId,
64+
type,
6365
reply_id
6466
);
6567
})

app/service/message.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,18 @@ class MessageService extends Service {
117117
const update = { $set: { has_read: true } };
118118
return this.ctx.model.Message.update(query, update, { multi: true }).exec();
119119
}
120+
121+
async sendAtMessage(userId, authorId, topicId, type, replyId) {
122+
const message = this.ctx.model.Message();
123+
124+
message.type = type;
125+
message.master_id = userId;
126+
message.author_id = authorId;
127+
message.topic_id = topicId;
128+
message.reply_id = replyId;
129+
130+
return message.save();
131+
}
120132
}
121133

122134
module.exports = MessageService;

test/app/service/at.test.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
'use strict';
2+
3+
const { app, assert } = require('egg-mock/bootstrap');
4+
5+
describe('test/app/service/at.test.js', () => {
6+
let topicId,
7+
loginname1,
8+
loginname2,
9+
user1,
10+
user2,
11+
replyId,
12+
ctx,
13+
atService;
14+
before(async function() {
15+
ctx = app.mockContext();
16+
atService = ctx.service.at;
17+
loginname1 = `loginname1_${Date.now()}`;
18+
loginname2 = `loginname2_${Date.now()}`;
19+
user1 = await ctx.service.user.newAndSave('name', loginname1, 'pass', `${loginname1}@test.com`, 'avatar_url', 'active');
20+
user2 = await ctx.service.user.newAndSave('name', loginname2, 'pass', `${loginname2}@test.com`, 'avatar_url', 'active');
21+
assert(user1.loginname === loginname1);
22+
assert(user2.loginname === loginname2);
23+
24+
const title = 'first post';
25+
const content = 'hello world';
26+
const tab = 'share';
27+
const topic = await ctx.service.topic.newAndSave(title, content, tab, user1._id);
28+
topicId = topic._id;
29+
assert(topic.title === title);
30+
assert(topic.content === content);
31+
assert(topic.tab === tab);
32+
assert.equal(topic.author_id, user1._id);
33+
34+
const reply = await ctx.service.reply.newAndSave('hi', topicId, user1._id);
35+
assert(reply.content === 'hi');
36+
assert(reply.author_id === user1._id);
37+
assert(reply.topic_id === topicId);
38+
replyId = reply._id;
39+
});
40+
41+
it('fetchUsers should ok', async () => {
42+
const result1 = await atService.fetchUsers();
43+
assert(result1.length === 0);
44+
const result2 = await ctx.service.at.fetchUsers('good job!!! @sinchang @cnode');
45+
assert(result2[0] === 'sinchang');
46+
assert(result2[1] === 'cnode');
47+
});
48+
49+
it('sendMessageToMentionUsers should ok', async () => {
50+
const result1 = await atService.sendMessageToMentionUsers(`hi!!!@${loginname2}`, topicId, user1._id, 'at');
51+
assert(result1[0].type === 'at');
52+
assert(result1[0].topic_id === topicId);
53+
assert(result1[0].author_id === user1._id);
54+
assert.equal(result1[0].master_id.toString(), user2._id);
55+
assert(result1[0].reply_id === null);
56+
const result2 = await atService.sendMessageToMentionUsers(`hi!!!@${loginname2}`, topicId, user1._id, 'reply2', replyId);
57+
assert(result2[0].type === 'reply2');
58+
assert(result2[0].topic_id === topicId);
59+
assert(result2[0].author_id === user1._id);
60+
assert.equal(result2[0].master_id.toString(), user2._id);
61+
assert(result2[0].reply_id === replyId);
62+
});
63+
64+
it('linkUsers should ok', async () => {
65+
const result = await atService.linkUsers(`hi!!!@${loginname2}`);
66+
assert(result === `hi!!![@${loginname2}](/user/${loginname2})`);
67+
});
68+
});

0 commit comments

Comments
 (0)