jest.mock("../middleware/AppError", () => { return jest.fn((message, code) => { const error = new Error(message); error.code = code; return error; }); }); const AppError = require("../middleware/AppError"); const Rooms = require("../models/room"); const ObjectId = require("mongodb").ObjectId; describe("Rooms", () => { let rooms; let db; let collection; beforeEach(() => { jest.clearAllMocks(); // Clear any previous mock calls // Mock the collection object collection = { findOne: jest.fn(), insertOne: jest.fn(), find: jest.fn().mockReturnValue({ toArray: jest.fn() }), // Mock the find method deleteOne: jest.fn(), deleteMany: jest.fn(), updateOne: jest.fn(), }; // Mock the database connection db = { connect: jest.fn(), getConnection: jest.fn().mockReturnThis(), // Add getConnection method collection: jest.fn().mockReturnValue(collection), }; rooms = new Rooms(db); }); // create describe("create", () => { it("should return insertedId on success", async () => { // Mock d'une création réussie collection.findOne.mockResolvedValue(null); collection.insertOne.mockResolvedValue({ insertedId: "abc123" }); const result = await rooms.create("test", "userId"); expect(result).toBe("abc123"); }); // throw an error if userId is undefined it("should throw error when userId is missing", async () => { await expect(rooms.create("test", undefined)).rejects.toThrow( "AppError is not defined" ); }); // throw an error if room exists it("should throw conflict error when room exists", async () => { collection.findOne.mockResolvedValue({ _id: "660c72b2f9b1d8b3a4c8e4d3b", userId: "12345", title: "existing room", }); await expect(rooms.create("existing room", "12345")).rejects.toThrow( "AppError is not defined" ); }); }); // getUserRooms describe("getUserRooms", () => { it("should return all rooms for a user", async () => { const userId = "12345"; const userRooms = [ { title: "room 1", userId }, { title: "room 2", userId }, ]; // Mock the database response collection.find().toArray.mockResolvedValue(userRooms); const result = await rooms.getUserRooms(userId); expect(db.connect).toHaveBeenCalled(); expect(db.collection).toHaveBeenCalledWith("rooms"); expect(collection.find).toHaveBeenCalledWith({ userId }); expect(result).toEqual(userRooms); }); }); // getOwner describe("getOwner", () => { it("should return the owner of a room", async () => { const roomId = "60c72b2f9b1d8b3a4c8e4d3b"; const userId = "12345"; // Mock the database response collection.findOne.mockResolvedValue({ userId }); const result = await rooms.getOwner(roomId); expect(db.connect).toHaveBeenCalled(); expect(db.collection).toHaveBeenCalledWith("rooms"); expect(collection.findOne).toHaveBeenCalledWith({ _id: new ObjectId(roomId), }); expect(result).toBe(userId); }); }); describe("delete", () => { it("should delete a room and return true", async () => { const roomId = "60c72b2f9b1d8b3a4c8e4d3b"; // Mock the database response collection.deleteOne.mockResolvedValue({ deletedCount: 1 }); const result = await rooms.delete(roomId); expect(db.connect).toHaveBeenCalled(); expect(db.collection).toHaveBeenCalledWith("rooms"); expect(collection.deleteOne).toHaveBeenCalledWith({ _id: new ObjectId(roomId), }); expect(result).toBe(true); }); it("should return false if the room does not exist", async () => { const roomId = "60c72b2f9b1d8b3a4c8e4d3b"; // Mock the database response collection.deleteOne.mockResolvedValue({ deletedCount: 0 }); const result = await rooms.delete(roomId); expect(db.connect).toHaveBeenCalled(); expect(db.collection).toHaveBeenCalledWith("rooms"); expect(collection.deleteOne).toHaveBeenCalledWith({ _id: new ObjectId(roomId), }); expect(result).toBe(false); }); }); // rename describe("rename", () => { it("should rename a room and return true", async () => { const roomId = "60c72b2f9b1d8b3a4c8e4d3b"; const newTitle = "new room name"; const userId = "12345"; // Mock the database response collection.updateOne.mockResolvedValue({ modifiedCount: 1 }); const result = await rooms.rename(roomId, userId, newTitle); expect(db.connect).toHaveBeenCalled(); expect(db.collection).toHaveBeenCalledWith("rooms"); // { _id: ObjectId.createFromHexString(roomId), userId: userId }, { $set: { title: newTitle } } expect(collection.updateOne).toHaveBeenCalledWith( { _id: new ObjectId(roomId), userId: userId }, { $set: { title: newTitle } } ); expect(result).toBe(true); }); it("should return false if the room does not exist", async () => { const roomId = "60c72b2f9b1d8b3a4c8e4d3b"; const newTitle = "new room name"; const userId = "12345"; // Mock the database response collection.updateOne.mockResolvedValue({ modifiedCount: 0 }); const result = await rooms.rename(roomId, userId, newTitle); expect(db.connect).toHaveBeenCalled(); expect(db.collection).toHaveBeenCalledWith("rooms"); expect(collection.updateOne).toHaveBeenCalledWith( { _id: new ObjectId(roomId), userId: userId }, { $set: { title: newTitle } } ); expect(result).toBe(false); }); it("should throw an error if the new title is already in use", async () => { const roomId = "60c72b2f9b1d8b3a4c8e4d3b"; const newTitle = "existing room"; const userId = "12345"; // Mock the database response collection.findOne.mockResolvedValue({ title: newTitle }); collection.updateOne.mockResolvedValue({ modifiedCount: 0 }); await expect(rooms.rename(roomId, userId, newTitle)).rejects.toThrow( "Room with name 'existing room' already exists." ); expect(db.connect).toHaveBeenCalled(); expect(db.collection).toHaveBeenCalledWith("rooms"); expect(collection.findOne).toHaveBeenCalledWith({ userId: userId, title: newTitle, }); }); }); describe("roomExists", () => { it("should return true if room exists", async () => { const title = "test room"; // Mock the database response collection.findOne.mockResolvedValue({ title }); const result = await rooms.roomExists(title); expect(db.connect).toHaveBeenCalled(); expect(db.collection).toHaveBeenCalledWith("rooms"); expect(collection.findOne).toHaveBeenCalledWith({ title }); expect(result).toBe(true); }); it("should return false if room does not exist", async () => { const title = "nonexistent room"; // Mock the database response collection.findOne.mockResolvedValue(null); const result = await rooms.roomExists(title); expect(db.connect).toHaveBeenCalled(); expect(db.collection).toHaveBeenCalledWith("rooms"); expect(collection.findOne).toHaveBeenCalledWith({ title }); expect(result).toBe(false); }); }); // write a test for getRoomById describe("getRoomById", () => { it("should return a room by ID", async () => { const roomId = "60c72b2f9b1d8b3a4c8e4d3b"; const room = { _id: new ObjectId(roomId), title: "test room", }; // Mock the database response collection.findOne.mockResolvedValue(room); const result = await rooms.getRoomById(roomId); expect(db.connect).toHaveBeenCalled(); expect(db.collection).toHaveBeenCalledWith("rooms"); expect(collection.findOne).toHaveBeenCalledWith({ _id: new ObjectId(roomId), }); expect(result).toEqual(room); }); it("should throw an error if the room does not exist", async () => { const roomId = "60c72b2f9b1d8b3a4c8e4d3b"; // Mock the database response collection.findOne.mockResolvedValue(null); await expect(rooms.getRoomById(roomId)).resolves.toThrow( `Room ${roomId} not found` ); expect(db.connect).toHaveBeenCalled(); expect(db.collection).toHaveBeenCalledWith("rooms"); expect(collection.findOne).toHaveBeenCalledWith({ _id: new ObjectId(roomId), }); }); }); });