|
| 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!!`); |
| 67 | + }); |
| 68 | +}); |
0 commit comments