|
| 1 | +/** |
| 2 | + * Copyright: The PastVu contributors. |
| 3 | + * GNU Affero General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/agpl.txt) |
| 4 | + */ |
| 5 | + |
| 6 | +import { CommentN } from '../../models/Comment'; |
| 7 | +import admin from '../admin'; |
| 8 | +import comment from '../comment'; |
| 9 | +import testHelpers from '../../tests/testHelpers'; |
| 10 | + |
| 11 | +describe('comment', () => { |
| 12 | + beforeEach(async () => { |
| 13 | + // Mock non-registerd user handshake. |
| 14 | + admin.handshake = { 'usObj': { 'isAdmin': true } }; |
| 15 | + }); |
| 16 | + |
| 17 | + afterEach(() => { |
| 18 | + // Delete handshake. |
| 19 | + delete admin.handshake; |
| 20 | + }); |
| 21 | + |
| 22 | + describe('create for news', () => { |
| 23 | + let news; |
| 24 | + |
| 25 | + beforeEach(async () => { |
| 26 | + const data = { pdate: new Date(), 'title': 'Test news', 'txt': 'Test news content' }; |
| 27 | + |
| 28 | + ({ news } = await admin.saveOrCreateNews(data)); |
| 29 | + |
| 30 | + const user = await testHelpers.createUser({ login: 'user1', pass: 'pass1' }); |
| 31 | + |
| 32 | + // Mock non-registered user handshake. |
| 33 | + comment.handshake = { 'usObj': { 'isAdmin': true, 'registered': true, user } }; |
| 34 | + }); |
| 35 | + |
| 36 | + afterEach(() => { |
| 37 | + // Delete handshake. |
| 38 | + delete comment.handshake; |
| 39 | + }); |
| 40 | + |
| 41 | + it('create', async () => { |
| 42 | + expect.assertions(3); |
| 43 | + |
| 44 | + const data = { txt: 'news comment', type: 'news', obj: news.cid }; |
| 45 | + |
| 46 | + // Create two comments. |
| 47 | + const result = await comment.create(data); |
| 48 | + |
| 49 | + expect(result.comment.txt).toMatch(data.txt); |
| 50 | + expect(result.comment.user).toMatch('user1'); |
| 51 | + |
| 52 | + await expect(CommentN.count({ obj: news })).resolves.toBe(1); |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + describe('retrive', () => { |
| 57 | + let news; |
| 58 | + |
| 59 | + beforeEach(async () => { |
| 60 | + const data = { pdate: new Date(), 'title': 'Test news', 'txt': 'Test news content' }; |
| 61 | + |
| 62 | + ({ news } = await admin.saveOrCreateNews(data)); |
| 63 | + |
| 64 | + const user = await testHelpers.createUser({ login: 'user1', pass: 'pass1' }); |
| 65 | + |
| 66 | + // Mock non-registered user handshake. |
| 67 | + comment.handshake = { 'usObj': { 'isAdmin': true, 'registered': true, user } }; |
| 68 | + }); |
| 69 | + |
| 70 | + afterEach(() => { |
| 71 | + // Delete handshake. |
| 72 | + delete comment.handshake; |
| 73 | + }); |
| 74 | + |
| 75 | + it('give news comments for user', async () => { |
| 76 | + expect.assertions(17); |
| 77 | + |
| 78 | + const data = { txt: 'news comment', type: 'news', obj: news.cid }; |
| 79 | + |
| 80 | + // Create 4 comments. |
| 81 | + const comment0 = await comment.create(data); |
| 82 | + const comment1 = await comment.create(data); |
| 83 | + |
| 84 | + data.parent = comment1.comment.cid; |
| 85 | + data.level = comment1.comment.level + 1; |
| 86 | + |
| 87 | + const comment2 = await comment.create(data); |
| 88 | + |
| 89 | + data.parent = comment2.comment.cid; |
| 90 | + data.level = comment2.comment.level + 1; |
| 91 | + |
| 92 | + const comment3 = await comment.create(data); |
| 93 | + |
| 94 | + // Sanity check. |
| 95 | + await expect(CommentN.count({ obj: news })).resolves.toBe(4); |
| 96 | + |
| 97 | + const comments = await comment.giveForUser({ login: 'user1', type: 'news' }); |
| 98 | + |
| 99 | + expect(comments.type).toMatch('news'); |
| 100 | + expect(comments.countActive).toBe(4); |
| 101 | + expect(comments.objs[news.cid].cid).toStrictEqual(news.cid); |
| 102 | + expect(comments.objs[news.cid].ccount).toBe(4); |
| 103 | + // Comment 0 - no child, waits answer. |
| 104 | + expect(comments.comments[3].cid).toStrictEqual(comment0.comment.cid); |
| 105 | + expect(comments.comments[3].hasChild).toBeFalsy(); |
| 106 | + expect(comments.comments[3].waitsAnswer).toBeTruthy(); |
| 107 | + // Comment 1 - has child, does not wait answer. |
| 108 | + expect(comments.comments[2].cid).toStrictEqual(comment1.comment.cid); |
| 109 | + expect(comments.comments[2].hasChild).toBeTruthy(); |
| 110 | + expect(comments.comments[2].waitsAnswer).toBeFalsy(); |
| 111 | + // Comment 2 - has child, does not wait answer. |
| 112 | + expect(comments.comments[1].cid).toStrictEqual(comment2.comment.cid); |
| 113 | + expect(comments.comments[1].hasChild).toBeTruthy(); |
| 114 | + expect(comments.comments[1].waitsAnswer).toBeFalsy(); |
| 115 | + // Comment 3 - no child, waits answer. |
| 116 | + expect(comments.comments[0].cid).toStrictEqual(comment3.comment.cid); |
| 117 | + expect(comments.comments[0].hasChild).toBeFalsy(); |
| 118 | + expect(comments.comments[0].waitsAnswer).toBeTruthy(); |
| 119 | + }); |
| 120 | + }); |
| 121 | +}); |
0 commit comments