ajout tests

This commit is contained in:
NouhailaAater 2025-02-22 00:20:37 -05:00
parent 5f87aa1b7a
commit 562fdfb791
5 changed files with 259 additions and 32 deletions

View file

@ -0,0 +1,17 @@
import { RoomType } from "../../Types/RoomType";
const room: RoomType = {
_id: '123',
userId: '456',
title: 'Test Room',
created_at: '2025-02-21T00:00:00Z'
};
describe('RoomType', () => {
test('creates a room with _id, userId, title, and created_at', () => {
expect(room._id).toBe('123');
expect(room.userId).toBe('456');
expect(room.title).toBe('Test Room');
expect(room.created_at).toBe('2025-02-21T00:00:00Z');
});
});

View file

@ -37,9 +37,10 @@ describe('WebSocketService', () => {
});
test('createRoom should emit create-room event', () => {
const roomName = 'Test Room';
WebsocketService.connect(ENV_VARIABLES.VITE_BACKEND_SOCKET_URL);
WebsocketService.createRoom();
expect(mockSocket.emit).toHaveBeenCalledWith('create-room');
WebsocketService.createRoom(roomName);
expect(mockSocket.emit).toHaveBeenCalledWith('create-room', roomName);
});
test('nextQuestion should emit next-question event with correct parameters', () => {

View file

@ -1,4 +1,5 @@
const Rooms = require('../models/room');
const ObjectId = require('mongodb').ObjectId;
describe('Rooms', () => {
let rooms;
@ -23,70 +24,281 @@ describe('Rooms', () => {
connect: jest.fn(),
getConnection: jest.fn().mockReturnThis(), // Add getConnection method
collection: jest.fn().mockReturnValue(collection),
};
};
rooms = new Rooms(db);
});
// Test for getRoomTitleByUserId
describe('getRoomTitleByUserId', () => {
it('should return the titles of all rooms for a given userId', async () => {
const userId = '678561cac3e923c972d2d930';
const roomsData = [
{ title: 'Salle 1', userId },
{ title: 'Salle 2', userId },
];
// create
describe('create', () => {
it('should create a new room and return the new room ID', async () => {
const title = 'Test Room';
collection.find().toArray.mockResolvedValue(roomsData);
// Mock the database response
collection.findOne.mockResolvedValue(null);
collection.insertOne.mockResolvedValue({ insertedId: new ObjectId() });
const result = await rooms.getRoomTitleByUserId(userId);
const result = await rooms.create(title, '12345');
expect(db.connect).toHaveBeenCalled();
expect(db.collection).toHaveBeenCalledWith('rooms');
expect(collection.find).toHaveBeenCalledWith({ userId });
expect(result).toEqual(['Salle 1', 'Salle 2']);
expect(collection.findOne).toHaveBeenCalledWith({ title, userId: '12345' });
expect(collection.insertOne).toHaveBeenCalledWith(expect.objectContaining({ title, userId: '12345' }));
expect(result).toBeDefined();
});
it('should return an empty array if no rooms are found for the given userId', async () => {
// throw an error if userId is undefined
it('should throw an error if userId is undefined', async () => {
const title = 'Test Room';
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 () => {
const title = 'Existing Room';
const userId = '66fc70bea1b9e87655cf17c9';
// Mock the database response of a found room
collection.findOne.mockResolvedValue(
// 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
describe('getUserRooms', () => {
it('should return all rooms for a user', async () => {
const userId = '12345';
const userRooms = [
{ title: 'Room 1', userId },
{ title: 'Room 2', userId },
];
collection.find().toArray.mockResolvedValue([]);
// Mock the database response
collection.find().toArray.mockResolvedValue(userRooms);
const result = await rooms.getRoomTitleByUserId(userId);
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);
});
});
// write a test for getContent
describe('getContent', () => {
it('should return the content of a room', async () => {
const roomId = '60c72b2f9b1d8b3a4c8e4d3b';
const content = [
{ title: 'Salle 1', content: [] },
{ title: 'Salle 2', content: [] },
];
// Mock the database response
collection.find().toArray.mockResolvedValue(content);
const result = await rooms.getContent(roomId);
expect(db.connect).toHaveBeenCalled();
expect(db.collection).toHaveBeenCalledWith('files');
expect(collection.find).toHaveBeenCalledWith({ roomId });
expect(result).toEqual(content);
});
it('should return an empty array if the room has no content', async () => {
const roomId = '60c72b2f9b1d8b3a4c8e4d3b';
// Mock the database response
collection.find().toArray.mockResolvedValue([]);
const result = await rooms.getContent(roomId);
expect(db.connect).toHaveBeenCalled();
expect(db.collection).toHaveBeenCalledWith('files');
expect(collection.find).toHaveBeenCalledWith({ roomId });
expect(result).toEqual([]);
});
});
describe('delete', () => {
it('should delete a room and return true', async () => {
const roomId = '60c72b2f9b1d8b3a4c8e4d3b';
// Test for roomExists
// 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 '${newTitle}' already exists.`);
expect(db.connect).toHaveBeenCalled();
expect(db.collection).toHaveBeenCalledWith('rooms');
// expect(collection.updateOne).toHaveBeenCalledWith({ _id: new ObjectId(roomId) }, { $set: { title: newTitle } });
expect(collection.findOne).toHaveBeenCalledWith({ userId: userId, title: newTitle });
});
});
describe('roomExists', () => {
it('should return true if the room exists', async () => {
const title = 'Numero Salle test 0';
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';
const userId = '12345';
it('should return false if the room does not exist', async () => {
const title = 'Non-existent 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(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) });
});
});
});

View file

@ -8,7 +8,6 @@ class RoomsController {
this.getRoomTitle = this.getRoomTitle.bind(this);
}
// In RoomsController.js
create = async (req, res, next) => {
try {
const { title } = req.body;
@ -25,7 +24,7 @@ create = async (req, res, next) => {
return res.status(200).json({
message: 'Room cree avec succes.',
roomId: result.insertedId // Ensure result contains the insertedId
roomId: result.insertedId
});
} catch (error) {
@ -100,7 +99,7 @@ create = async (req, res, next) => {
}
return res.status(200).json({
message: 'Salle supprim<EFBFBD> avec succ<63>s.'
message: 'Salle supprimé avec succès.'
});
} catch (error) {

View file

@ -1,9 +1,8 @@
const ObjectId = require('mongodb').ObjectId;
class Rooms {
constructor(db, quizModel) {
constructor(db) {
this.db = db;
this.quizModel = quizModel;
}
async create(title, userId) {
@ -76,7 +75,6 @@ class Rooms {
const roomResult = await roomsCollection.deleteOne({ _id: ObjectId.createFromHexString(roomId) });
if (roomResult.deletedCount != 1) return false;
await this.quizModel.deleteQuizzesByRoomId(roomId);
return true;
}