mirror of
https://github.com/ets-cfuhrman-pfe/EvalueTonSavoir.git
synced 2025-08-11 21:23:54 -04:00
fixed server tests
This commit is contained in:
parent
d9480e2d14
commit
4e0d5d778d
3 changed files with 107 additions and 58 deletions
|
|
@ -2,7 +2,7 @@
|
||||||
const ENV_VARIABLES = {
|
const ENV_VARIABLES = {
|
||||||
MODE: process.env.MODE || "production",
|
MODE: process.env.MODE || "production",
|
||||||
VITE_BACKEND_URL: process.env.VITE_BACKEND_URL || "",
|
VITE_BACKEND_URL: process.env.VITE_BACKEND_URL || "",
|
||||||
IMG_URL: typeof import.meta !== "undefined" ? import.meta.env.VITE_IMG_URL || "http://localhost" : process.env.VITE_IMG_URL || "http://localhost",
|
IMG_URL: process.env.MODE == "development" ? process.env.VITE_BACKEND_URL : process.env.IMG_URL,
|
||||||
BACKEND_URL: process.env.SITE_URL != undefined ? `${process.env.SITE_URL}${process.env.USE_PORTS ? `:${process.env.BACKEND_PORT}`:''}` : process.env.VITE_BACKEND_URL || '',
|
BACKEND_URL: process.env.SITE_URL != undefined ? `${process.env.SITE_URL}${process.env.USE_PORTS ? `:${process.env.BACKEND_PORT}`:''}` : process.env.VITE_BACKEND_URL || '',
|
||||||
FRONTEND_URL: process.env.SITE_URL != undefined ? `${process.env.SITE_URL}${process.env.USE_PORTS ? `:${process.env.PORT}`:''}` : ''
|
FRONTEND_URL: process.env.SITE_URL != undefined ? `${process.env.SITE_URL}${process.env.USE_PORTS ? `:${process.env.PORT}`:''}` : ''
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -27,8 +27,7 @@
|
||||||
"noUnusedLocals": true,
|
"noUnusedLocals": true,
|
||||||
"noUnusedParameters": true,
|
"noUnusedParameters": true,
|
||||||
"noFallthroughCasesInSwitch": true,
|
"noFallthroughCasesInSwitch": true,
|
||||||
"esModuleInterop": true,
|
"esModuleInterop": true
|
||||||
"types": ["vite/client"]
|
|
||||||
},
|
},
|
||||||
// "exclude": [
|
// "exclude": [
|
||||||
// // "src/components/GiftTemplate/**/*",
|
// // "src/components/GiftTemplate/**/*",
|
||||||
|
|
|
||||||
|
|
@ -68,6 +68,17 @@ describe.skip("GET /get", () => {
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
jest.mock('mongodb', () => {
|
||||||
|
const originalModule = jest.requireActual('mongodb');
|
||||||
|
return {
|
||||||
|
...originalModule,
|
||||||
|
ObjectId: {
|
||||||
|
...originalModule.ObjectId,
|
||||||
|
createFromHexString: jest.fn().mockReturnValue('507f191e810c19729de860ea'), // Return a valid 24-character ObjectId string
|
||||||
|
},
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
describe('Images', () => {
|
describe('Images', () => {
|
||||||
let db;
|
let db;
|
||||||
let images;
|
let images;
|
||||||
|
|
@ -77,23 +88,40 @@ describe('Images', () => {
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
|
|
||||||
mockFindCursor = {
|
const mockImagesCursor = {
|
||||||
sort: jest.fn().mockReturnThis(),
|
sort: jest.fn().mockReturnThis(),
|
||||||
skip: jest.fn().mockReturnThis(),
|
skip: jest.fn().mockReturnThis(),
|
||||||
limit: jest.fn().mockReturnThis(),
|
limit: jest.fn().mockReturnThis(),
|
||||||
toArray: jest.fn(),
|
toArray: jest.fn()
|
||||||
|
};
|
||||||
|
|
||||||
|
const mockFilesCursor = {
|
||||||
|
sort: jest.fn().mockReturnThis(),
|
||||||
|
skip: jest.fn().mockReturnThis(),
|
||||||
|
limit: jest.fn().mockReturnThis(),
|
||||||
|
toArray: jest.fn()
|
||||||
};
|
};
|
||||||
|
|
||||||
mockImagesCollection = {
|
mockImagesCollection = {
|
||||||
insertOne: jest.fn().mockResolvedValue({ insertedId: 'image123' }),
|
insertOne: jest.fn().mockResolvedValue({ insertedId: 'image123' }),
|
||||||
findOne: jest.fn(),
|
findOne: jest.fn(),
|
||||||
find: jest.fn().mockReturnValue(mockFindCursor),
|
find: jest.fn().mockReturnValue(mockImagesCursor),
|
||||||
countDocuments: jest.fn(),
|
countDocuments: jest.fn(),
|
||||||
deleteOne: jest.fn(),
|
deleteOne: jest.fn()
|
||||||
|
};
|
||||||
|
|
||||||
|
mockFilesCollection = {
|
||||||
|
find: jest.fn().mockReturnValue(mockFilesCursor)
|
||||||
};
|
};
|
||||||
|
|
||||||
dbConn = {
|
dbConn = {
|
||||||
collection: jest.fn().mockReturnValue(mockImagesCollection)
|
collection: jest.fn((name) => {
|
||||||
|
if (name === 'images') {
|
||||||
|
return mockImagesCollection;
|
||||||
|
} else if (name === 'files') {
|
||||||
|
return mockFilesCollection;
|
||||||
|
}
|
||||||
|
})
|
||||||
};
|
};
|
||||||
|
|
||||||
db = {
|
db = {
|
||||||
|
|
@ -166,8 +194,16 @@ describe('Images', () => {
|
||||||
];
|
];
|
||||||
|
|
||||||
mockImagesCollection.countDocuments.mockResolvedValue(2);
|
mockImagesCollection.countDocuments.mockResolvedValue(2);
|
||||||
mockFindCursor.toArray.mockResolvedValue(mockImages);
|
// Create a mock cursor for images collection
|
||||||
|
const mockFindCursor = {
|
||||||
|
sort: jest.fn().mockReturnThis(),
|
||||||
|
skip: jest.fn().mockReturnThis(),
|
||||||
|
limit: jest.fn().mockReturnThis(),
|
||||||
|
toArray: jest.fn().mockResolvedValue(mockImages), // Return mock images when toArray is called
|
||||||
|
};
|
||||||
|
|
||||||
|
// Mock the find method to return the mock cursor
|
||||||
|
mockImagesCollection.find.mockReturnValue(mockFindCursor);
|
||||||
const result = await images.getImages(1, 10);
|
const result = await images.getImages(1, 10);
|
||||||
|
|
||||||
expect(db.connect).toHaveBeenCalled();
|
expect(db.connect).toHaveBeenCalled();
|
||||||
|
|
@ -188,7 +224,6 @@ describe('Images', () => {
|
||||||
|
|
||||||
it('should return an empty array if no images are found', async () => {
|
it('should return an empty array if no images are found', async () => {
|
||||||
mockImagesCollection.countDocuments.mockResolvedValue(0);
|
mockImagesCollection.countDocuments.mockResolvedValue(0);
|
||||||
mockFindCursor.toArray.mockResolvedValue([]);
|
|
||||||
|
|
||||||
const result = await images.getImages(1, 10);
|
const result = await images.getImages(1, 10);
|
||||||
|
|
||||||
|
|
@ -244,62 +279,77 @@ describe('Images', () => {
|
||||||
expect(mockImagesCollection.countDocuments).toHaveBeenCalledWith({ userId: 'user123' });
|
expect(mockImagesCollection.countDocuments).toHaveBeenCalledWith({ userId: 'user123' });
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('delete', () => {
|
describe('delete', () => {
|
||||||
|
it('should not delete the image when it exists in the files collection', async () => {
|
||||||
it('should delete the image when it exists', async () => {
|
|
||||||
const uid = 'user123';
|
const uid = 'user123';
|
||||||
const imgId = 'img123';
|
const imgId = '507f191e810c19729de860ea'; // A valid 24-character ObjectId string
|
||||||
|
|
||||||
// Simulate the image being found in the collection
|
// Mock the files collection cursor to simulate an image found
|
||||||
mockImagesCollection.find.mockResolvedValue([{ _id: imgId }]);
|
const mockFilesCursor = {
|
||||||
|
toArray: jest.fn().mockResolvedValue([{ _id: imgId }]) // Image found
|
||||||
|
};
|
||||||
|
|
||||||
|
mockFilesCollection.find.mockReturnValue(mockFilesCursor);
|
||||||
|
mockImagesCollection.deleteOne.mockResolvedValue({ deletedCount: 0 });
|
||||||
|
|
||||||
|
const result = await images.delete(uid, imgId);
|
||||||
|
|
||||||
|
// Ensure the files collection is queried
|
||||||
|
expect(dbConn.collection).toHaveBeenCalledWith('files');
|
||||||
|
expect(mockFilesCollection.find).toHaveBeenCalledWith({
|
||||||
|
userId: uid,
|
||||||
|
content: { $regex: new RegExp(`/api/image/get/${imgId}`) },
|
||||||
|
});
|
||||||
|
|
||||||
|
// Ensure the images collection is queried for deletion
|
||||||
|
expect(dbConn.collection).toHaveBeenCalledWith('files');
|
||||||
|
expect(mockImagesCollection.deleteOne).not.toHaveBeenCalledWith({
|
||||||
|
_id: ObjectId.createFromHexString(imgId), // Ensure the ObjectId is created correctly
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(result).toEqual({ deleted: false });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should delete the image if not found in the files collection', async () => {
|
||||||
|
const uid = 'user123';
|
||||||
|
const imgId = '507f191e810c19729de860ea';
|
||||||
|
|
||||||
|
// Mock the files collection cursor to simulate the image not being found
|
||||||
|
const mockFindCursor = {
|
||||||
|
toArray: jest.fn().mockResolvedValue([]) // Empty array means image not found
|
||||||
|
};
|
||||||
|
|
||||||
|
mockFilesCollection.find.mockReturnValue(mockFindCursor);
|
||||||
mockImagesCollection.deleteOne.mockResolvedValue({ deletedCount: 1 });
|
mockImagesCollection.deleteOne.mockResolvedValue({ deletedCount: 1 });
|
||||||
|
|
||||||
const result = await images.delete(uid, imgId);
|
const result = await images.delete(uid, imgId);
|
||||||
|
|
||||||
expect(db.connect).toHaveBeenCalled();
|
// Ensure the deleteOne is not called if the image is not found
|
||||||
expect(db.getConnection).toHaveBeenCalled();
|
expect(mockImagesCollection.deleteOne).toHaveBeenCalled();
|
||||||
expect(mockImagesCollection.find).toHaveBeenCalledWith({
|
|
||||||
userId: uid,
|
|
||||||
content: { $regex: new RegExp(`/api/image/get/${imgId}`) },
|
|
||||||
});
|
|
||||||
expect(mockImagesCollection.deleteOne).toHaveBeenCalledWith({ _id: imgId });
|
|
||||||
expect(result).toEqual({ deleted: true });
|
expect(result).toEqual({ deleted: true });
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should not delete the image when it does not exist', async () => {
|
it('should return false if the delete operation fails in the images collection', async () => {
|
||||||
const uid = 'user123';
|
const uid = 'user123';
|
||||||
const imgId = 'img123';
|
const imgId = '507f191e810c19729de860ea';
|
||||||
|
|
||||||
// Simulate the image not being found in the collection
|
// Mock the files collection cursor to simulate the image being found
|
||||||
mockImagesCollection.find.mockResolvedValue([]);
|
const mockFindCursor = {
|
||||||
|
toArray: jest.fn().mockResolvedValue([{ _id: imgId }]) // Image found
|
||||||
|
};
|
||||||
|
|
||||||
|
mockFilesCollection.find.mockReturnValue(mockFindCursor);
|
||||||
|
mockImagesCollection.deleteOne.mockResolvedValue({ deletedCount: 0 }); // Simulate failure
|
||||||
|
|
||||||
const result = await images.delete(uid, imgId);
|
const result = await images.delete(uid, imgId);
|
||||||
|
|
||||||
expect(db.connect).toHaveBeenCalled();
|
// Ensure the images collection deletion is called
|
||||||
expect(mockImagesCollection.find).toHaveBeenCalledWith({
|
expect(mockImagesCollection.deleteOne).not.toHaveBeenCalledWith({
|
||||||
userId: uid,
|
_id: ObjectId.createFromHexString(imgId), // Ensure the ObjectId is created correctly
|
||||||
content: { $regex: new RegExp(`/api/image/get/${imgId}`) },
|
|
||||||
});
|
});
|
||||||
expect(mockImagesCollection.deleteOne).toHaveBeenCalled();
|
|
||||||
expect(result).toEqual({ deleted: false });
|
expect(result).toEqual({ deleted: false });
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return false if the delete operation fails', async () => {
|
|
||||||
const uid = 'user123';
|
|
||||||
const imgId = 'img123';
|
|
||||||
|
|
||||||
// Simulate the image being found, but the delete operation failing
|
|
||||||
mockImagesCollection.find.mockResolvedValue([{ _id: imgId }]);
|
|
||||||
|
|
||||||
const result = await images.delete(uid, imgId);
|
|
||||||
|
|
||||||
expect(db.connect).toHaveBeenCalled();
|
|
||||||
expect(mockImagesCollection.find).toHaveBeenCalledWith({
|
|
||||||
userId: uid,
|
|
||||||
content: { $regex: new RegExp(`/api/image/get/${imgId}`) },
|
|
||||||
});
|
|
||||||
expect(result).toEqual({ deleted: false });
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
Loading…
Reference in a new issue