-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathmessage.service.spec.ts
198 lines (183 loc) · 7.38 KB
/
message.service.spec.ts
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
* Copyright © 2024 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
*/
import { EventEmitter2 } from '@nestjs/event-emitter';
import { MongooseModule } from '@nestjs/mongoose';
import { Test } from '@nestjs/testing';
import { AttachmentRepository } from '@/attachment/repositories/attachment.repository';
import { AttachmentModel } from '@/attachment/schemas/attachment.schema';
import { AttachmentService } from '@/attachment/services/attachment.service';
import { LoggerService } from '@/logger/logger.service';
import { RoleRepository } from '@/user/repositories/role.repository';
import { UserRepository } from '@/user/repositories/user.repository';
import { PermissionModel } from '@/user/schemas/permission.schema';
import { RoleModel } from '@/user/schemas/role.schema';
import { User, UserModel } from '@/user/schemas/user.schema';
import { RoleService } from '@/user/services/role.service';
import { UserService } from '@/user/services/user.service';
import {
installMessageFixtures,
messageFixtures,
} from '@/utils/test/fixtures/message';
import { getPageQuery } from '@/utils/test/pagination';
import { sortRowsBy } from '@/utils/test/sort';
import {
closeInMongodConnection,
rootMongooseTestModule,
} from '@/utils/test/test';
import { MessageRepository } from '../repositories/message.repository';
import { Message, MessageModel } from '../schemas/message.schema';
import { Subscriber, SubscriberModel } from '../schemas/subscriber.schema';
import { SubscriberRepository } from './../repositories/subscriber.repository';
import { MessageService } from './message.service';
import { SubscriberService } from './subscriber.service';
describe('MessageService', () => {
let messageRepository: MessageRepository;
let messageService: MessageService;
let subscriberRepository: SubscriberRepository;
let userRepository: UserRepository;
let allMessages: Message[];
let allSubscribers: Subscriber[];
let allUsers: User[];
let message: Message;
let sender: Subscriber;
let recipient: Subscriber;
let messagesWithSenderAndRecipient: Message[];
let user: User;
beforeAll(async () => {
const module = await Test.createTestingModule({
imports: [
rootMongooseTestModule(installMessageFixtures),
MongooseModule.forFeature([
UserModel,
RoleModel,
PermissionModel,
SubscriberModel,
MessageModel,
AttachmentModel,
]),
],
providers: [
LoggerService,
AttachmentService,
AttachmentRepository,
UserService,
UserRepository,
RoleService,
RoleRepository,
SubscriberService,
SubscriberRepository,
MessageService,
MessageRepository,
EventEmitter2,
],
}).compile();
messageService = module.get<MessageService>(MessageService);
messageRepository = module.get<MessageRepository>(MessageRepository);
subscriberRepository =
module.get<SubscriberRepository>(SubscriberRepository);
userRepository = module.get<UserRepository>(UserRepository);
allSubscribers = await subscriberRepository.findAll();
allUsers = await userRepository.findAll();
allMessages = await messageRepository.findAll();
message = (await messageRepository.findOne({ mid: 'mid-1' }))!;
sender = (await subscriberRepository.findOne(message.sender!))!;
recipient = (await subscriberRepository.findOne(message.recipient!))!;
user = (await userRepository.findOne(message.sentBy!))!;
messagesWithSenderAndRecipient = allMessages.map((message) => ({
...message,
sender: allSubscribers.find(({ id }) => id === message.sender)?.id,
recipient: allSubscribers.find(({ id }) => id === message.recipient)?.id,
sentBy: allUsers.find(({ id }) => id === message.sentBy)?.id,
}));
});
afterEach(jest.clearAllMocks);
afterAll(closeInMongodConnection);
function toArray(value?: string | string[]): string[] {
return value ? (Array.isArray(value) ? value : [value]) : [];
}
describe('findOneAndPopulate', () => {
it('should find message by id, and populate its corresponding sender and recipient', async () => {
jest.spyOn(messageRepository, 'findOneAndPopulate');
const result = await messageService.findOneAndPopulate(message.id);
expect(messageRepository.findOneAndPopulate).toHaveBeenCalledWith(
message.id,
undefined,
);
const expectedFixture = messageFixtures.find(
({ mid }) =>
JSON.stringify(toArray(mid)) === JSON.stringify(message.mid),
);
expect(result).toEqualPayload({
...expectedFixture,
mid: toArray(expectedFixture?.mid),
sender,
recipient,
sentBy: user.id,
});
});
});
describe('findPageAndPopulate', () => {
const pageQuery = getPageQuery<Message>();
it('should find messages, and foreach message populate the corresponding sender and recipient', async () => {
jest.spyOn(messageRepository, 'findPageAndPopulate');
const result = await messageService.findPageAndPopulate({}, pageQuery);
const messagesWithSenderAndRecipient = allMessages.map((message) => ({
...message,
sender: allSubscribers.find(({ id }) => id === message.sender),
recipient: allSubscribers.find(({ id }) => id === message.recipient),
sentBy: allUsers.find(({ id }) => id === message.sentBy)?.id,
}));
expect(messageRepository.findPageAndPopulate).toHaveBeenCalledWith(
{},
pageQuery,
);
expect(result).toEqualPayload(messagesWithSenderAndRecipient);
});
});
describe('findHistoryUntilDate', () => {
it('should return history until given date', async () => {
const until: Date = new Date(
new Date().setMonth(new Date().getMonth() + 1),
);
const result = await messageService.findHistoryUntilDate(
sender!,
until,
30,
);
const historyMessages = messagesWithSenderAndRecipient.filter(
(message) => message.createdAt <= until,
);
expect(result).toEqualPayload(historyMessages);
});
});
describe('findHistorySinceDate', () => {
it('should return history since given date', async () => {
const since: Date = new Date();
const result = await messageService.findHistorySinceDate(
sender!,
since,
30,
);
const messagesWithSenderAndRecipient = allMessages.map((message) => ({
...message,
sender: allSubscribers.find(({ id }) => id === message.sender)?.id,
recipient: allSubscribers.find(({ id }) => id === message.recipient)
?.id,
sentBy: allUsers.find(({ id }) => id === message.sentBy)?.id,
}));
const historyMessages = messagesWithSenderAndRecipient.filter(
(message) => message.createdAt > since,
);
expect(result).toEqual(
historyMessages.sort((message1, message2) =>
sortRowsBy(message1, message2, 'createdAt', 'asc'),
),
);
});
});
});