From 5b0b309c4911326a2631053cc21687853f69cc22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Vi=E1=BB=87t=20H=C6=B0ng?= Date: Wed, 28 Aug 2019 21:51:36 +1200 Subject: [PATCH] added test for getAttachmentsPathAndStatus --- tests/dataApi/attachmentManagement.test.js | 66 ++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/tests/dataApi/attachmentManagement.test.js b/tests/dataApi/attachmentManagement.test.js index 0f420bcc..13dcedca 100644 --- a/tests/dataApi/attachmentManagement.test.js +++ b/tests/dataApi/attachmentManagement.test.js @@ -578,6 +578,72 @@ it('should test that deleteAttachmentsNotPresentInNote does nothing if noteKey, expect(fs.unlink).not.toHaveBeenCalled() }) +it('should test that getAttachmentsPathAndStatus return null if noteKey, storageKey or noteContent was undefined', function () { + const noteKey = undefined + const storageKey = undefined + const markdownContent = '' + + const result = systemUnderTest.getAttachmentsPathAndStatus(markdownContent, storageKey, noteKey) + expect(result).toBeNull() +}) + +it('should test that getAttachmentsPathAndStatus return null if noteKey, storageKey or noteContent was null', function () { + const noteKey = null + const storageKey = null + const markdownContent = '' + + const result = systemUnderTest.getAttachmentsPathAndStatus(markdownContent, storageKey, noteKey) + expect(result).toBeNull() +}) + +it('should test that getAttachmentsPathAndStatus return the correct path and status for attachments', async function () { + const dummyStorage = {path: 'dummyStoragePath'} + const noteKey = 'noteKey' + const storageKey = 'storageKey' + const markdownContent = + 'Test input' + + '![' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.win32.sep + noteKey + path.win32.sep + 'file2.pdf](file2.pdf) \n' + const dummyFilesInFolder = ['file1.txt', 'file2.pdf', 'file3.jpg'] + + findStorage.findStorage = jest.fn(() => dummyStorage) + fs.existsSync = jest.fn(() => true) + fs.readdir = jest.fn((paht, callback) => callback(undefined, dummyFilesInFolder)) + fs.unlink = jest.fn() + + const targetStorage = findStorage.findStorage(storageKey) + + const attachments = await systemUnderTest.getAttachmentsPathAndStatus(markdownContent, storageKey, noteKey) + expect(attachments.length).toBe(3) + expect(attachments[0].isInUse).toBe(false) + expect(attachments[1].isInUse).toBe(true) + expect(attachments[2].isInUse).toBe(false) + + expect(attachments[0].path).toBe( + path.join( + targetStorage.path, + systemUnderTest.DESTINATION_FOLDER, + noteKey, + dummyFilesInFolder[0] + ) + ) + expect(attachments[1].path).toBe( + path.join( + targetStorage.path, + systemUnderTest.DESTINATION_FOLDER, + noteKey, + dummyFilesInFolder[1] + ) + ) + expect(attachments[2].path).toBe( + path.join( + targetStorage.path, + systemUnderTest.DESTINATION_FOLDER, + noteKey, + dummyFilesInFolder[2] + ) + ) +}) + it('should test that moveAttachments moves attachments only if the source folder existed', function () { fse.existsSync = jest.fn(() => false) fse.moveSync = jest.fn()