forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit-comments.spec.ts
More file actions
90 lines (69 loc) · 2.86 KB
/
Copy pathsplit-comments.spec.ts
File metadata and controls
90 lines (69 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { Test, TestingModule } from '@nestjs/testing';
import { SplitCommentService } from './provider/provider.service';
import { getRepositoryToken } from '@nestjs/typeorm';
import { SplitComment } from './split-comment.entity';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { MentionService } from '../mentions/provider/service';
const mockRepo = {
save: jest.fn(),
findAndCount: jest.fn(),
findOne: jest.fn(),
remove: jest.fn(),
};
const mockMentionService = {
extractMentions: jest.fn(),
};
const mockEventEmitter = {
emit: jest.fn(),
};
describe('SplitCommentService', () => {
let service: SplitCommentService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
SplitCommentService,
{ provide: getRepositoryToken(SplitComment), useValue: mockRepo },
{ provide: MentionService, useValue: mockMentionService },
{ provide: EventEmitter2, useValue: mockEventEmitter },
],
}).compile();
service = module.get<SplitCommentService>(SplitCommentService);
});
afterEach(() => jest.clearAllMocks());
it('should create a comment', async () => {
const dto = { splitId: 'split-1', comment: 'hello world' };
mockRepo.save.mockResolvedValue({ id: 'c1', ...dto, userId: 'u1' });
mockMentionService.extractMentions.mockReturnValue([]);
const result = await service.createComment('u1', dto);
expect(mockRepo.save).toHaveBeenCalled();
expect(result.splitId).toBe('split-1');
});
it('should emit mention event when comment has mentions', async () => {
const dto = { splitId: 'split-1', comment: 'hello @john' };
mockRepo.save.mockResolvedValue({ id: 'c1', ...dto, userId: 'u1' });
mockMentionService.extractMentions.mockReturnValue(['john']);
await service.createComment('u1', dto);
expect(mockEventEmitter.emit).toHaveBeenCalledWith('comment.mentioned', {
splitId: 'split-1',
mentionedUsernames: ['john'],
actorId: 'u1',
commentId: 'c1',
});
});
it('should list comments with pagination', async () => {
mockRepo.findAndCount.mockResolvedValue([[{ id: 'c1' }], 1]);
const result = await service.listComments('split-1', 1, 10);
expect(result.total).toBe(1);
expect(result.data.length).toBe(1);
});
it('should delete a comment if user is the owner', async () => {
mockRepo.findOne.mockResolvedValue({ id: 'c1', userId: 'u1' });
mockRepo.remove.mockResolvedValue({});
const result = await service.deleteComment('c1', 'u1');
expect(result.message).toBe('Comment deleted successfully');
});
it('should throw error if user tries to delete someone elses comment', async () => {
mockRepo.findOne.mockResolvedValue({ id: 'c1', userId: 'u1' });
await expect(service.deleteComment('c1', 'u2')).rejects.toThrow('You can only delete your own comments');
});
});