diff --git a/browser/main/lib/dataApi/deleteFolder.js b/browser/main/lib/dataApi/deleteFolder.js index 908677e1..0c7486f5 100644 --- a/browser/main/lib/dataApi/deleteFolder.js +++ b/browser/main/lib/dataApi/deleteFolder.js @@ -5,6 +5,7 @@ const resolveStorageNotes = require('./resolveStorageNotes') const CSON = require('@rokt33r/season') const sander = require('sander') const { findStorage } = require('browser/lib/findStorage') +const deleteSingleNote = require('./deleteNote') /** * @param {String} storageKey @@ -49,11 +50,7 @@ function deleteFolder (storageKey, folderKey) { const deleteAllNotes = targetNotes .map(function deleteNote (note) { - const notePath = path.join(storage.path, 'notes', note.key + '.cson') - return sander.unlink(notePath) - .catch(function (err) { - console.warn('Failed to delete', notePath, err) - }) + return deleteSingleNote(storageKey, note.key) }) return Promise.all(deleteAllNotes) .then(() => storage) diff --git a/tests/dataApi/deleteFolder-test.js b/tests/dataApi/deleteFolder-test.js index 1d9c7646..af901896 100644 --- a/tests/dataApi/deleteFolder-test.js +++ b/tests/dataApi/deleteFolder-test.js @@ -1,5 +1,9 @@ const test = require('ava') const deleteFolder = require('browser/main/lib/dataApi/deleteFolder') +const attachmentManagement = require('browser/main/lib/dataApi/attachmentManagement') +const createNote = require('browser/main/lib/dataApi/createNote') +const fs = require('fs') +const faker = require('faker') global.document = require('jsdom').jsdom('') global.window = document.defaultView @@ -24,8 +28,32 @@ test.beforeEach((t) => { test.serial('Delete a folder', (t) => { const storageKey = t.context.storage.cache.key const folderKey = t.context.storage.json.folders[0].key + let noteKey + + const input1 = { + type: 'SNIPPET_NOTE', + description: faker.lorem.lines(), + snippets: [{ + name: faker.system.fileName(), + mode: 'text', + content: faker.lorem.lines() + }], + tags: faker.lorem.words().split(' '), + folder: folderKey + } + input1.title = input1.description.split('\n').shift() return Promise.resolve() + .then(function prepare () { + return createNote(storageKey, input1) + .then(function createAttachmentFolder (data) { + fs.mkdirSync(path.join(storagePath, attachmentManagement.DESTINATION_FOLDER)) + fs.mkdirSync(path.join(storagePath, attachmentManagement.DESTINATION_FOLDER, data.key)) + noteKey = data.key + + return data + }) + }) .then(function doTest () { return deleteFolder(storageKey, folderKey) }) @@ -36,6 +64,9 @@ test.serial('Delete a folder', (t) => { t.true(_.find(jsonData.folders, {key: folderKey}) == null) const notePaths = sander.readdirSync(data.storage.path, 'notes') t.is(notePaths.length, t.context.storage.notes.filter((note) => note.folder !== folderKey).length) + + const attachmentFolderPath = path.join(storagePath, attachmentManagement.DESTINATION_FOLDER, noteKey) + t.false(fs.existsSync(attachmentFolderPath)) }) }) diff --git a/tests/dataApi/exportFolder-test.js b/tests/dataApi/exportFolder-test.js index ee6fb898..fb4aaa7b 100644 --- a/tests/dataApi/exportFolder-test.js +++ b/tests/dataApi/exportFolder-test.js @@ -13,6 +13,7 @@ const TestDummy = require('../fixtures/TestDummy') const os = require('os') const faker = require('faker') const fs = require('fs') +const sander = require('sander') const storagePath = path.join(os.tmpdir(), 'test/export-note') @@ -60,3 +61,8 @@ test.serial('Export a folder', (t) => { t.false(fs.existsSync(filePath)) }) }) + +test.after.always(function after () { + localStorage.clear() + sander.rimrafSync(storagePath) +})