Skip to content

Commit e3304c4

Browse files
sinchangJacksonTian
authored andcommitted
test: add unit test for service topic (#37)
* test: add unit test for service topic
1 parent ab426ca commit e3304c4

File tree

2 files changed

+135
-1
lines changed

2 files changed

+135
-1
lines changed

app/service/topic.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class TopicService extends Service {
5454

5555
await Promise.all(
5656
topics.map(async topic => {
57-
const { author, reply } = await Promise.all([
57+
const [ author, reply ] = await Promise.all([
5858
this.service.user.getUserById(topic.author_id),
5959
// 获取主题的最后回复
6060
this.service.reply.getReplyById(topic.last_reply),

test/app/service/topic.test.js

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
'use strict';
2+
3+
const { app, assert } = require('egg-mock/bootstrap');
4+
5+
describe('test/app/service/topic.test.js', () => {
6+
let ctx;
7+
let topic;
8+
let topicId;
9+
let replyId;
10+
let userId;
11+
let loginname;
12+
let email;
13+
14+
before(async () => {
15+
ctx = app.mockContext();
16+
topic = ctx.service.topic;
17+
loginname = `loginname_${Date.now()}`;
18+
email = `${loginname}@test.com`;
19+
const result = await ctx.service.user.newAndSave('name', loginname, 'pass', email, 'avatar_url', 'active');
20+
userId = result._id;
21+
assert(result.loginname === loginname);
22+
});
23+
24+
25+
it('newAndSave should ok', async () => {
26+
const title = 'first post';
27+
const content = 'hello world';
28+
const tab = 'share';
29+
const result = await topic.newAndSave(title, content, tab, userId);
30+
assert(result.title === title);
31+
assert(result.content === content);
32+
assert(result.tab === tab);
33+
assert.equal(result.author_id, userId);
34+
topicId = result._id;
35+
const reply = await ctx.service.reply.newAndSave('hi', topicId, userId);
36+
assert(reply.content === 'hi');
37+
assert(reply.author_id === userId);
38+
assert(reply.topic_id === topicId);
39+
replyId = reply._id;
40+
});
41+
42+
it('updateLastReply should ok', async () => {
43+
const result1 = await topic.updateLastReply(topicId, replyId);
44+
assert(result1.last_reply === replyId);
45+
const result2 = await topic.updateLastReply();
46+
assert(!result2);
47+
});
48+
49+
it('getTopicById should ok', async () => {
50+
const result1 = await topic.getTopicById(topicId);
51+
assert.equal(result1.topic._id.toString(), topicId);
52+
assert.equal(result1.author._id.toString(), userId);
53+
assert.equal(result1.last_reply._id.toString(), replyId);
54+
const result2 = await topic.getTopicById();
55+
assert(result2.topic === null);
56+
assert(result2.author === null);
57+
assert(result2.last_reply === null);
58+
});
59+
60+
it('getCountByQuery should ok', async () => {
61+
const query = {
62+
good: false,
63+
};
64+
const result = await topic.getCountByQuery(query);
65+
assert(result === 1);
66+
});
67+
68+
it('getTopicsByQuery should ok', async () => {
69+
const query1 = {
70+
good: false,
71+
};
72+
const result1 = await topic.getTopicsByQuery(query1, {});
73+
assert(result1.length === 1);
74+
75+
const query2 = {
76+
good: 'test',
77+
};
78+
const result2 = await topic.getTopicsByQuery(query2, {});
79+
assert(result2.length === 0);
80+
});
81+
82+
it('getLimit5w should ok', async () => {
83+
const result = await topic.getLimit5w();
84+
assert(result.length === 1);
85+
});
86+
87+
it('getFullTopic should ok', async () => {
88+
let err1;
89+
try {
90+
await topic.getFullTopic();
91+
} catch (e) {
92+
err1 = e;
93+
assert(e.message === '此话题不存在或已被删除。');
94+
}
95+
assert(err1);
96+
97+
const result = await topic.getFullTopic(topicId);
98+
assert.equal(result[0]._id.toString(), topicId);
99+
assert(result[1].loginname === loginname);
100+
101+
let err2;
102+
await ctx.model.User.deleteOne({ _id: userId }).exec();
103+
try {
104+
await topic.getFullTopic(topicId);
105+
} catch (e) {
106+
err2 = e;
107+
assert(e.message === '话题的作者丢了。');
108+
}
109+
assert(err2);
110+
});
111+
112+
it('getTopic should ok', async () => {
113+
const result = await topic.getTopic(topicId);
114+
assert.equal(result._id.toString(), topicId);
115+
assert.equal(result.author_id.toString(), userId);
116+
});
117+
118+
it('reduceCount should ok', async () => {
119+
const result1 = await topic.reduceCount(topicId);
120+
assert(result1.last_reply, replyId);
121+
await ctx.model.Reply.deleteOne({ _id: replyId }).exec();
122+
const result2 = await topic.reduceCount(topicId);
123+
assert(result2.last_reply === null);
124+
125+
let err;
126+
try {
127+
await topic.reduceCount();
128+
} catch (e) {
129+
err = e;
130+
assert(e.message === '该主题不存在');
131+
}
132+
assert(err);
133+
});
134+
});

0 commit comments

Comments
 (0)