Skip to content

Commit c9571be

Browse files
sinchangJacksonTian
authored andcommitted
test: add service message unit test
1 parent 2f7dd8c commit c9571be

File tree

3 files changed

+107
-6
lines changed

3 files changed

+107
-6
lines changed

app/service/message.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ class MessageService extends Service {
3131
message.type === 'at'
3232
) {
3333
const [ author, topic, reply ] = await Promise.all([
34-
this.services.user.getUserById(message.author_id),
35-
this.services.topic.getTopicById(message.topic_id),
36-
this.services.reply.getReplyById(message.reply_id),
34+
this.service.user.getUserById(message.author_id),
35+
this.service.topic.getTopicById(message.topic_id),
36+
this.service.reply.getReplyById(message.reply_id),
3737
]);
3838

3939
message.author = author;

test/app/service/message.test.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
'use strict';
2+
3+
const { app, assert } = require('egg-mock/bootstrap');
4+
5+
describe('test/app/service/message.test.js', () => {
6+
let topicId,
7+
loginname1,
8+
loginname2,
9+
user1,
10+
user2,
11+
ctx,
12+
message,
13+
messageService;
14+
before(async function() {
15+
ctx = app.mockContext();
16+
messageService = ctx.service.message;
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+
35+
it('sendAtMessage should ok', async () => {
36+
const result = await messageService.sendAtMessage(user2._id, user1._id, topicId, 'at');
37+
message = result;
38+
assert(message.type === 'at');
39+
assert(message.topic_id.toString() === topicId.toString());
40+
assert(message.author_id === user1._id);
41+
assert.equal(message.master_id.toString(), user2._id);
42+
});
43+
44+
it('getMessagesCount should ok', async () => {
45+
const result = await messageService.getMessagesCount(user2._id);
46+
assert(result >= 1);
47+
});
48+
49+
it('getMessageRelations should ok', async () => {
50+
const result1 = await messageService.getMessageRelations(message);
51+
assert(result1.topic_id === message.topic_id);
52+
const mockMessage1 = await messageService.sendAtMessage(user2._id, '565c4473d0bc14ae279399fe', user1._id, 'at');
53+
const result2 = await messageService.getMessageRelations(mockMessage1);
54+
assert(result2.is_invalid === true);
55+
assert(result1.topic_id === message.topic_id);
56+
const mockMessage2 = await messageService.sendAtMessage(user2._id, topicId, user1._id, 'at1');
57+
const result3 = await messageService.getMessageRelations(mockMessage2);
58+
assert(result3.is_invalid === true);
59+
});
60+
61+
it('getMessageById should ok', async () => {
62+
const result = await messageService.getMessageById(message._id);
63+
assert(result._id.toString() === message._id.toString());
64+
});
65+
66+
it('getReadMessagesByUserId should ok', async () => {
67+
const result = await messageService.getReadMessagesByUserId(user2._id);
68+
assert(result.length === 0);
69+
});
70+
71+
it('getUnreadMessagesByUserId should ok', async () => {
72+
const result = await messageService.getUnreadMessagesByUserId(user2._id);
73+
assert(result.length >= 1);
74+
});
75+
76+
it('updateMessagesToRead should ok', async () => {
77+
const result1 = await messageService.updateMessagesToRead(user2._id, []);
78+
assert(result1 === undefined);
79+
80+
const result2 = await messageService.updateMessagesToRead(user2._id, [ message ]);
81+
assert(result2.ok === 1);
82+
});
83+
84+
it('updateOneMessageToRead should ok', async () => {
85+
const message = await messageService.sendAtMessage(user2._id, topicId, user1._id, 'at');
86+
const result1 = await messageService.updateOneMessageToRead();
87+
assert(result1 === undefined);
88+
const result2 = await messageService.updateOneMessageToRead(message._id);
89+
assert(result2.ok === 1);
90+
});
91+
});

test/app/service/reply.test.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ describe('reply service', () => {
3333
assert(reply.content === reply_content);
3434
});
3535

36+
it('getReply should ok', async function() {
37+
// 创建 ctx
38+
const ctx = app.mockContext();
39+
40+
const test_reply = await ctx.service.reply.getReply(reply._id);
41+
assert(test_reply.content === reply.content);
42+
});
43+
3644
it('getReplyById should ok', async function() {
3745
// 创建 ctx
3846
const ctx = app.mockContext();
@@ -77,8 +85,10 @@ describe('reply service', () => {
7785
assert(test_topic.title === topic_title);
7886

7987
const reply_content = '<div class=\"markdown-text\"><p>unit test reply</p>\n</div>';
80-
const test_reply = await ctx.service.reply.newAndSave(reply_content, test_topic._id, user._id);
81-
assert(test_reply.content === reply_content);
88+
const test_reply1 = await ctx.service.reply.newAndSave(reply_content, test_topic._id, user._id);
89+
const test_reply2 = await ctx.service.reply.newAndSave(reply_content, test_topic._id, user._id, reply._id);
90+
assert(test_reply1.content === reply_content);
91+
assert(test_reply2.reply_id.toString() === reply._id.toString());
8292
});
8393

8494
it('getLastReplyByTopId should ok', async function() {
@@ -103,7 +113,7 @@ describe('reply service', () => {
103113
const ctx = app.mockContext();
104114

105115
const count = await ctx.service.reply.getCountByAuthorId(user._id);
106-
assert(count === 2);
116+
assert(count >= 1);
107117
});
108118

109119
});

0 commit comments

Comments
 (0)