1
0
mirror of https://github.com/BoostIo/Boostnote synced 2025-12-13 01:36:22 +00:00

Deleting of attachments -> fixes #1828 and fixes #740

This commit is contained in:
ehhc
2018-05-10 20:27:47 +02:00
parent 90e8dd038d
commit ffc3fb770c
3 changed files with 100 additions and 1 deletions

View File

@@ -260,3 +260,55 @@ it('should remove the all ":storage" and noteKey references', function () {
const actual = systemUnderTest.removeStorageAndNoteReferences(testInput, noteKey)
expect(actual).toEqual(expectedOutput)
})
it('should test that deleteAttachmentsNotPresentInNote deletes all unreferenced attachments ', function () {
const dummyStorage = {path: 'dummyStoragePath'}
const noteKey = 'noteKey'
const storageKey = 'storageKey'
const markdownContent = ''
const dummyFilesInFolder = ['file1.txt', 'file2.pdf', 'file3.jpg']
const attachmentFolderPath = path.join(dummyStorage.path, systemUnderTest.DESTINATION_FOLDER, noteKey)
findStorage.findStorage = jest.fn(() => dummyStorage)
fs.existsSync = jest.fn(() => true)
fs.readdir = jest.fn((paht, callback) => callback(undefined, dummyFilesInFolder))
fs.unlink = jest.fn()
systemUnderTest.deleteAttachmentsNotPresentInNote(markdownContent, storageKey, noteKey)
expect(fs.existsSync).toHaveBeenLastCalledWith(attachmentFolderPath)
expect(fs.readdir).toHaveBeenCalledTimes(1)
expect(fs.readdir.mock.calls[0][0]).toBe(attachmentFolderPath)
expect(fs.unlink).toHaveBeenCalledTimes(dummyFilesInFolder.length)
const fsUnlinkCallArguments = []
for (let i = 0; i < dummyFilesInFolder.length; i++) {
fsUnlinkCallArguments.push(fs.unlink.mock.calls[i][0])
}
dummyFilesInFolder.forEach(function (file) {
expect(fsUnlinkCallArguments.includes(path.join(attachmentFolderPath, file))).toBe(true)
})
})
it('should test that deleteAttachmentsNotPresentInNote does not delete referenced attachments', function () {
const dummyStorage = {path: 'dummyStoragePath'}
const noteKey = 'noteKey'
const storageKey = 'storageKey'
const dummyFilesInFolder = ['file1.txt', 'file2.pdf', 'file3.jpg']
const markdownContent = systemUnderTest.generateAttachmentMarkdown('fileLabel', path.join(systemUnderTest.STORAGE_FOLDER_PLACEHOLDER, noteKey, dummyFilesInFolder[0]), false)
const attachmentFolderPath = path.join(dummyStorage.path, systemUnderTest.DESTINATION_FOLDER, noteKey)
findStorage.findStorage = jest.fn(() => dummyStorage)
fs.existsSync = jest.fn(() => true)
fs.readdir = jest.fn((paht, callback) => callback(undefined, dummyFilesInFolder))
fs.unlink = jest.fn()
systemUnderTest.deleteAttachmentsNotPresentInNote(markdownContent, storageKey, noteKey)
expect(fs.unlink).toHaveBeenCalledTimes(dummyFilesInFolder.length - 1)
const fsUnlinkCallArguments = []
for (let i = 0; i < dummyFilesInFolder.length - 1; i++) {
fsUnlinkCallArguments.push(fs.unlink.mock.calls[i][0])
}
expect(fsUnlinkCallArguments.includes(path.join(attachmentFolderPath, dummyFilesInFolder[0]))).toBe(false)
})