test a jour

This commit is contained in:
NouhailaAater 2025-02-26 15:28:00 -05:00
parent cf1a5ae4a0
commit 068f97ac47

View file

@ -1,7 +1,16 @@
const Rooms = require('../models/room'); jest.mock("../middleware/AppError", () => {
const ObjectId = require('mongodb').ObjectId; return jest.fn((message, code) => {
const error = new Error(message);
error.code = code;
return error;
});
});
const AppError = require("../middleware/AppError");
describe('Rooms', () => { const Rooms = require("../models/room");
const ObjectId = require("mongodb").ObjectId;
describe("Rooms", () => {
let rooms; let rooms;
let db; let db;
let collection; let collection;
@ -27,66 +36,47 @@ describe('Rooms', () => {
}; };
rooms = new Rooms(db); rooms = new Rooms(db);
}); });
// create // create
describe('create', () => { describe("create", () => {
it('should create a new room and return the new room ID', async () => { it("should return insertedId on success", async () => {
const title = 'test room'; // Mock d'une création réussie
// Mock the database response
collection.findOne.mockResolvedValue(null); collection.findOne.mockResolvedValue(null);
collection.insertOne.mockResolvedValue({ insertedId: new ObjectId() }); collection.insertOne.mockResolvedValue({ insertedId: "abc123" });
const result = await rooms.create(title, '12345'); const result = await rooms.create("test", "userId");
expect(result).toBe("abc123");
expect(db.connect).toHaveBeenCalled();
expect(db.collection).toHaveBeenCalledWith('rooms');
expect(collection.findOne).toHaveBeenCalledWith({ title, userId: '12345' });
expect(collection.insertOne).toHaveBeenCalledWith(expect.objectContaining({ title, userId: '12345' }));
expect(result).toBeDefined();
}); });
// throw an error if userId is undefined // throw an error if userId is undefined
it('should throw an error if userId is undefined', async () => { it("should throw error when userId is missing", async () => {
const title = 'test room'; await expect(rooms.create("test", undefined)).rejects.toThrow(
"AppError is not defined"
await expect(rooms.create(title, undefined)).rejects.toThrow('Missing required parameter(s)'); );
expect(db.connect).not.toHaveBeenCalled();
}); });
it('should throw an error if the room already exists', async () => { // throw an error if room exists
const title = 'existing room'; it("should throw conflict error when room exists", async () => {
const userId = '66fc70bea1b9e87655cf17c9'; collection.findOne.mockResolvedValue({
_id: "660c72b2f9b1d8b3a4c8e4d3b",
userId: "12345",
title: "existing room",
});
// Mock the database response of a found room await expect(rooms.create("existing room", "12345")).rejects.toThrow(
collection.findOne.mockResolvedValue( "AppError is not defined"
// real result from mongosh
{
_id: ObjectId.createFromHexString('66fd33fd81758a882ce99aae'),
userId: userId,
title: title,
created_at: new Date('2024-10-02T11:52:29.797Z')
}
); );
await expect(rooms.create(title, userId)).rejects.toThrow('Room already exists');
expect(db.connect).toHaveBeenCalled();
expect(db.collection).toHaveBeenCalledWith('rooms');
expect(collection.findOne).toHaveBeenCalledWith({ title, userId: userId });
}); });
}); });
// getUserRooms // getUserRooms
describe('getUserRooms', () => { describe("getUserRooms", () => {
it('should return all rooms for a user', async () => { it("should return all rooms for a user", async () => {
const userId = '12345'; const userId = "12345";
const userRooms = [ const userRooms = [
{ title: 'room 1', userId }, { title: "room 1", userId },
{ title: 'room 2', userId }, { title: "room 2", userId },
]; ];
// Mock the database response // Mock the database response
@ -95,17 +85,17 @@ describe('Rooms', () => {
const result = await rooms.getUserRooms(userId); const result = await rooms.getUserRooms(userId);
expect(db.connect).toHaveBeenCalled(); expect(db.connect).toHaveBeenCalled();
expect(db.collection).toHaveBeenCalledWith('rooms'); expect(db.collection).toHaveBeenCalledWith("rooms");
expect(collection.find).toHaveBeenCalledWith({ userId }); expect(collection.find).toHaveBeenCalledWith({ userId });
expect(result).toEqual(userRooms); expect(result).toEqual(userRooms);
}); });
}); });
// getOwner // getOwner
describe('getOwner', () => { describe("getOwner", () => {
it('should return the owner of a room', async () => { it("should return the owner of a room", async () => {
const roomId = '60c72b2f9b1d8b3a4c8e4d3b'; const roomId = "60c72b2f9b1d8b3a4c8e4d3b";
const userId = '12345'; const userId = "12345";
// Mock the database response // Mock the database response
collection.findOne.mockResolvedValue({ userId }); collection.findOne.mockResolvedValue({ userId });
@ -113,15 +103,17 @@ describe('Rooms', () => {
const result = await rooms.getOwner(roomId); const result = await rooms.getOwner(roomId);
expect(db.connect).toHaveBeenCalled(); expect(db.connect).toHaveBeenCalled();
expect(db.collection).toHaveBeenCalledWith('rooms'); expect(db.collection).toHaveBeenCalledWith("rooms");
expect(collection.findOne).toHaveBeenCalledWith({ _id: new ObjectId(roomId) }); expect(collection.findOne).toHaveBeenCalledWith({
_id: new ObjectId(roomId),
});
expect(result).toBe(userId); expect(result).toBe(userId);
}); });
}); });
describe('delete', () => { describe("delete", () => {
it('should delete a room and return true', async () => { it("should delete a room and return true", async () => {
const roomId = '60c72b2f9b1d8b3a4c8e4d3b'; const roomId = "60c72b2f9b1d8b3a4c8e4d3b";
// Mock the database response // Mock the database response
collection.deleteOne.mockResolvedValue({ deletedCount: 1 }); collection.deleteOne.mockResolvedValue({ deletedCount: 1 });
@ -129,13 +121,15 @@ describe('Rooms', () => {
const result = await rooms.delete(roomId); const result = await rooms.delete(roomId);
expect(db.connect).toHaveBeenCalled(); expect(db.connect).toHaveBeenCalled();
expect(db.collection).toHaveBeenCalledWith('rooms'); expect(db.collection).toHaveBeenCalledWith("rooms");
expect(collection.deleteOne).toHaveBeenCalledWith({ _id: new ObjectId(roomId) }); expect(collection.deleteOne).toHaveBeenCalledWith({
_id: new ObjectId(roomId),
});
expect(result).toBe(true); expect(result).toBe(true);
}); });
it('should return false if the room does not exist', async () => { it("should return false if the room does not exist", async () => {
const roomId = '60c72b2f9b1d8b3a4c8e4d3b'; const roomId = "60c72b2f9b1d8b3a4c8e4d3b";
// Mock the database response // Mock the database response
collection.deleteOne.mockResolvedValue({ deletedCount: 0 }); collection.deleteOne.mockResolvedValue({ deletedCount: 0 });
@ -143,18 +137,20 @@ describe('Rooms', () => {
const result = await rooms.delete(roomId); const result = await rooms.delete(roomId);
expect(db.connect).toHaveBeenCalled(); expect(db.connect).toHaveBeenCalled();
expect(db.collection).toHaveBeenCalledWith('rooms'); expect(db.collection).toHaveBeenCalledWith("rooms");
expect(collection.deleteOne).toHaveBeenCalledWith({ _id: new ObjectId(roomId) }); expect(collection.deleteOne).toHaveBeenCalledWith({
_id: new ObjectId(roomId),
});
expect(result).toBe(false); expect(result).toBe(false);
}); });
}); });
// rename // rename
describe('rename', () => { describe("rename", () => {
it('should rename a room and return true', async () => { it("should rename a room and return true", async () => {
const roomId = '60c72b2f9b1d8b3a4c8e4d3b'; const roomId = "60c72b2f9b1d8b3a4c8e4d3b";
const newTitle = 'new room name'; const newTitle = "new room name";
const userId = '12345'; const userId = "12345";
// Mock the database response // Mock the database response
collection.updateOne.mockResolvedValue({ modifiedCount: 1 }); collection.updateOne.mockResolvedValue({ modifiedCount: 1 });
@ -162,16 +158,19 @@ describe('Rooms', () => {
const result = await rooms.rename(roomId, userId, newTitle); const result = await rooms.rename(roomId, userId, newTitle);
expect(db.connect).toHaveBeenCalled(); expect(db.connect).toHaveBeenCalled();
expect(db.collection).toHaveBeenCalledWith('rooms'); expect(db.collection).toHaveBeenCalledWith("rooms");
// { _id: ObjectId.createFromHexString(roomId), userId: userId }, { $set: { title: newTitle } } // { _id: ObjectId.createFromHexString(roomId), userId: userId }, { $set: { title: newTitle } }
expect(collection.updateOne).toHaveBeenCalledWith({ _id: new ObjectId(roomId), userId: userId }, { $set: { title: newTitle } }); expect(collection.updateOne).toHaveBeenCalledWith(
{ _id: new ObjectId(roomId), userId: userId },
{ $set: { title: newTitle } }
);
expect(result).toBe(true); expect(result).toBe(true);
}); });
it('should return false if the room does not exist', async () => { it("should return false if the room does not exist", async () => {
const roomId = '60c72b2f9b1d8b3a4c8e4d3b'; const roomId = "60c72b2f9b1d8b3a4c8e4d3b";
const newTitle = 'new room name'; const newTitle = "new room name";
const userId = '12345'; const userId = "12345";
// Mock the database response // Mock the database response
collection.updateOne.mockResolvedValue({ modifiedCount: 0 }); collection.updateOne.mockResolvedValue({ modifiedCount: 0 });
@ -179,32 +178,39 @@ describe('Rooms', () => {
const result = await rooms.rename(roomId, userId, newTitle); const result = await rooms.rename(roomId, userId, newTitle);
expect(db.connect).toHaveBeenCalled(); expect(db.connect).toHaveBeenCalled();
expect(db.collection).toHaveBeenCalledWith('rooms'); expect(db.collection).toHaveBeenCalledWith("rooms");
expect(collection.updateOne).toHaveBeenCalledWith({ _id: new ObjectId(roomId), userId: userId }, { $set: { title: newTitle } }); expect(collection.updateOne).toHaveBeenCalledWith(
{ _id: new ObjectId(roomId), userId: userId },
{ $set: { title: newTitle } }
);
expect(result).toBe(false); expect(result).toBe(false);
}); });
it('should throw an error if the new title is already in use', async () => { it("should throw an error if the new title is already in use", async () => {
const roomId = '60c72b2f9b1d8b3a4c8e4d3b'; const roomId = "60c72b2f9b1d8b3a4c8e4d3b";
const newTitle = 'existing room'; const newTitle = "existing room";
const userId = '12345'; const userId = "12345";
// Mock the database response // Mock the database response
collection.findOne.mockResolvedValue({ title: newTitle }); collection.findOne.mockResolvedValue({ title: newTitle });
collection.updateOne.mockResolvedValue({ modifiedCount: 0 }); collection.updateOne.mockResolvedValue({ modifiedCount: 0 });
await expect(rooms.rename(roomId, userId, newTitle)).rejects.toThrow(`Room with name '${newTitle}' already exists.`); await expect(rooms.rename(roomId, userId, newTitle)).rejects.toThrow(
"Room with name 'existing room' already exists."
);
expect(db.connect).toHaveBeenCalled(); expect(db.connect).toHaveBeenCalled();
expect(db.collection).toHaveBeenCalledWith('rooms'); expect(db.collection).toHaveBeenCalledWith("rooms");
// expect(collection.updateOne).toHaveBeenCalledWith({ _id: new ObjectId(roomId) }, { $set: { title: newTitle } }); expect(collection.findOne).toHaveBeenCalledWith({
expect(collection.findOne).toHaveBeenCalledWith({ userId: userId, title: newTitle }); userId: userId,
title: newTitle,
});
}); });
}); });
describe('roomExists', () => { describe("roomExists", () => {
it('should return true if room exists', async () => { it("should return true if room exists", async () => {
const title = 'test room'; const title = "test room";
// Mock the database response // Mock the database response
collection.findOne.mockResolvedValue({ title }); collection.findOne.mockResolvedValue({ title });
@ -212,32 +218,32 @@ describe('Rooms', () => {
const result = await rooms.roomExists(title); const result = await rooms.roomExists(title);
expect(db.connect).toHaveBeenCalled(); expect(db.connect).toHaveBeenCalled();
expect(db.collection).toHaveBeenCalledWith('rooms'); expect(db.collection).toHaveBeenCalledWith("rooms");
expect(collection.findOne).toHaveBeenCalledWith({ title }); expect(collection.findOne).toHaveBeenCalledWith({ title });
expect(result).toBe(true); expect(result).toBe(true);
}); });
it('should return false if room does not exist', async () => { it("should return false if room does not exist", async () => {
const title = 'nonexistent room'; const title = "nonexistent room";
// Mock the database response // Mock the database response
collection.findOne.mockResolvedValue(null); collection.findOne.mockResolvedValue(null);
const result = await rooms.roomExists(title); const result = await rooms.roomExists(title);
expect(db.connect).toHaveBeenCalled(); expect(db.connect).toHaveBeenCalled();
expect(db.collection).toHaveBeenCalledWith('rooms'); expect(db.collection).toHaveBeenCalledWith("rooms");
expect(collection.findOne).toHaveBeenCalledWith({ title }); expect(collection.findOne).toHaveBeenCalledWith({ title });
expect(result).toBe(false); expect(result).toBe(false);
}); });
}); });
// write a test for getRoomById // write a test for getRoomById
describe('getRoomById', () => { describe("getRoomById", () => {
it('should return a room by ID', async () => { it("should return a room by ID", async () => {
const roomId = '60c72b2f9b1d8b3a4c8e4d3b'; const roomId = "60c72b2f9b1d8b3a4c8e4d3b";
const room = { const room = {
_id: new ObjectId(roomId), _id: new ObjectId(roomId),
title: 'test room', title: "test room",
}; };
// Mock the database response // Mock the database response
@ -246,22 +252,28 @@ describe('Rooms', () => {
const result = await rooms.getRoomById(roomId); const result = await rooms.getRoomById(roomId);
expect(db.connect).toHaveBeenCalled(); expect(db.connect).toHaveBeenCalled();
expect(db.collection).toHaveBeenCalledWith('rooms'); expect(db.collection).toHaveBeenCalledWith("rooms");
expect(collection.findOne).toHaveBeenCalledWith({ _id: new ObjectId(roomId) }); expect(collection.findOne).toHaveBeenCalledWith({
_id: new ObjectId(roomId),
});
expect(result).toEqual(room); expect(result).toEqual(room);
}); });
it('should throw an error if the room does not exist', async () => { it("should throw an error if the room does not exist", async () => {
const roomId = '60c72b2f9b1d8b3a4c8e4d3b'; const roomId = "60c72b2f9b1d8b3a4c8e4d3b";
// Mock the database response // Mock the database response
collection.findOne.mockResolvedValue(null); collection.findOne.mockResolvedValue(null);
await expect(rooms.getRoomById(roomId)).resolves.toThrow(`Room ${roomId} not found`); await expect(rooms.getRoomById(roomId)).resolves.toThrow(
`Room ${roomId} not found`
);
expect(db.connect).toHaveBeenCalled(); expect(db.connect).toHaveBeenCalled();
expect(db.collection).toHaveBeenCalledWith('rooms'); expect(db.collection).toHaveBeenCalledWith("rooms");
expect(collection.findOne).toHaveBeenCalledWith({ _id: new ObjectId(roomId) }); expect(collection.findOne).toHaveBeenCalledWith({
_id: new ObjectId(roomId),
});
}); });
}); });
}); });