Compare commits

..

3 commits

Author SHA1 Message Date
C. Fuhrman
e6a3af5838 delete test passes 2025-03-14 13:03:53 -04:00
C. Fuhrman
b20f33500e getContent test passes 2025-03-14 13:00:47 -04:00
C. Fuhrman
ea656fe9c7 getOwner test 2025-03-14 12:44:43 -04:00

View file

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