From a3f5aa5a0f8292d220a7545e3ba79a5d835092f8 Mon Sep 17 00:00:00 2001 From: phucd5 Date: Wed, 15 Nov 2023 21:34:09 -0500 Subject: [PATCH] finished message tests --- server/__tests__/controllers/messageTest.js | 72 +++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/server/__tests__/controllers/messageTest.js b/server/__tests__/controllers/messageTest.js index 4b1094f..af31d07 100644 --- a/server/__tests__/controllers/messageTest.js +++ b/server/__tests__/controllers/messageTest.js @@ -5,8 +5,10 @@ import { postMessage, incrementLikes, changeVisibility, + deleteMessage, } from "../../controllers/message.js"; +import UserModel from "../../models/User.js"; import MessageModel from "../../models/Message.js"; import httpMocks from "node-mocks-http"; jest.mock("../../models/User"); @@ -216,3 +218,73 @@ describe("changeVisibility", () => { expect(res._getData()).toContain("Internal Server Error"); }); }); + +describe("deleteMessage", () => { + it("should successfully delete a message", async () => { + const mockMessageId = "messageId"; + const mockMessage = { _id: mockMessageId }; + const mockUser = { + messages: [mockMessageId], + save: jest.fn().mockResolvedValue({}), + messages: { + remove: jest.fn(), + }, + }; + + MessageModel.findById = jest.fn().mockResolvedValue(mockMessage); + MessageModel.findByIdAndDelete = jest + .fn() + .mockResolvedValue(mockMessage); + UserModel.findOne = jest.fn().mockResolvedValue(mockUser); + + const req = httpMocks.createRequest({ + body: { messageId: mockMessageId }, + }); + const res = httpMocks.createResponse(); + + await deleteMessage(req, res); + + expect(MessageModel.findById).toHaveBeenCalledWith(mockMessageId); + expect(UserModel.findOne).toHaveBeenCalledWith({ + messages: mockMessageId, + }); + + expect(mockUser.messages.remove).toHaveBeenCalledWith(mockMessageId); + expect(mockUser.save).toHaveBeenCalled(); + expect(MessageModel.findByIdAndDelete).toHaveBeenCalledWith( + mockMessageId + ); + + expect(res.statusCode).toBe(200); + const responseData = JSON.parse(res._getData()); + expect(responseData).toEqual(mockMessage); + }); + it("should return 404 if the message is not found", async () => { + MessageModel.findById.mockResolvedValue(null); + + const req = httpMocks.createRequest({ + body: { messageId: "nonexistentMessageId" }, + }); + const res = httpMocks.createResponse(); + + await deleteMessage(req, res); + + expect(res.statusCode).toBe(404); + expect(res._getData()).toContain("Message not found"); + }); + it("should handle server errors", async () => { + MessageModel.findById.mockImplementationOnce(() => { + throw new Error("Internal Server Error"); + }); + + const req = httpMocks.createRequest({ + body: { messageId: "messageId" }, + }); + const res = httpMocks.createResponse(); + + await deleteMessage(req, res); + + expect(res.statusCode).toBe(500); + expect(res._getData()).toContain("Internal Server Error"); + }); +});