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

Copying the link to an attachment should make sure that it is located in the same note folder -> Fixes #1924

This commit is contained in:
ehhc
2018-05-29 16:30:43 +02:00
parent ca0b03e97c
commit 9c247bcb22
21 changed files with 364 additions and 34 deletions

View File

@@ -467,3 +467,239 @@ it('should test that cloneAttachments finds all attachments and copies them to t
expect(findStorage.findStorage).not.toHaveBeenCalled()
expect(sander.copyFileSync).not.toHaveBeenCalled()
})
it('should test that isAttachmentLink works correctly', function () {
expect(systemUnderTest.isAttachmentLink('text')).toBe(false)
expect(systemUnderTest.isAttachmentLink('text [linkText](link)')).toBe(false)
expect(systemUnderTest.isAttachmentLink('text ![linkText](link)')).toBe(false)
expect(systemUnderTest.isAttachmentLink('[linkText](' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + 'noteKey' + path.sep + 'pdf.pdf)')).toBe(true)
expect(systemUnderTest.isAttachmentLink('![linkText](' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + 'noteKey' + path.sep + 'pdf.pdf )')).toBe(true)
expect(systemUnderTest.isAttachmentLink('text [ linkText](' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + 'noteKey' + path.sep + 'pdf.pdf)')).toBe(true)
expect(systemUnderTest.isAttachmentLink('text ![linkText ](' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + 'noteKey' + path.sep + 'pdf.pdf)')).toBe(true)
expect(systemUnderTest.isAttachmentLink('[linkText](' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + 'noteKey' + path.sep + 'pdf.pdf) test')).toBe(true)
expect(systemUnderTest.isAttachmentLink('![linkText](' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + 'noteKey' + path.sep + 'pdf.pdf) test')).toBe(true)
expect(systemUnderTest.isAttachmentLink('text [linkText](' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + 'noteKey' + path.sep + 'pdf.pdf) test')).toBe(true)
expect(systemUnderTest.isAttachmentLink('text ![linkText](' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + 'noteKey' + path.sep + 'pdf.pdf) test')).toBe(true)
})
it('should test that handleAttachmentLinkPaste copies the attachments to the new location', function () {
const dummyStorage = {path: 'dummyStoragePath'}
findStorage.findStorage = jest.fn(() => dummyStorage)
const pastedNoteKey = 'b1e06f81-8266-49b9-b438-084003c2e723'
const newNoteKey = 'abc234-8266-49b9-b438-084003c2e723'
const pasteText = 'text ![alt](' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + pastedNoteKey + path.sep + 'pdf.pdf)'
const storageKey = 'storageKey'
const expectedSourceFilePath = path.join(dummyStorage.path, systemUnderTest.DESTINATION_FOLDER, pastedNoteKey, 'pdf.pdf')
sander.exists = jest.fn(() => Promise.resolve(true))
systemUnderTest.copyAttachment = jest.fn(() => Promise.resolve('dummyNewFileName'))
return systemUnderTest.handleAttachmentLinkPaste(storageKey, newNoteKey, pasteText)
.then(() => {
expect(findStorage.findStorage).toHaveBeenCalledWith(storageKey)
expect(sander.exists).toHaveBeenCalledWith(expectedSourceFilePath)
expect(systemUnderTest.copyAttachment).toHaveBeenCalledWith(expectedSourceFilePath, storageKey, newNoteKey)
})
})
it('should test that handleAttachmentLinkPaste don\'t try to copy the file if it does not exist', function () {
const dummyStorage = {path: 'dummyStoragePath'}
findStorage.findStorage = jest.fn(() => dummyStorage)
const pastedNoteKey = 'b1e06f81-8266-49b9-b438-084003c2e723'
const newNoteKey = 'abc234-8266-49b9-b438-084003c2e723'
const pasteText = 'text ![alt](' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + pastedNoteKey + path.sep + 'pdf.pdf)'
const storageKey = 'storageKey'
const expectedSourceFilePath = path.join(dummyStorage.path, systemUnderTest.DESTINATION_FOLDER, pastedNoteKey, 'pdf.pdf')
sander.exists = jest.fn(() => Promise.resolve(false))
systemUnderTest.copyAttachment = jest.fn()
systemUnderTest.generateFileNotFoundMarkdown = jest.fn()
return systemUnderTest.handleAttachmentLinkPaste(storageKey, newNoteKey, pasteText)
.then(() => {
expect(findStorage.findStorage).toHaveBeenCalledWith(storageKey)
expect(sander.exists).toHaveBeenCalledWith(expectedSourceFilePath)
expect(systemUnderTest.copyAttachment).not.toHaveBeenCalled()
})
})
it('should test that handleAttachmentLinkPaste copies multiple attachments if multiple were pasted', function () {
const dummyStorage = {path: 'dummyStoragePath'}
findStorage.findStorage = jest.fn(() => dummyStorage)
const pastedNoteKey = 'b1e06f81-8266-49b9-b438-084003c2e723'
const newNoteKey = 'abc234-8266-49b9-b438-084003c2e723'
const pasteText = 'text ![alt](' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + pastedNoteKey + path.sep + 'pdf.pdf) ..' +
'![secondAttachment](' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + pastedNoteKey + path.sep + 'img.jpg)'
const storageKey = 'storageKey'
const expectedSourceFilePathOne = path.join(dummyStorage.path, systemUnderTest.DESTINATION_FOLDER, pastedNoteKey, 'pdf.pdf')
const expectedSourceFilePathTwo = path.join(dummyStorage.path, systemUnderTest.DESTINATION_FOLDER, pastedNoteKey, 'img.jpg')
sander.exists = jest.fn(() => Promise.resolve(true))
systemUnderTest.copyAttachment = jest.fn(() => Promise.resolve('dummyNewFileName'))
return systemUnderTest.handleAttachmentLinkPaste(storageKey, newNoteKey, pasteText)
.then(() => {
expect(findStorage.findStorage).toHaveBeenCalledWith(storageKey)
expect(sander.exists).toHaveBeenCalledWith(expectedSourceFilePathOne)
expect(sander.exists).toHaveBeenCalledWith(expectedSourceFilePathTwo)
expect(systemUnderTest.copyAttachment).toHaveBeenCalledWith(expectedSourceFilePathOne, storageKey, newNoteKey)
expect(systemUnderTest.copyAttachment).toHaveBeenCalledWith(expectedSourceFilePathTwo, storageKey, newNoteKey)
})
})
it('should test that handleAttachmentLinkPaste returns the correct modified paste text', function () {
const dummyStorage = {path: 'dummyStoragePath'}
findStorage.findStorage = jest.fn(() => dummyStorage)
const pastedNoteKey = 'b1e06f81-8266-49b9-b438-084003c2e723'
const newNoteKey = 'abc234-8266-49b9-b438-084003c2e723'
const dummyNewFileName = 'dummyNewFileName'
const pasteText = 'text ![alt](' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + pastedNoteKey + path.sep + 'pdf.pdf)'
const expectedText = 'text ![alt](' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + newNoteKey + path.sep + dummyNewFileName + ')'
const storageKey = 'storageKey'
sander.exists = jest.fn(() => Promise.resolve(true))
systemUnderTest.copyAttachment = jest.fn(() => Promise.resolve(dummyNewFileName))
return systemUnderTest.handleAttachmentLinkPaste(storageKey, newNoteKey, pasteText)
.then((returnedPastedText) => {
expect(returnedPastedText).toBe(expectedText)
})
})
it('should test that handleAttachmentLinkPaste returns the correct modified paste text if multiple links are posted', function () {
const dummyStorage = {path: 'dummyStoragePath'}
findStorage.findStorage = jest.fn(() => dummyStorage)
const pastedNoteKey = 'b1e06f81-8266-49b9-b438-084003c2e723'
const newNoteKey = 'abc234-8266-49b9-b438-084003c2e723'
const dummyNewFileNameOne = 'dummyNewFileName'
const dummyNewFileNameTwo = 'dummyNewFileNameTwo'
const pasteText = 'text ![alt](' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + pastedNoteKey + path.sep + 'pdf.pdf) ' +
'![secondImage](' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + pastedNoteKey + path.sep + 'img.jpg)'
const expectedText = 'text ![alt](' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + newNoteKey + path.sep + dummyNewFileNameOne + ') ' +
'![secondImage](' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + newNoteKey + path.sep + dummyNewFileNameTwo + ')'
const storageKey = 'storageKey'
sander.exists = jest.fn(() => Promise.resolve(true))
systemUnderTest.copyAttachment = jest.fn()
systemUnderTest.copyAttachment.mockReturnValueOnce(Promise.resolve(dummyNewFileNameOne))
systemUnderTest.copyAttachment.mockReturnValue(Promise.resolve(dummyNewFileNameTwo))
return systemUnderTest.handleAttachmentLinkPaste(storageKey, newNoteKey, pasteText)
.then((returnedPastedText) => {
expect(returnedPastedText).toBe(expectedText)
})
})
it('should test that handleAttachmentLinkPaste calls the copy method correct if multiple links are posted where one file was found and one was not', function () {
const dummyStorage = {path: 'dummyStoragePath'}
findStorage.findStorage = jest.fn(() => dummyStorage)
const pastedNoteKey = 'b1e06f81-8266-49b9-b438-084003c2e723'
const newNoteKey = 'abc234-8266-49b9-b438-084003c2e723'
const pasteText = 'text ![alt](' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + pastedNoteKey + path.sep + 'pdf.pdf) ..' +
'![secondAttachment](' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + pastedNoteKey + path.sep + 'img.jpg)'
const storageKey = 'storageKey'
const expectedSourceFilePathOne = path.join(dummyStorage.path, systemUnderTest.DESTINATION_FOLDER, pastedNoteKey, 'pdf.pdf')
const expectedSourceFilePathTwo = path.join(dummyStorage.path, systemUnderTest.DESTINATION_FOLDER, pastedNoteKey, 'img.jpg')
sander.exists = jest.fn()
sander.exists.mockReturnValueOnce(Promise.resolve(false))
sander.exists.mockReturnValue(Promise.resolve(true))
systemUnderTest.copyAttachment = jest.fn(() => Promise.resolve('dummyNewFileName'))
systemUnderTest.generateFileNotFoundMarkdown = jest.fn()
return systemUnderTest.handleAttachmentLinkPaste(storageKey, newNoteKey, pasteText)
.then(() => {
expect(findStorage.findStorage).toHaveBeenCalledWith(storageKey)
expect(sander.exists).toHaveBeenCalledWith(expectedSourceFilePathOne)
expect(sander.exists).toHaveBeenCalledWith(expectedSourceFilePathTwo)
expect(systemUnderTest.copyAttachment).toHaveBeenCalledTimes(1)
expect(systemUnderTest.copyAttachment).toHaveBeenCalledWith(expectedSourceFilePathTwo, storageKey, newNoteKey)
})
})
it('should test that handleAttachmentLinkPaste returns the correct modified paste text if the file was not found', function () {
const dummyStorage = {path: 'dummyStoragePath'}
findStorage.findStorage = jest.fn(() => dummyStorage)
const pastedNoteKey = 'b1e06f81-8266-49b9-b438-084003c2e723'
const newNoteKey = 'abc234-8266-49b9-b438-084003c2e723'
const pasteText = 'text ![alt.png](' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + pastedNoteKey + path.sep + 'pdf.pdf)'
const storageKey = 'storageKey'
const fileNotFoundMD = 'file not found'
const expectedPastText = 'text ' + fileNotFoundMD
systemUnderTest.generateFileNotFoundMarkdown = jest.fn(() => fileNotFoundMD)
sander.exists = jest.fn(() => Promise.resolve(false))
return systemUnderTest.handleAttachmentLinkPaste(storageKey, newNoteKey, pasteText)
.then((returnedPastedText) => {
expect(returnedPastedText).toBe(expectedPastText)
})
})
it('should test that handleAttachmentLinkPaste returns the correct modified paste text if multiple files were not found', function () {
const dummyStorage = {path: 'dummyStoragePath'}
findStorage.findStorage = jest.fn(() => dummyStorage)
const pastedNoteKey = 'b1e06f81-8266-49b9-b438-084003c2e723'
const newNoteKey = 'abc234-8266-49b9-b438-084003c2e723'
const pasteText = 'text ![alt](' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + pastedNoteKey + path.sep + 'pdf.pdf) ' +
'![secondImage](' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + pastedNoteKey + path.sep + 'img.jpg)'
const storageKey = 'storageKey'
const fileNotFoundMD = 'file not found'
const expectedPastText = 'text ' + fileNotFoundMD + ' ' + fileNotFoundMD
systemUnderTest.generateFileNotFoundMarkdown = jest.fn(() => fileNotFoundMD)
sander.exists = jest.fn(() => Promise.resolve(false))
return systemUnderTest.handleAttachmentLinkPaste(storageKey, newNoteKey, pasteText)
.then((returnedPastedText) => {
expect(returnedPastedText).toBe(expectedPastText)
})
})
it('should test that handleAttachmentLinkPaste returns the correct modified paste text if one file was found and one was not found', function () {
const dummyStorage = {path: 'dummyStoragePath'}
findStorage.findStorage = jest.fn(() => dummyStorage)
const pastedNoteKey = 'b1e06f81-8266-49b9-b438-084003c2e723'
const newNoteKey = 'abc234-8266-49b9-b438-084003c2e723'
const dummyFoundFileName = 'dummyFileName'
const fileNotFoundMD = 'file not found'
const pasteText = 'text ![alt](' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + pastedNoteKey + path.sep + 'pdf.pdf) .. ' +
'![secondAttachment](' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + pastedNoteKey + path.sep + 'img.jpg)'
const storageKey = 'storageKey'
const expectedPastText = 'text ' + fileNotFoundMD + ' .. ![secondAttachment](' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + newNoteKey + path.sep + dummyFoundFileName + ')'
sander.exists = jest.fn()
sander.exists.mockReturnValueOnce(Promise.resolve(false))
sander.exists.mockReturnValue(Promise.resolve(true))
systemUnderTest.copyAttachment = jest.fn(() => Promise.resolve(dummyFoundFileName))
systemUnderTest.generateFileNotFoundMarkdown = jest.fn(() => fileNotFoundMD)
return systemUnderTest.handleAttachmentLinkPaste(storageKey, newNoteKey, pasteText)
.then((returnedPastedText) => {
expect(returnedPastedText).toBe(expectedPastText)
})
})
it('should test that handleAttachmentLinkPaste returns the correct modified paste text if one file was found and one was not found', function () {
const dummyStorage = {path: 'dummyStoragePath'}
findStorage.findStorage = jest.fn(() => dummyStorage)
const pastedNoteKey = 'b1e06f81-8266-49b9-b438-084003c2e723'
const newNoteKey = 'abc234-8266-49b9-b438-084003c2e723'
const dummyFoundFileName = 'dummyFileName'
const fileNotFoundMD = 'file not found'
const pasteText = 'text ![alt](' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + pastedNoteKey + path.sep + 'pdf.pdf) .. ' +
'![secondAttachment](' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + pastedNoteKey + path.sep + 'img.jpg)'
const storageKey = 'storageKey'
const expectedPastText = 'text ![alt](' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + newNoteKey + path.sep + dummyFoundFileName + ') .. ' + fileNotFoundMD
sander.exists = jest.fn()
sander.exists.mockReturnValueOnce(Promise.resolve(true))
sander.exists.mockReturnValue(Promise.resolve(false))
systemUnderTest.copyAttachment = jest.fn(() => Promise.resolve(dummyFoundFileName))
systemUnderTest.generateFileNotFoundMarkdown = jest.fn(() => fileNotFoundMD)
return systemUnderTest.handleAttachmentLinkPaste(storageKey, newNoteKey, pasteText)
.then((returnedPastedText) => {
expect(returnedPastedText).toBe(expectedPastText)
})
})