Compare commits

..

No commits in common. "e6a3af5838c03bc0721e3e104d36d4f65d47cdb4" and "e2d4582ccf059ff2af2a86b00d36b59666bff52f" have entirely different histories.

View file

@ -6,8 +6,7 @@ console.log('db:', db); // Debugging line
console.log('db.getConnection:', db.getConnection); // Debugging line console.log('db.getConnection:', db.getConnection); // Debugging line
describe('Folders', () => { describe('Folders', () => {
const quiz = new Quiz(db); const folder = new Folder(db, new Quiz(db));
const folder = new Folder(db, quiz);
let database; let database;
beforeAll(async () => { beforeAll(async () => {
@ -65,120 +64,155 @@ describe('Folders', () => {
}); });
}); });
// beforeEach(() => {
// jest.clearAllMocks(); // Clear any previous mock calls
// getUserFolders // // Mock the collection object
describe('getUserFolders', () => { // collection = {
it('should return all folders for a user', async () => { // findOne: jest.fn(),
const userId = '12345'; // insertOne: jest.fn(),
const userFolders = [ // find: jest.fn().mockReturnValue({ toArray: jest.fn() }), // Mock the find method
{ title: 'Folder 1', userId }, // deleteOne: jest.fn(),
{ title: 'Folder 2', userId }, // deleteMany: jest.fn(),
]; // updateOne: jest.fn(),
// Ensure the folders do not exist before the test // };
await database.collection('folders').deleteMany({ userId });
// Insert the folders
await database.collection('folders').insertMany(userFolders);
const result = await folder.getUserFolders(userId); // // Mock the database connection
// db = {
expect(result).toEqual(userFolders); // connect: jest.fn(),
// getConnection: jest.fn().mockReturnThis(), // Add getConnection method
// collection: jest.fn().mockReturnValue(collection),
// };
// Clean up // quizzes = new Quizzes(db);
await database.collection('folders').deleteMany({ userId }); // folders = new Folders(db, quizzes);
}); // });
});
// getOwner // // create
describe('getOwner', () => { // describe('create', () => {
it('should return the owner of a folder', async () => { // it('should create a new folder and return the new folder ID', async () => {
// userId must be 24-char hex string // const title = 'Test Folder';
const userId = '60c72b2f9b1d8b3a4c8e4d3b';
expect(userId.length).toBe(24); // // Mock the database response
expect(/^[0-9A-Fa-f]{24}$/.test(userId)).toBe(true); // collection.findOne.mockResolvedValue(null);
// collection.insertOne.mockResolvedValue({ insertedId: new ObjectId() });
// create the folder using the folder API // const result = await folders.create(title, '12345');
const folderId = await folder.create('Test Folder', userId);
const folderIdString = folderId.toString(); // 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();
// });
expect(folderIdString.length).toBe(24); // // throw an error if userId is undefined
expect(/^[0-9A-Fa-f]{24}$/.test(folderIdString)).toBe(true); // it('should throw an error if userId is undefined', async () => {
// const title = 'Test Folder';
const result = await folder.getOwner(folderIdString); // await expect(folders.create(title, undefined)).rejects.toThrow('Missing required parameter(s)');
expect(result).toBe(userId); // expect(db.connect).not.toHaveBeenCalled();
}); // });
});
// write a test for getContent // it('should throw an error if the folder already exists', async () => {
describe('getContent', () => { // const title = 'Existing Folder';
it('should return the content of a folder', async () => { // const userId = '66fc70bea1b9e87655cf17c9';
// create a new folder // // Mock the database response of a found folder
const folderId = await folder.create('Test Folder', '12345'); // 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')
// }
// );
// Insert the content using the quiz API // await expect(folders.create(title, userId)).rejects.toThrow('Folder already exists');
const _quiz1ObjectId = await quiz.create('Quiz 1', [], folderId.toString(), '12345');
const _quiz2ObjectId = await quiz.create('Quiz 2', [], folderId.toString(), '12345');
const content = [ // expect(db.connect).toHaveBeenCalled();
{ title: 'Quiz 1', content: [], _id: _quiz1ObjectId, created_at: expect.any(Date), updated_at: expect.any(Date), folderId: folderId.toString(), userId: '12345' }, // expect(db.collection).toHaveBeenCalledWith('folders');
{ title: 'Quiz 2', content: [], _id: _quiz2ObjectId, created_at: expect.any(Date), updated_at: expect.any(Date), folderId: folderId.toString(), userId: '12345' }, // expect(collection.findOne).toHaveBeenCalledWith({ title, userId: userId });
]; // });
// });
const result = await folder.getContent(folderId.toString()); // // getUserFolders
// describe('getUserFolders', () => {
// it('should return all folders for a user', async () => {
// const userId = '12345';
// const userFolders = [
// { title: 'Folder 1', userId },
// { title: 'Folder 2', userId },
// ];
expect(result).toEqual(content); // // Mock the database response
// collection.find().toArray.mockResolvedValue(userFolders);
// Clean up // const result = await folders.getUserFolders(userId);
await database.collection('files').deleteMany({ folderId: folderId.toString() });
await database.collection('folders').deleteMany({ _id: folderId });
});
it('should return an empty array if the folder has no content', async () => { // expect(db.connect).toHaveBeenCalled();
// create a new folder // expect(db.collection).toHaveBeenCalledWith('folders');
const folderId = await folder.create('Test Folder', '12345'); // expect(collection.find).toHaveBeenCalledWith({ userId });
// expect(result).toEqual(userFolders);
// });
// });
const content = []; // // getOwner
const result = await folder.getContent(folderId.toString()); // describe('getOwner', () => {
// it('should return the owner of a folder', async () => {
// const folderId = '60c72b2f9b1d8b3a4c8e4d3b';
// const userId = '12345';
expect(result).toEqual(content); // // Mock the database response
// collection.findOne.mockResolvedValue({ userId });
// Clean up // const result = await folders.getOwner(folderId);
await database.collection('folders').deleteMany({ _id: folderId });
});
});
// delete // expect(db.connect).toHaveBeenCalled();
describe('delete', () => { // expect(db.collection).toHaveBeenCalledWith('folders');
it('should delete a folder and return true', async () => { // expect(collection.findOne).toHaveBeenCalledWith({ _id: new ObjectId(folderId) });
const folderId = await folder.create('Test Folder', '12345'); // expect(result).toBe(userId);
// });
// });
// Insert the content using the quiz API // // write a test for getContent
const _quiz1ObjectId = await quiz.create('Quiz 1', [], folderId.toString(), '12345'); // describe('getContent', () => {
const _quiz2ObjectId = await quiz.create('Quiz 2', [], folderId.toString(), '12345'); // it('should return the content of a folder', async () => {
// const folderId = '60c72b2f9b1d8b3a4c8e4d3b';
// const content = [
// { title: 'Quiz 1', content: [] },
// { title: 'Quiz 2', content: [] },
// ];
const result = await folder.delete(folderId.toString()); // // Mock the database response
// collection.find().toArray.mockResolvedValue(content);
expect(result).toBe(true); // const result = await folders.getContent(folderId);
// make sure quizzes were deleted // expect(db.connect).toHaveBeenCalled();
const quiz1 = await database.collection('files').findOne({ _id: _quiz1ObjectId }); // expect(db.collection).toHaveBeenCalledWith('files');
const quiz2 = await database.collection('files').findOne({ _id: _quiz2ObjectId }); // expect(collection.find).toHaveBeenCalledWith({ folderId });
// expect(result).toEqual(content);
// });
expect(quiz1).toBeNull(); // it('should return an empty array if the folder has no content', async () => {
expect(quiz2).toBeNull(); // const folderId = '60c72b2f9b1d8b3a4c8e4d3b';
// Clean up // // Mock the database response
await database.collection('files').deleteMany({ folderId: folderId.toString() }); // collection.find().toArray.mockResolvedValue([]);
await database.collection('folders').deleteMany({ _id: folderId });
}); // const result = await folders.getContent(folderId);
// expect(db.connect).toHaveBeenCalled();
// expect(db.collection).toHaveBeenCalledWith('files');
// expect(collection.find).toHaveBeenCalledWith({ folderId });
// expect(result).toEqual([]);
// });
// });
});
// // delete // // delete
// describe('delete', () => { // describe('delete', () => {
// it('should delete a folder and return true', async () => { // it('should delete a folder and return true', async () => {