chore: writes test for existing upload deletion (#2103)

This commit is contained in:
Jacob Fletcher
2023-02-15 11:39:13 -05:00
committed by GitHub
parent 060c3805e5
commit e36f775273
3 changed files with 32 additions and 0 deletions

View File

@@ -181,6 +181,38 @@ describe('Collections - Uploads', () => {
expect(await fileExists(path.join(__dirname, './media', mediaDoc.sizes.icon.filename))).toBe(true);
});
it('should remove existing media on re-upload', async () => {
// Create temp file
const filePath = path.resolve(__dirname, './temp.png');
const file = await getFileByPath(filePath);
file.name = 'temp.png';
const mediaDoc = await payload.create({
collection: mediaSlug,
data: {},
file,
});
// Check that the temp file was created
expect(await fileExists(path.join(__dirname, './media', mediaDoc.filename))).toBe(true);
// Replace the temp file with a new one
const newFilePath = path.resolve(__dirname, './temp-renamed.png');
const newFile = await getFileByPath(newFilePath);
newFile.name = 'temp-renamed.png';
const updatedMediaDoc = await payload.update({
collection: mediaSlug,
id: mediaDoc.id,
file: newFile,
data: {},
});
// Check that the replacement file was created and the old one was removed
expect(await fileExists(path.join(__dirname, './media', updatedMediaDoc.filename))).toBe(true);
expect(await fileExists(path.join(__dirname, './media', mediaDoc.filename))).toBe(false);
});
it('should remove extra sizes on update', async () => {
const filePath = path.resolve(__dirname, './image.png');
const file = await getFileByPath(filePath);

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

BIN
test/uploads/temp.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB