add test for Folder.create

found a bug with return new Error instead of throw new Error (!)
This commit is contained in:
C. Fuhrman 2024-10-03 13:17:14 -04:00
parent b1d1173768
commit ee5ffa432b
7 changed files with 50 additions and 13 deletions

View file

@ -17,6 +17,7 @@ describe('Folders', () => {
find: jest.fn().mockReturnValue({ toArray: jest.fn() }), // Mock the find method
};
// Mock the database connection
db = {
connect: jest.fn(),
@ -29,6 +30,47 @@ describe('Folders', () => {
});
// create
describe('create', () => {
it('should create a new folder and return the new folder ID', async () => {
const title = 'Test Folder';
// Mock the database response
collection.findOne.mockResolvedValue(null);
collection.insertOne.mockResolvedValue({ insertedId: new ObjectId() });
const result = await folders.create(title, '12345');
expect(db.connect).toHaveBeenCalled();
expect(db.collection).toHaveBeenCalledWith('folders');
expect(collection.findOne).toHaveBeenCalledWith({ title, userId: '12345' });
expect(collection.insertOne).toHaveBeenCalledWith(expect.objectContaining({ title, userId: '12345' }));
expect(result).toBeDefined();
});
it('should throw an error if the folder already exists', async () => {
const title = 'Existing Folder';
const userId = '66fc70bea1b9e87655cf17c9';
// Mock the database response of a found folder
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(folders.create(title, userId)).rejects.toThrow('Folder already exists');
expect(db.connect).toHaveBeenCalled();
expect(db.collection).toHaveBeenCalledWith('folders');
expect(collection.findOne).toHaveBeenCalledWith({ title, userId: userId });
});
});
describe('folderExists', () => {
it('should return true if folder exists', async () => {
const title = 'Test Folder';
@ -37,9 +79,6 @@ describe('Folders', () => {
// Mock the database response
collection.findOne.mockResolvedValue({ title, userId });
// Spy on console.log
const consoleSpy = jest.spyOn(console, 'log');
const result = await folders.folderExists(title, userId);
expect(db.connect).toHaveBeenCalled();

View file

@ -7,8 +7,6 @@ class FoldersController {
constructor(foldersModel) {
this.folders = foldersModel;
// this.quizzes = quizModel;
console.log("FoldersController constructor: folders", this.folders);
}
/***

View file

@ -5,7 +5,6 @@ class ImagesController {
constructor(imagesModel) {
this.images = imagesModel;
console.log("ImagesController constructor: images", this.images);
}
upload = async (req, res, next) => {

View file

@ -7,9 +7,7 @@ class QuizController {
constructor(quizModel, foldersModel) {
this.folders = foldersModel;
console.log("QuizController constructor: folders", this.folders);
this.quizzes = quizModel;
console.log("QuizController constructor: quizzes", this.quizzes);
}
create = async (req, res, next) => {

View file

@ -9,7 +9,6 @@ class UsersController {
constructor(userModel) {
this.users = userModel;
console.log("UsersController constructor: users", this.users);
}
register = async (req, res, next) => {

View file

@ -15,7 +15,12 @@ class Folders {
const existingFolder = await foldersCollection.findOne({ title: title, userId: userId });
if (existingFolder) return new Error('Folder already exists');
console.log(`Folders.create: existingFolder`, existingFolder);
if (existingFolder) {
console.log('Folder already exists, throwing Error');
throw new Error('Folder already exists');
}
const newFolder = {
userId: userId,
@ -50,6 +55,7 @@ class Folders {
return folder.userId;
}
// finds all quizzes in a folder
async getContent(folderId) {
await this.db.connect()
const conn = this.db.getConnection();
@ -110,8 +116,6 @@ class Folders {
for (const quiz of sourceFolder.content) {
const { title, content } = quiz;
//console.log(title);
//console.log(content);
await this.quizModel.create(title, content, newFolderId.toString(), userId);
}

View file

@ -9,7 +9,7 @@ const setupWebsocket = (io) => {
console.log("Connection limit reached. Disconnecting client.");
socket.emit(
"join-failure",
"Le nombre maximum de connexion a été atteint"
"Le nombre maximum de connexions a été atteint"
);
socket.disconnect(true);
return;