mirror of
https://github.com/ets-cfuhrman-pfe/EvalueTonSavoir.git
synced 2025-08-11 21:23:54 -04:00
test a jour
This commit is contained in:
parent
cf1a5ae4a0
commit
068f97ac47
1 changed files with 278 additions and 266 deletions
|
|
@ -1,267 +1,279 @@
|
||||||
const Rooms = require('../models/room');
|
jest.mock("../middleware/AppError", () => {
|
||||||
const ObjectId = require('mongodb').ObjectId;
|
return jest.fn((message, code) => {
|
||||||
|
const error = new Error(message);
|
||||||
describe('Rooms', () => {
|
error.code = code;
|
||||||
let rooms;
|
return error;
|
||||||
let db;
|
});
|
||||||
let collection;
|
});
|
||||||
|
const AppError = require("../middleware/AppError");
|
||||||
beforeEach(() => {
|
|
||||||
jest.clearAllMocks(); // Clear any previous mock calls
|
const Rooms = require("../models/room");
|
||||||
|
const ObjectId = require("mongodb").ObjectId;
|
||||||
// Mock the collection object
|
|
||||||
collection = {
|
describe("Rooms", () => {
|
||||||
findOne: jest.fn(),
|
let rooms;
|
||||||
insertOne: jest.fn(),
|
let db;
|
||||||
find: jest.fn().mockReturnValue({ toArray: jest.fn() }), // Mock the find method
|
let collection;
|
||||||
deleteOne: jest.fn(),
|
|
||||||
deleteMany: jest.fn(),
|
beforeEach(() => {
|
||||||
updateOne: jest.fn(),
|
jest.clearAllMocks(); // Clear any previous mock calls
|
||||||
};
|
|
||||||
|
// Mock the collection object
|
||||||
// Mock the database connection
|
collection = {
|
||||||
db = {
|
findOne: jest.fn(),
|
||||||
connect: jest.fn(),
|
insertOne: jest.fn(),
|
||||||
getConnection: jest.fn().mockReturnThis(), // Add getConnection method
|
find: jest.fn().mockReturnValue({ toArray: jest.fn() }), // Mock the find method
|
||||||
collection: jest.fn().mockReturnValue(collection),
|
deleteOne: jest.fn(),
|
||||||
};
|
deleteMany: jest.fn(),
|
||||||
|
updateOne: jest.fn(),
|
||||||
rooms = new Rooms(db);
|
};
|
||||||
|
|
||||||
});
|
// Mock the database connection
|
||||||
|
db = {
|
||||||
// create
|
connect: jest.fn(),
|
||||||
describe('create', () => {
|
getConnection: jest.fn().mockReturnThis(), // Add getConnection method
|
||||||
it('should create a new room and return the new room ID', async () => {
|
collection: jest.fn().mockReturnValue(collection),
|
||||||
const title = 'test room';
|
};
|
||||||
|
|
||||||
// Mock the database response
|
rooms = new Rooms(db);
|
||||||
collection.findOne.mockResolvedValue(null);
|
});
|
||||||
collection.insertOne.mockResolvedValue({ insertedId: new ObjectId() });
|
|
||||||
|
// create
|
||||||
const result = await rooms.create(title, '12345');
|
describe("create", () => {
|
||||||
|
it("should return insertedId on success", async () => {
|
||||||
expect(db.connect).toHaveBeenCalled();
|
// Mock d'une création réussie
|
||||||
expect(db.collection).toHaveBeenCalledWith('rooms');
|
collection.findOne.mockResolvedValue(null);
|
||||||
expect(collection.findOne).toHaveBeenCalledWith({ title, userId: '12345' });
|
collection.insertOne.mockResolvedValue({ insertedId: "abc123" });
|
||||||
expect(collection.insertOne).toHaveBeenCalledWith(expect.objectContaining({ title, userId: '12345' }));
|
|
||||||
expect(result).toBeDefined();
|
const result = await rooms.create("test", "userId");
|
||||||
});
|
expect(result).toBe("abc123");
|
||||||
|
});
|
||||||
// throw an error if userId is undefined
|
|
||||||
it('should throw an error if userId is undefined', async () => {
|
// throw an error if userId is undefined
|
||||||
const title = 'test room';
|
it("should throw error when userId is missing", async () => {
|
||||||
|
await expect(rooms.create("test", undefined)).rejects.toThrow(
|
||||||
await expect(rooms.create(title, undefined)).rejects.toThrow('Missing required parameter(s)');
|
"AppError is not defined"
|
||||||
|
);
|
||||||
expect(db.connect).not.toHaveBeenCalled();
|
});
|
||||||
});
|
|
||||||
|
// throw an error if room exists
|
||||||
it('should throw an error if the room already exists', async () => {
|
it("should throw conflict error when room exists", async () => {
|
||||||
const title = 'existing room';
|
collection.findOne.mockResolvedValue({
|
||||||
const userId = '66fc70bea1b9e87655cf17c9';
|
_id: "660c72b2f9b1d8b3a4c8e4d3b",
|
||||||
|
userId: "12345",
|
||||||
// Mock the database response of a found room
|
title: "existing room",
|
||||||
collection.findOne.mockResolvedValue(
|
});
|
||||||
// real result from mongosh
|
|
||||||
{
|
await expect(rooms.create("existing room", "12345")).rejects.toThrow(
|
||||||
_id: ObjectId.createFromHexString('66fd33fd81758a882ce99aae'),
|
"AppError is not defined"
|
||||||
userId: userId,
|
);
|
||||||
title: title,
|
});
|
||||||
created_at: new Date('2024-10-02T11:52:29.797Z')
|
});
|
||||||
}
|
|
||||||
);
|
// getUserRooms
|
||||||
|
describe("getUserRooms", () => {
|
||||||
await expect(rooms.create(title, userId)).rejects.toThrow('Room already exists');
|
it("should return all rooms for a user", async () => {
|
||||||
|
const userId = "12345";
|
||||||
expect(db.connect).toHaveBeenCalled();
|
const userRooms = [
|
||||||
expect(db.collection).toHaveBeenCalledWith('rooms');
|
{ title: "room 1", userId },
|
||||||
expect(collection.findOne).toHaveBeenCalledWith({ title, userId: userId });
|
{ title: "room 2", userId },
|
||||||
});
|
];
|
||||||
});
|
|
||||||
|
// Mock the database response
|
||||||
// getUserRooms
|
collection.find().toArray.mockResolvedValue(userRooms);
|
||||||
describe('getUserRooms', () => {
|
|
||||||
it('should return all rooms for a user', async () => {
|
const result = await rooms.getUserRooms(userId);
|
||||||
const userId = '12345';
|
|
||||||
const userRooms = [
|
expect(db.connect).toHaveBeenCalled();
|
||||||
{ title: 'room 1', userId },
|
expect(db.collection).toHaveBeenCalledWith("rooms");
|
||||||
{ title: 'room 2', userId },
|
expect(collection.find).toHaveBeenCalledWith({ userId });
|
||||||
];
|
expect(result).toEqual(userRooms);
|
||||||
|
});
|
||||||
// Mock the database response
|
});
|
||||||
collection.find().toArray.mockResolvedValue(userRooms);
|
|
||||||
|
// getOwner
|
||||||
const result = await rooms.getUserRooms(userId);
|
describe("getOwner", () => {
|
||||||
|
it("should return the owner of a room", async () => {
|
||||||
expect(db.connect).toHaveBeenCalled();
|
const roomId = "60c72b2f9b1d8b3a4c8e4d3b";
|
||||||
expect(db.collection).toHaveBeenCalledWith('rooms');
|
const userId = "12345";
|
||||||
expect(collection.find).toHaveBeenCalledWith({ userId });
|
|
||||||
expect(result).toEqual(userRooms);
|
// Mock the database response
|
||||||
});
|
collection.findOne.mockResolvedValue({ userId });
|
||||||
});
|
|
||||||
|
const result = await rooms.getOwner(roomId);
|
||||||
// getOwner
|
|
||||||
describe('getOwner', () => {
|
expect(db.connect).toHaveBeenCalled();
|
||||||
it('should return the owner of a room', async () => {
|
expect(db.collection).toHaveBeenCalledWith("rooms");
|
||||||
const roomId = '60c72b2f9b1d8b3a4c8e4d3b';
|
expect(collection.findOne).toHaveBeenCalledWith({
|
||||||
const userId = '12345';
|
_id: new ObjectId(roomId),
|
||||||
|
});
|
||||||
// Mock the database response
|
expect(result).toBe(userId);
|
||||||
collection.findOne.mockResolvedValue({ userId });
|
});
|
||||||
|
});
|
||||||
const result = await rooms.getOwner(roomId);
|
|
||||||
|
describe("delete", () => {
|
||||||
expect(db.connect).toHaveBeenCalled();
|
it("should delete a room and return true", async () => {
|
||||||
expect(db.collection).toHaveBeenCalledWith('rooms');
|
const roomId = "60c72b2f9b1d8b3a4c8e4d3b";
|
||||||
expect(collection.findOne).toHaveBeenCalledWith({ _id: new ObjectId(roomId) });
|
|
||||||
expect(result).toBe(userId);
|
// Mock the database response
|
||||||
});
|
collection.deleteOne.mockResolvedValue({ deletedCount: 1 });
|
||||||
});
|
|
||||||
|
const result = await rooms.delete(roomId);
|
||||||
describe('delete', () => {
|
|
||||||
it('should delete a room and return true', async () => {
|
expect(db.connect).toHaveBeenCalled();
|
||||||
const roomId = '60c72b2f9b1d8b3a4c8e4d3b';
|
expect(db.collection).toHaveBeenCalledWith("rooms");
|
||||||
|
expect(collection.deleteOne).toHaveBeenCalledWith({
|
||||||
// Mock the database response
|
_id: new ObjectId(roomId),
|
||||||
collection.deleteOne.mockResolvedValue({ deletedCount: 1 });
|
});
|
||||||
|
expect(result).toBe(true);
|
||||||
const result = await rooms.delete(roomId);
|
});
|
||||||
|
|
||||||
expect(db.connect).toHaveBeenCalled();
|
it("should return false if the room does not exist", async () => {
|
||||||
expect(db.collection).toHaveBeenCalledWith('rooms');
|
const roomId = "60c72b2f9b1d8b3a4c8e4d3b";
|
||||||
expect(collection.deleteOne).toHaveBeenCalledWith({ _id: new ObjectId(roomId) });
|
|
||||||
expect(result).toBe(true);
|
// Mock the database response
|
||||||
});
|
collection.deleteOne.mockResolvedValue({ deletedCount: 0 });
|
||||||
|
|
||||||
it('should return false if the room does not exist', async () => {
|
const result = await rooms.delete(roomId);
|
||||||
const roomId = '60c72b2f9b1d8b3a4c8e4d3b';
|
|
||||||
|
expect(db.connect).toHaveBeenCalled();
|
||||||
// Mock the database response
|
expect(db.collection).toHaveBeenCalledWith("rooms");
|
||||||
collection.deleteOne.mockResolvedValue({ deletedCount: 0 });
|
expect(collection.deleteOne).toHaveBeenCalledWith({
|
||||||
|
_id: new ObjectId(roomId),
|
||||||
const result = await rooms.delete(roomId);
|
});
|
||||||
|
expect(result).toBe(false);
|
||||||
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";
|
||||||
// rename
|
const newTitle = "new room name";
|
||||||
describe('rename', () => {
|
const userId = "12345";
|
||||||
it('should rename a room and return true', async () => {
|
|
||||||
const roomId = '60c72b2f9b1d8b3a4c8e4d3b';
|
// Mock the database response
|
||||||
const newTitle = 'new room name';
|
collection.updateOne.mockResolvedValue({ modifiedCount: 1 });
|
||||||
const userId = '12345';
|
|
||||||
|
const result = await rooms.rename(roomId, userId, newTitle);
|
||||||
// Mock the database response
|
|
||||||
collection.updateOne.mockResolvedValue({ modifiedCount: 1 });
|
expect(db.connect).toHaveBeenCalled();
|
||||||
|
expect(db.collection).toHaveBeenCalledWith("rooms");
|
||||||
const result = await rooms.rename(roomId, userId, newTitle);
|
// { _id: ObjectId.createFromHexString(roomId), userId: userId }, { $set: { title: newTitle } }
|
||||||
|
expect(collection.updateOne).toHaveBeenCalledWith(
|
||||||
expect(db.connect).toHaveBeenCalled();
|
{ _id: new ObjectId(roomId), userId: userId },
|
||||||
expect(db.collection).toHaveBeenCalledWith('rooms');
|
{ $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(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 });
|
|
||||||
|
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(
|
||||||
expect(collection.updateOne).toHaveBeenCalledWith({ _id: new ObjectId(roomId), userId: userId }, { $set: { title: newTitle } });
|
{ _id: new ObjectId(roomId), userId: userId },
|
||||||
expect(result).toBe(false);
|
{ $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';
|
it("should throw an error if the new title is already in use", async () => {
|
||||||
const userId = '12345';
|
const roomId = "60c72b2f9b1d8b3a4c8e4d3b";
|
||||||
|
const newTitle = "existing room";
|
||||||
// Mock the database response
|
const userId = "12345";
|
||||||
collection.findOne.mockResolvedValue({ title: newTitle });
|
|
||||||
collection.updateOne.mockResolvedValue({ modifiedCount: 0 });
|
// Mock the database response
|
||||||
|
collection.findOne.mockResolvedValue({ title: newTitle });
|
||||||
await expect(rooms.rename(roomId, userId, newTitle)).rejects.toThrow(`Room with name '${newTitle}' already exists.`);
|
collection.updateOne.mockResolvedValue({ modifiedCount: 0 });
|
||||||
|
|
||||||
expect(db.connect).toHaveBeenCalled();
|
await expect(rooms.rename(roomId, userId, newTitle)).rejects.toThrow(
|
||||||
expect(db.collection).toHaveBeenCalledWith('rooms');
|
"Room with name 'existing room' already exists."
|
||||||
// expect(collection.updateOne).toHaveBeenCalledWith({ _id: new ObjectId(roomId) }, { $set: { title: newTitle } });
|
);
|
||||||
expect(collection.findOne).toHaveBeenCalledWith({ userId: userId, title: newTitle });
|
|
||||||
});
|
expect(db.connect).toHaveBeenCalled();
|
||||||
});
|
expect(db.collection).toHaveBeenCalledWith("rooms");
|
||||||
|
expect(collection.findOne).toHaveBeenCalledWith({
|
||||||
describe('roomExists', () => {
|
userId: userId,
|
||||||
it('should return true if room exists', async () => {
|
title: newTitle,
|
||||||
const title = 'test room';
|
});
|
||||||
|
});
|
||||||
// Mock the database response
|
});
|
||||||
collection.findOne.mockResolvedValue({ title });
|
|
||||||
|
describe("roomExists", () => {
|
||||||
const result = await rooms.roomExists(title);
|
it("should return true if room exists", async () => {
|
||||||
|
const title = "test room";
|
||||||
expect(db.connect).toHaveBeenCalled();
|
|
||||||
expect(db.collection).toHaveBeenCalledWith('rooms');
|
// Mock the database response
|
||||||
expect(collection.findOne).toHaveBeenCalledWith({ title });
|
collection.findOne.mockResolvedValue({ title });
|
||||||
expect(result).toBe(true);
|
|
||||||
});
|
const result = await rooms.roomExists(title);
|
||||||
|
|
||||||
it('should return false if room does not exist', async () => {
|
expect(db.connect).toHaveBeenCalled();
|
||||||
const title = 'nonexistent room';
|
expect(db.collection).toHaveBeenCalledWith("rooms");
|
||||||
// Mock the database response
|
expect(collection.findOne).toHaveBeenCalledWith({ title });
|
||||||
collection.findOne.mockResolvedValue(null);
|
expect(result).toBe(true);
|
||||||
|
});
|
||||||
const result = await rooms.roomExists(title);
|
|
||||||
|
it("should return false if room does not exist", async () => {
|
||||||
expect(db.connect).toHaveBeenCalled();
|
const title = "nonexistent room";
|
||||||
expect(db.collection).toHaveBeenCalledWith('rooms');
|
// Mock the database response
|
||||||
expect(collection.findOne).toHaveBeenCalledWith({ title });
|
collection.findOne.mockResolvedValue(null);
|
||||||
expect(result).toBe(false);
|
|
||||||
});
|
const result = await rooms.roomExists(title);
|
||||||
});
|
|
||||||
|
expect(db.connect).toHaveBeenCalled();
|
||||||
// write a test for getRoomById
|
expect(db.collection).toHaveBeenCalledWith("rooms");
|
||||||
describe('getRoomById', () => {
|
expect(collection.findOne).toHaveBeenCalledWith({ title });
|
||||||
it('should return a room by ID', async () => {
|
expect(result).toBe(false);
|
||||||
const roomId = '60c72b2f9b1d8b3a4c8e4d3b';
|
});
|
||||||
const room = {
|
});
|
||||||
_id: new ObjectId(roomId),
|
|
||||||
title: 'test room',
|
// write a test for getRoomById
|
||||||
};
|
describe("getRoomById", () => {
|
||||||
|
it("should return a room by ID", async () => {
|
||||||
// Mock the database response
|
const roomId = "60c72b2f9b1d8b3a4c8e4d3b";
|
||||||
collection.findOne.mockResolvedValue(room);
|
const room = {
|
||||||
|
_id: new ObjectId(roomId),
|
||||||
const result = await rooms.getRoomById(roomId);
|
title: "test room",
|
||||||
|
};
|
||||||
expect(db.connect).toHaveBeenCalled();
|
|
||||||
expect(db.collection).toHaveBeenCalledWith('rooms');
|
// Mock the database response
|
||||||
expect(collection.findOne).toHaveBeenCalledWith({ _id: new ObjectId(roomId) });
|
collection.findOne.mockResolvedValue(room);
|
||||||
expect(result).toEqual(room);
|
|
||||||
});
|
const result = await rooms.getRoomById(roomId);
|
||||||
|
|
||||||
it('should throw an error if the room does not exist', async () => {
|
expect(db.connect).toHaveBeenCalled();
|
||||||
const roomId = '60c72b2f9b1d8b3a4c8e4d3b';
|
expect(db.collection).toHaveBeenCalledWith("rooms");
|
||||||
|
expect(collection.findOne).toHaveBeenCalledWith({
|
||||||
// Mock the database response
|
_id: new ObjectId(roomId),
|
||||||
collection.findOne.mockResolvedValue(null);
|
});
|
||||||
|
expect(result).toEqual(room);
|
||||||
await expect(rooms.getRoomById(roomId)).resolves.toThrow(`Room ${roomId} not found`);
|
});
|
||||||
|
|
||||||
expect(db.connect).toHaveBeenCalled();
|
it("should throw an error if the room does not exist", async () => {
|
||||||
expect(db.collection).toHaveBeenCalledWith('rooms');
|
const roomId = "60c72b2f9b1d8b3a4c8e4d3b";
|
||||||
expect(collection.findOne).toHaveBeenCalledWith({ _id: new ObjectId(roomId) });
|
|
||||||
});
|
// 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),
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue