From a76aed2d4efd31f99e49230717808479280544c0 Mon Sep 17 00:00:00 2001 From: ehhc Date: Sat, 21 Apr 2018 14:49:43 +0200 Subject: [PATCH 1/5] Fixes #1822 --- browser/main/lib/dataApi/attachmentManagement.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/browser/main/lib/dataApi/attachmentManagement.js b/browser/main/lib/dataApi/attachmentManagement.js index 6d4d7406..d1e57b75 100644 --- a/browser/main/lib/dataApi/attachmentManagement.js +++ b/browser/main/lib/dataApi/attachmentManagement.js @@ -39,7 +39,7 @@ function copyAttachment (sourceFilePath, storageKey, noteKey, useRandomName = tr const targetStorage = findStorage.findStorage(storageKey) - const inputFile = fs.createReadStream(sourceFilePath) + const inputFileStream = fs.createReadStream(sourceFilePath) let destinationName if (useRandomName) { destinationName = `${uniqueSlug()}${path.extname(sourceFilePath)}` @@ -49,8 +49,10 @@ function copyAttachment (sourceFilePath, storageKey, noteKey, useRandomName = tr const destinationDir = path.join(targetStorage.path, DESTINATION_FOLDER, noteKey) createAttachmentDestinationFolder(targetStorage.path, noteKey) const outputFile = fs.createWriteStream(path.join(destinationDir, destinationName)) - inputFile.pipe(outputFile) - resolve(destinationName) + inputFileStream.pipe(outputFile) + inputFileStream.on('end', () => { + resolve(destinationName) + }) } catch (e) { return reject(e) } @@ -146,7 +148,7 @@ function handlePastImageEvent (codeEditor, storageKey, noteKey, dataTransferItem base64data = reader.result.replace(/^data:image\/png;base64,/, '') base64data += base64data.replace('+', ' ') const binaryData = new Buffer(base64data, 'base64').toString('binary') - fs.writeFile(imagePath, binaryData, 'binary') + fs.writeFileSync(imagePath, binaryData, 'binary') let imageMd = generateAttachmentMarkdown(imageName, imagePath, true) codeEditor.insertAttachmentMd(imageMd) } From 16794b9d78b8b30fc664b166bb9d56a4a76d7987 Mon Sep 17 00:00:00 2001 From: ehhc Date: Sat, 21 Apr 2018 16:32:27 +0200 Subject: [PATCH 2/5] Fixes #1822 -> fix for the broken tests --- tests/dataApi/attachmentManagement.test.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/tests/dataApi/attachmentManagement.test.js b/tests/dataApi/attachmentManagement.test.js index 7e97cf80..1f40cd60 100644 --- a/tests/dataApi/attachmentManagement.test.js +++ b/tests/dataApi/attachmentManagement.test.js @@ -48,11 +48,13 @@ it('should test that copyAttachment works correctly assuming correct working of const noteKey = 'noteKey' const dummyUniquePath = 'dummyPath' const dummyStorage = {path: 'dummyStoragePath'} + const dummyReadStream = {} + dummyReadStream.pipe = jest.fn() + dummyReadStream.on = jest.fn((event, callback) => { callback() }) fs.existsSync = jest.fn() fs.existsSync.mockReturnValue(true) - fs.createReadStream = jest.fn() - fs.createReadStream.mockReturnValue({pipe: jest.fn()}) + fs.createReadStream = jest.fn(() => dummyReadStream) fs.createWriteStream = jest.fn() findStorage.findStorage = jest.fn() @@ -75,7 +77,11 @@ it('should test that copyAttachment creates a new folder if the attachment folde const noteKey = 'noteKey' const attachmentFolderPath = path.join(dummyStorage.path, systemUnderTest.DESTINATION_FOLDER) const attachmentFolderNoteKyPath = path.join(dummyStorage.path, systemUnderTest.DESTINATION_FOLDER, noteKey) + const dummyReadStream = {} + dummyReadStream.pipe = jest.fn() + dummyReadStream.on = jest.fn() + fs.createReadStream = jest.fn(() => dummyReadStream) fs.existsSync = jest.fn() fs.existsSync.mockReturnValueOnce(true) fs.existsSync.mockReturnValueOnce(false) @@ -97,7 +103,11 @@ it('should test that copyAttachment creates a new folder if the attachment folde it('should test that copyAttachment don\'t uses a random file name if not intended ', function () { const dummyStorage = {path: 'dummyStoragePath'} + const dummyReadStream = {} + dummyReadStream.pipe = jest.fn() + dummyReadStream.on = jest.fn() + fs.createReadStream = jest.fn(() => dummyReadStream) fs.existsSync = jest.fn() fs.existsSync.mockReturnValueOnce(true) fs.existsSync.mockReturnValueOnce(false) From f76224bd17c10c43feca02c8d76027d27551ebbf Mon Sep 17 00:00:00 2001 From: ehhc Date: Sun, 20 May 2018 15:18:49 +0200 Subject: [PATCH 3/5] Cloning of a note should also clone its attachments -> fixes #1904 --- browser/main/NoteList/index.js | 5 ++ .../main/lib/dataApi/attachmentManagement.js | 37 ++++++++++++- tests/dataApi/attachmentManagement.test.js | 55 +++++++++++++++++++ 3 files changed, 96 insertions(+), 1 deletion(-) diff --git a/browser/main/NoteList/index.js b/browser/main/NoteList/index.js index 876de0c0..be885f8e 100644 --- a/browser/main/NoteList/index.js +++ b/browser/main/NoteList/index.js @@ -7,6 +7,7 @@ import moment from 'moment' import _ from 'lodash' import ee from 'browser/main/lib/eventEmitter' import dataApi from 'browser/main/lib/dataApi' +import attachmentManagement from 'browser/main/lib/dataApi/attachmentManagement' import ConfigManager from 'browser/main/lib/ConfigManager' import NoteItem from 'browser/components/NoteItem' import NoteItemSimple from 'browser/components/NoteItemSimple' @@ -662,6 +663,10 @@ class NoteList extends React.Component { title: firstNote.title + ' ' + i18n.__('copy'), content: firstNote.content }) + .then((note) => { + attachmentManagement.cloneAttachments(storage.key, firstNote, note) + return note + }) .then((note) => { dispatch({ type: 'UPDATE_NOTE', diff --git a/browser/main/lib/dataApi/attachmentManagement.js b/browser/main/lib/dataApi/attachmentManagement.js index b78ff8a9..e9604d30 100644 --- a/browser/main/lib/dataApi/attachmentManagement.js +++ b/browser/main/lib/dataApi/attachmentManagement.js @@ -200,8 +200,19 @@ function moveAttachments (oldPath, newPath, noteKey, newNoteKey, noteContent) { if (fse.existsSync(src)) { fse.moveSync(src, dest) } + return replaceNoteKeyWithNewNoteKey(noteContent, noteKey, newNoteKey) +} + +/** + * Modifies the given content so that in all attachment references the oldNoteKey is replaced by the new one + * @param noteContent content that should be modified + * @param oldNoteKey note key to be replaced + * @param newNoteKey note key serving as a replacement + * @returns {String} modified note content + */ +function replaceNoteKeyWithNewNoteKey (noteContent, oldNoteKey, newNoteKey) { if (noteContent) { - return noteContent.replace(new RegExp(STORAGE_FOLDER_PLACEHOLDER + escapeStringRegexp(path.sep) + noteKey, 'g'), path.join(STORAGE_FOLDER_PLACEHOLDER, newNoteKey)) + return noteContent.replace(new RegExp(STORAGE_FOLDER_PLACEHOLDER + escapeStringRegexp(path.sep) + oldNoteKey, 'g'), path.join(STORAGE_FOLDER_PLACEHOLDER, newNoteKey)) } return noteContent } @@ -270,6 +281,29 @@ function deleteAttachmentsNotPresentInNote (markdownContent, storageKey, noteKey } } +/** + * Clones the attachments of a given note. + * Copies the attachments to their new destination and updates the content of the new note so that the attachment-links again point to the correct destination. + * @param storageKey Key of the current storage + * @param oldNote Note that is being cloned + * @param newNote Clone of the note + */ +function cloneAttachments (storageKey, oldNote, newNote) { + const storage = findStorage.findStorage(storageKey) + const attachmentsPaths = getAbsolutePathsOfAttachmentsInContent(oldNote.content, storage.path) || [] + + const destinationFolder = path.join(storage.path, DESTINATION_FOLDER, newNote.key) + if (!sander.existsSync(destinationFolder)) { + sander.mkdirSync(destinationFolder) + } + + for (const attachment of attachmentsPaths) { + const destination = path.join(storage.path, DESTINATION_FOLDER, newNote.key, path.basename(attachment)) + sander.copyFileSync(attachment).to(destination) + } + newNote.content = replaceNoteKeyWithNewNoteKey(newNote.content, oldNote.key, newNote.key) +} + module.exports = { copyAttachment, fixLocalURLS, @@ -282,6 +316,7 @@ module.exports = { deleteAttachmentFolder, deleteAttachmentsNotPresentInNote, moveAttachments, + cloneAttachments, STORAGE_FOLDER_PLACEHOLDER, DESTINATION_FOLDER } diff --git a/tests/dataApi/attachmentManagement.test.js b/tests/dataApi/attachmentManagement.test.js index a3c88cbb..4175846a 100644 --- a/tests/dataApi/attachmentManagement.test.js +++ b/tests/dataApi/attachmentManagement.test.js @@ -8,6 +8,7 @@ jest.mock('unique-slug') const uniqueSlug = require('unique-slug') const mdurl = require('mdurl') const fse = require('fs-extra') +jest.mock('sander') const sander = require('sander') const systemUnderTest = require('browser/main/lib/dataApi/attachmentManagement') @@ -393,3 +394,57 @@ it('should test that moveAttachments returns a correct modified content version' const actualContent = systemUnderTest.moveAttachments(oldPath, newPath, oldNoteKey, newNoteKey, testInput) expect(actualContent).toBe(expectedOutput) }) + +it('should test that cloneAttachments modifies the content of the new note correctly', function () { + const storageKey = 'storageKey' + const oldNote = {key: 'oldNoteKey', content: 'oldNoteContent'} + const newNote = {key: 'newNoteKey', content: 'oldNoteContent'} + const testInput = + 'Test input' + + '![' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + oldNote.key + path.sep + 'image.jpg](imageName}) \n' + + '[' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + oldNote.key + path.sep + 'pdf.pdf](pdf})' + newNote.content = testInput + + const expectedOutput = + 'Test input' + + '![' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + newNote.key + path.sep + 'image.jpg](imageName}) \n' + + '[' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + newNote.key + path.sep + 'pdf.pdf](pdf})' + systemUnderTest.cloneAttachments(storageKey, oldNote, newNote) + + expect(newNote.content).toBe(expectedOutput) +}) + +it('should test that cloneAttachments finds all attachments and copies them to the new location', function () { + const storageKey = 'storageKey' + const storagePath = 'storagePath' + const dummyStorage = {path: storagePath} + const oldNote = {key: 'oldNoteKey', content: 'oldNoteContent'} + const newNote = {key: 'newNoteKey', content: 'oldNoteContent'} + const testInput = + 'Test input' + + '![' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + oldNote.key + path.sep + 'image.jpg](imageName}) \n' + + '[' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + oldNote.key + path.sep + 'pdf.pdf](pdf})' + oldNote.content = testInput + newNote.content = testInput + + const copyFileSyncResp = {to: jest.fn()} + sander.copyFileSync = jest.fn() + sander.copyFileSync.mockReturnValue(copyFileSyncResp) + findStorage.findStorage = jest.fn(() => dummyStorage) + + const pathAttachmentOneFrom = path.join(storagePath, systemUnderTest.DESTINATION_FOLDER, oldNote.key, 'image.jpg') + const pathAttachmentOneTo = path.join(storagePath, systemUnderTest.DESTINATION_FOLDER, newNote.key, 'image.jpg') + + const pathAttachmentTwoFrom = path.join(storagePath, systemUnderTest.DESTINATION_FOLDER, oldNote.key, 'pdf.pdf') + const pathAttachmentTwoTo = path.join(storagePath, systemUnderTest.DESTINATION_FOLDER, newNote.key, 'pdf.pdf') + + systemUnderTest.cloneAttachments(storageKey, oldNote, newNote) + + expect(findStorage.findStorage).toHaveBeenCalledWith(storageKey) + expect(sander.copyFileSync).toHaveBeenCalledTimes(2) + expect(copyFileSyncResp.to).toHaveBeenCalledTimes(2) + expect(sander.copyFileSync.mock.calls[0][0]).toBe(pathAttachmentOneFrom) + expect(copyFileSyncResp.to.mock.calls[0][0]).toBe(pathAttachmentOneTo) + expect(sander.copyFileSync.mock.calls[1][0]).toBe(pathAttachmentTwoFrom) + expect(copyFileSyncResp.to.mock.calls[1][0]).toBe(pathAttachmentTwoTo) +}) From cd6233a3d7cfa2a03aede8b23bcc630c70fb2560 Mon Sep 17 00:00:00 2001 From: ehhc Date: Sun, 20 May 2018 15:49:15 +0200 Subject: [PATCH 4/5] Cloning of a note should also clone its attachments -> works if the notes are in different storages now --- browser/main/NoteList/index.js | 2 +- .../main/lib/dataApi/attachmentManagement.js | 12 +++---- tests/dataApi/attachmentManagement.test.js | 35 ++++++++++--------- 3 files changed, 26 insertions(+), 23 deletions(-) diff --git a/browser/main/NoteList/index.js b/browser/main/NoteList/index.js index be885f8e..d6b7f846 100644 --- a/browser/main/NoteList/index.js +++ b/browser/main/NoteList/index.js @@ -664,7 +664,7 @@ class NoteList extends React.Component { content: firstNote.content }) .then((note) => { - attachmentManagement.cloneAttachments(storage.key, firstNote, note) + attachmentManagement.cloneAttachments(firstNote, note) return note }) .then((note) => { diff --git a/browser/main/lib/dataApi/attachmentManagement.js b/browser/main/lib/dataApi/attachmentManagement.js index e9604d30..eb73bd95 100644 --- a/browser/main/lib/dataApi/attachmentManagement.js +++ b/browser/main/lib/dataApi/attachmentManagement.js @@ -284,21 +284,21 @@ function deleteAttachmentsNotPresentInNote (markdownContent, storageKey, noteKey /** * Clones the attachments of a given note. * Copies the attachments to their new destination and updates the content of the new note so that the attachment-links again point to the correct destination. - * @param storageKey Key of the current storage * @param oldNote Note that is being cloned * @param newNote Clone of the note */ -function cloneAttachments (storageKey, oldNote, newNote) { - const storage = findStorage.findStorage(storageKey) - const attachmentsPaths = getAbsolutePathsOfAttachmentsInContent(oldNote.content, storage.path) || [] +function cloneAttachments (oldNote, newNote) { + const oldStorage = findStorage.findStorage(oldNote.storage) + const newStorage = findStorage.findStorage(newNote.storage) + const attachmentsPaths = getAbsolutePathsOfAttachmentsInContent(oldNote.content, oldStorage.path) || [] - const destinationFolder = path.join(storage.path, DESTINATION_FOLDER, newNote.key) + const destinationFolder = path.join(newStorage.path, DESTINATION_FOLDER, newNote.key) if (!sander.existsSync(destinationFolder)) { sander.mkdirSync(destinationFolder) } for (const attachment of attachmentsPaths) { - const destination = path.join(storage.path, DESTINATION_FOLDER, newNote.key, path.basename(attachment)) + const destination = path.join(newStorage.path, DESTINATION_FOLDER, newNote.key, path.basename(attachment)) sander.copyFileSync(attachment).to(destination) } newNote.content = replaceNoteKeyWithNewNoteKey(newNote.content, oldNote.key, newNote.key) diff --git a/tests/dataApi/attachmentManagement.test.js b/tests/dataApi/attachmentManagement.test.js index 4175846a..c101030b 100644 --- a/tests/dataApi/attachmentManagement.test.js +++ b/tests/dataApi/attachmentManagement.test.js @@ -396,9 +396,8 @@ it('should test that moveAttachments returns a correct modified content version' }) it('should test that cloneAttachments modifies the content of the new note correctly', function () { - const storageKey = 'storageKey' - const oldNote = {key: 'oldNoteKey', content: 'oldNoteContent'} - const newNote = {key: 'newNoteKey', content: 'oldNoteContent'} + const oldNote = {key: 'oldNoteKey', content: 'oldNoteContent', storage: 'storageKey'} + const newNote = {key: 'newNoteKey', content: 'oldNoteContent', storage: 'storageKey'} const testInput = 'Test input' + '![' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + oldNote.key + path.sep + 'image.jpg](imageName}) \n' + @@ -409,17 +408,18 @@ it('should test that cloneAttachments modifies the content of the new note corre 'Test input' + '![' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + newNote.key + path.sep + 'image.jpg](imageName}) \n' + '[' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + newNote.key + path.sep + 'pdf.pdf](pdf})' - systemUnderTest.cloneAttachments(storageKey, oldNote, newNote) + systemUnderTest.cloneAttachments(oldNote, newNote) expect(newNote.content).toBe(expectedOutput) }) it('should test that cloneAttachments finds all attachments and copies them to the new location', function () { - const storageKey = 'storageKey' - const storagePath = 'storagePath' - const dummyStorage = {path: storagePath} - const oldNote = {key: 'oldNoteKey', content: 'oldNoteContent'} - const newNote = {key: 'newNoteKey', content: 'oldNoteContent'} + const storagePathOld = 'storagePathOld' + const storagePathNew = 'storagePathNew' + const dummyStorageOld = {path: storagePathOld} + const dummyStorageNew = {path: storagePathNew} + const oldNote = {key: 'oldNoteKey', content: 'oldNoteContent', storage: 'storageKeyOldNote'} + const newNote = {key: 'newNoteKey', content: 'oldNoteContent', storage: 'storageKeyNewNote'} const testInput = 'Test input' + '![' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + oldNote.key + path.sep + 'image.jpg](imageName}) \n' + @@ -430,17 +430,20 @@ it('should test that cloneAttachments finds all attachments and copies them to t const copyFileSyncResp = {to: jest.fn()} sander.copyFileSync = jest.fn() sander.copyFileSync.mockReturnValue(copyFileSyncResp) - findStorage.findStorage = jest.fn(() => dummyStorage) + findStorage.findStorage = jest.fn() + findStorage.findStorage.mockReturnValueOnce(dummyStorageOld) + findStorage.findStorage.mockReturnValue(dummyStorageNew) - const pathAttachmentOneFrom = path.join(storagePath, systemUnderTest.DESTINATION_FOLDER, oldNote.key, 'image.jpg') - const pathAttachmentOneTo = path.join(storagePath, systemUnderTest.DESTINATION_FOLDER, newNote.key, 'image.jpg') + const pathAttachmentOneFrom = path.join(storagePathOld, systemUnderTest.DESTINATION_FOLDER, oldNote.key, 'image.jpg') + const pathAttachmentOneTo = path.join(storagePathNew, systemUnderTest.DESTINATION_FOLDER, newNote.key, 'image.jpg') - const pathAttachmentTwoFrom = path.join(storagePath, systemUnderTest.DESTINATION_FOLDER, oldNote.key, 'pdf.pdf') - const pathAttachmentTwoTo = path.join(storagePath, systemUnderTest.DESTINATION_FOLDER, newNote.key, 'pdf.pdf') + const pathAttachmentTwoFrom = path.join(storagePathOld, systemUnderTest.DESTINATION_FOLDER, oldNote.key, 'pdf.pdf') + const pathAttachmentTwoTo = path.join(storagePathNew, systemUnderTest.DESTINATION_FOLDER, newNote.key, 'pdf.pdf') - systemUnderTest.cloneAttachments(storageKey, oldNote, newNote) + systemUnderTest.cloneAttachments(oldNote, newNote) - expect(findStorage.findStorage).toHaveBeenCalledWith(storageKey) + expect(findStorage.findStorage).toHaveBeenCalledWith(oldNote.storage) + expect(findStorage.findStorage).toHaveBeenCalledWith(newNote.storage) expect(sander.copyFileSync).toHaveBeenCalledTimes(2) expect(copyFileSyncResp.to).toHaveBeenCalledTimes(2) expect(sander.copyFileSync.mock.calls[0][0]).toBe(pathAttachmentOneFrom) From bfcf349ffefb54ec4e4e96dc19a6e85cb70b4b13 Mon Sep 17 00:00:00 2001 From: ehhc Date: Wed, 23 May 2018 19:24:03 +0200 Subject: [PATCH 5/5] Attachment management should only become active on cloning notes if the note was of type MARKDOWN_NOTE -> No Stacktraces otherwise! --- .../main/lib/dataApi/attachmentManagement.js | 26 +++++++++++-------- tests/dataApi/attachmentManagement.test.js | 24 ++++++++++++++--- 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/browser/main/lib/dataApi/attachmentManagement.js b/browser/main/lib/dataApi/attachmentManagement.js index eb73bd95..893e03d1 100644 --- a/browser/main/lib/dataApi/attachmentManagement.js +++ b/browser/main/lib/dataApi/attachmentManagement.js @@ -288,20 +288,24 @@ function deleteAttachmentsNotPresentInNote (markdownContent, storageKey, noteKey * @param newNote Clone of the note */ function cloneAttachments (oldNote, newNote) { - const oldStorage = findStorage.findStorage(oldNote.storage) - const newStorage = findStorage.findStorage(newNote.storage) - const attachmentsPaths = getAbsolutePathsOfAttachmentsInContent(oldNote.content, oldStorage.path) || [] + if (newNote.type === 'MARKDOWN_NOTE') { + const oldStorage = findStorage.findStorage(oldNote.storage) + const newStorage = findStorage.findStorage(newNote.storage) + const attachmentsPaths = getAbsolutePathsOfAttachmentsInContent(oldNote.content, oldStorage.path) || [] - const destinationFolder = path.join(newStorage.path, DESTINATION_FOLDER, newNote.key) - if (!sander.existsSync(destinationFolder)) { - sander.mkdirSync(destinationFolder) - } + const destinationFolder = path.join(newStorage.path, DESTINATION_FOLDER, newNote.key) + if (!sander.existsSync(destinationFolder)) { + sander.mkdirSync(destinationFolder) + } - for (const attachment of attachmentsPaths) { - const destination = path.join(newStorage.path, DESTINATION_FOLDER, newNote.key, path.basename(attachment)) - sander.copyFileSync(attachment).to(destination) + for (const attachment of attachmentsPaths) { + const destination = path.join(newStorage.path, DESTINATION_FOLDER, newNote.key, path.basename(attachment)) + sander.copyFileSync(attachment).to(destination) + } + newNote.content = replaceNoteKeyWithNewNoteKey(newNote.content, oldNote.key, newNote.key) + } else { + console.debug('Cloning of the attachment was skipped since it only works for MARKDOWN_NOTEs') } - newNote.content = replaceNoteKeyWithNewNoteKey(newNote.content, oldNote.key, newNote.key) } module.exports = { diff --git a/tests/dataApi/attachmentManagement.test.js b/tests/dataApi/attachmentManagement.test.js index c101030b..b61d8bf9 100644 --- a/tests/dataApi/attachmentManagement.test.js +++ b/tests/dataApi/attachmentManagement.test.js @@ -396,8 +396,8 @@ it('should test that moveAttachments returns a correct modified content version' }) it('should test that cloneAttachments modifies the content of the new note correctly', function () { - const oldNote = {key: 'oldNoteKey', content: 'oldNoteContent', storage: 'storageKey'} - const newNote = {key: 'newNoteKey', content: 'oldNoteContent', storage: 'storageKey'} + const oldNote = {key: 'oldNoteKey', content: 'oldNoteContent', storage: 'storageKey', type: 'MARKDOWN_NOTE'} + const newNote = {key: 'newNoteKey', content: 'oldNoteContent', storage: 'storageKey', type: 'MARKDOWN_NOTE'} const testInput = 'Test input' + '![' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + oldNote.key + path.sep + 'image.jpg](imageName}) \n' + @@ -418,8 +418,8 @@ it('should test that cloneAttachments finds all attachments and copies them to t const storagePathNew = 'storagePathNew' const dummyStorageOld = {path: storagePathOld} const dummyStorageNew = {path: storagePathNew} - const oldNote = {key: 'oldNoteKey', content: 'oldNoteContent', storage: 'storageKeyOldNote'} - const newNote = {key: 'newNoteKey', content: 'oldNoteContent', storage: 'storageKeyNewNote'} + const oldNote = {key: 'oldNoteKey', content: 'oldNoteContent', storage: 'storageKeyOldNote', type: 'MARKDOWN_NOTE'} + const newNote = {key: 'newNoteKey', content: 'oldNoteContent', storage: 'storageKeyNewNote', type: 'MARKDOWN_NOTE'} const testInput = 'Test input' + '![' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + oldNote.key + path.sep + 'image.jpg](imageName}) \n' + @@ -451,3 +451,19 @@ it('should test that cloneAttachments finds all attachments and copies them to t expect(sander.copyFileSync.mock.calls[1][0]).toBe(pathAttachmentTwoFrom) expect(copyFileSyncResp.to.mock.calls[1][0]).toBe(pathAttachmentTwoTo) }) + +it('should test that cloneAttachments finds all attachments and copies them to the new location', function () { + const oldNote = {key: 'oldNoteKey', content: 'oldNoteContent', storage: 'storageKeyOldNote', type: 'SOMETHING_ELSE'} + const newNote = {key: 'newNoteKey', content: 'oldNoteContent', storage: 'storageKeyNewNote', type: 'SOMETHING_ELSE'} + const testInput = 'Test input' + oldNote.content = testInput + newNote.content = testInput + + sander.copyFileSync = jest.fn() + findStorage.findStorage = jest.fn() + + systemUnderTest.cloneAttachments(oldNote, newNote) + + expect(findStorage.findStorage).not.toHaveBeenCalled() + expect(sander.copyFileSync).not.toHaveBeenCalled() +})