1
0
mirror of https://github.com/BoostIo/Boostnote synced 2025-12-13 17:56:25 +00:00

Merge branch 'master' into Issue_1900

This commit is contained in:
ehhc
2018-05-15 20:26:10 +02:00
committed by GitHub
29 changed files with 213 additions and 81 deletions

View File

@@ -274,3 +274,55 @@ it('should delete the correct attachment folder if a note is deleted', function
expect(findStorage.findStorage).toHaveBeenCalledWith(storageKey)
expect(sander.rimrafSync).toHaveBeenCalledWith(expectedPathToBeDeleted)
})
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)
})