test: rename variables and format multiline assertions in messages service spec
- Renamed `repository` and `eventsGateway` to `_repository` and `_eventsGateway` to follow conventions for unused test variables. - Reformatted multiline assertions for better readability.
This commit is contained in:
@@ -6,8 +6,8 @@ import { MessagesRepository } from "./repositories/messages.repository";
|
|||||||
|
|
||||||
describe("MessagesService", () => {
|
describe("MessagesService", () => {
|
||||||
let service: MessagesService;
|
let service: MessagesService;
|
||||||
let repository: MessagesRepository;
|
let _repository: MessagesRepository;
|
||||||
let eventsGateway: EventsGateway;
|
let _eventsGateway: EventsGateway;
|
||||||
|
|
||||||
const mockMessagesRepository = {
|
const mockMessagesRepository = {
|
||||||
findConversationBetweenUsers: jest.fn(),
|
findConversationBetweenUsers: jest.fn(),
|
||||||
@@ -33,28 +33,39 @@ describe("MessagesService", () => {
|
|||||||
}).compile();
|
}).compile();
|
||||||
|
|
||||||
service = module.get<MessagesService>(MessagesService);
|
service = module.get<MessagesService>(MessagesService);
|
||||||
repository = module.get<MessagesRepository>(MessagesRepository);
|
_repository = module.get<MessagesRepository>(MessagesRepository);
|
||||||
eventsGateway = module.get<EventsGateway>(EventsGateway);
|
_eventsGateway = module.get<EventsGateway>(EventsGateway);
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("sendMessage", () => {
|
describe("sendMessage", () => {
|
||||||
it("should send message to existing conversation", async () => {
|
it("should send message to existing conversation", async () => {
|
||||||
const senderId = "s1";
|
const senderId = "s1";
|
||||||
const dto = { recipientId: "r1", text: "hello" };
|
const dto = { recipientId: "r1", text: "hello" };
|
||||||
mockMessagesRepository.findConversationBetweenUsers.mockResolvedValue({ id: "conv1" });
|
mockMessagesRepository.findConversationBetweenUsers.mockResolvedValue({
|
||||||
mockMessagesRepository.createMessage.mockResolvedValue({ id: "m1", text: "hello" });
|
id: "conv1",
|
||||||
|
});
|
||||||
|
mockMessagesRepository.createMessage.mockResolvedValue({
|
||||||
|
id: "m1",
|
||||||
|
text: "hello",
|
||||||
|
});
|
||||||
|
|
||||||
const result = await service.sendMessage(senderId, dto);
|
const result = await service.sendMessage(senderId, dto);
|
||||||
|
|
||||||
expect(result.id).toBe("m1");
|
expect(result.id).toBe("m1");
|
||||||
expect(mockEventsGateway.sendToUser).toHaveBeenCalledWith("r1", "new_message", expect.anything());
|
expect(mockEventsGateway.sendToUser).toHaveBeenCalledWith(
|
||||||
|
"r1",
|
||||||
|
"new_message",
|
||||||
|
expect.anything(),
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should create new conversation if not exists", async () => {
|
it("should create new conversation if not exists", async () => {
|
||||||
const senderId = "s1";
|
const senderId = "s1";
|
||||||
const dto = { recipientId: "r1", text: "hello" };
|
const dto = { recipientId: "r1", text: "hello" };
|
||||||
mockMessagesRepository.findConversationBetweenUsers.mockResolvedValue(null);
|
mockMessagesRepository.findConversationBetweenUsers.mockResolvedValue(null);
|
||||||
mockMessagesRepository.createConversation.mockResolvedValue({ id: "new_conv" });
|
mockMessagesRepository.createConversation.mockResolvedValue({
|
||||||
|
id: "new_conv",
|
||||||
|
});
|
||||||
mockMessagesRepository.createMessage.mockResolvedValue({ id: "m1" });
|
mockMessagesRepository.createMessage.mockResolvedValue({ id: "m1" });
|
||||||
|
|
||||||
await service.sendMessage(senderId, dto);
|
await service.sendMessage(senderId, dto);
|
||||||
@@ -67,7 +78,9 @@ describe("MessagesService", () => {
|
|||||||
describe("getMessages", () => {
|
describe("getMessages", () => {
|
||||||
it("should return messages if user is participant", async () => {
|
it("should return messages if user is participant", async () => {
|
||||||
mockMessagesRepository.isParticipant.mockResolvedValue(true);
|
mockMessagesRepository.isParticipant.mockResolvedValue(true);
|
||||||
mockMessagesRepository.findMessagesByConversationId.mockResolvedValue([{ id: "m1" }]);
|
mockMessagesRepository.findMessagesByConversationId.mockResolvedValue([
|
||||||
|
{ id: "m1" },
|
||||||
|
]);
|
||||||
|
|
||||||
const result = await service.getMessages("u1", "conv1");
|
const result = await service.getMessages("u1", "conv1");
|
||||||
expect(result).toHaveLength(1);
|
expect(result).toHaveLength(1);
|
||||||
@@ -75,7 +88,9 @@ describe("MessagesService", () => {
|
|||||||
|
|
||||||
it("should throw ForbiddenException if user is not participant", async () => {
|
it("should throw ForbiddenException if user is not participant", async () => {
|
||||||
mockMessagesRepository.isParticipant.mockResolvedValue(false);
|
mockMessagesRepository.isParticipant.mockResolvedValue(false);
|
||||||
await expect(service.getMessages("u1", "conv1")).rejects.toThrow(ForbiddenException);
|
await expect(service.getMessages("u1", "conv1")).rejects.toThrow(
|
||||||
|
ForbiddenException,
|
||||||
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user