diff --git a/browser/main/lib/dataApi/createFolder.js b/browser/main/lib/dataApi/createFolder.js index 630d273f..2fb21a73 100644 --- a/browser/main/lib/dataApi/createFolder.js +++ b/browser/main/lib/dataApi/createFolder.js @@ -1,11 +1,8 @@ const _ = require('lodash') -const sander = require('sander') const keygen = require('browser/lib/keygen') const path = require('path') - -const defaultDataJSON = { - notes: [] -} +const resolveStorageData = require('./resolveStorageData') +const CSON = require('season') /** * @param {String} storageKey @@ -17,7 +14,12 @@ const defaultDataJSON = { * } * ``` * - * @return {key} + * @return {Object} + * ``` + * { + * storage: Object + * } + * ``` */ function createFolder (storageKey, input) { let rawStorages @@ -36,49 +38,24 @@ function createFolder (storageKey, input) { return Promise.reject(e) } - const storageData = Object.assign({}, targetStorage) - - const boostnoteJSONPath = path.join(targetStorage.path, 'boostnote.json') - return Promise.resolve() - .then(function fetchBoostnoteJSON () { - return sander.readFile(boostnoteJSONPath) - }) - .then(function updateBoostnoteJSON (data) { - let boostnoteJSON - // If `boostnote.json` is invalid, reset `boostnote.json`. - try { - boostnoteJSON = JSON.parse(data) - if (!_.isArray(boostnoteJSON.folders)) throw new Error('the value of `folders` must be array') - } catch (err) { - boostnoteJSON = { - folders: [] - } - } - + return resolveStorageData(targetStorage) + .then(function createFolder (storage) { let key = keygen() - while (boostnoteJSON.folders.some((folder) => folder.key === key)) { + while (storage.folders.some((folder) => folder.key === key)) { key = keygen() } - let newFolder = { key, color: input.color, name: input.name } - boostnoteJSON.folders.push(newFolder) - storageData.folders = boostnoteJSON.folders + storage.folders.push(newFolder) + + CSON.writeFileSync(path.join(storage.path, 'boostnote.json'), _.pick(storage, ['folders', 'version'])) - return sander.writeFile(boostnoteJSONPath, JSON.stringify(boostnoteJSON)) - .then(() => newFolder) - }) - .then(function createDataJSON (newFolder) { - const folderDirPath = path.join(targetStorage.path, newFolder.key) - return sander.writeFile(folderDirPath, 'data.json', JSON.stringify(defaultDataJSON)) - }) - .then(function returnData () { return { - storage: storageData + storage } }) } diff --git a/tests/dataApi/createFolder.js b/tests/dataApi/createFolder.js index 6c6c19f6..80def1bf 100644 --- a/tests/dataApi/createFolder.js +++ b/tests/dataApi/createFolder.js @@ -1,6 +1,5 @@ const test = require('ava') const createFolder = require('browser/main/lib/dataApi/createFolder') -const sander = require('sander') global.document = require('jsdom').jsdom('') global.window = document.defaultView @@ -9,50 +8,38 @@ global.navigator = window.navigator const Storage = require('dom-storage') const localStorage = window.localStorage = global.localStorage = new Storage(null, { strict: true }) const path = require('path') -const crypto = require('crypto') const _ = require('lodash') +const TestDummy = require('../fixtures/TestDummy') +const sander = require('sander') +const os = require('os') +const CSON = require('season') -function copyFile (filePath, targetPath) { - return sander.readFile(filePath) - .then(function writeFile (data) { - return sander.writeFile(targetPath, data.toString()) - }) -} +const storagePath = path.join(os.tmpdir(), 'test/rename-storage') -const dummyStoragePath = path.join(__dirname, '..', 'dummy/dummyStorage') -const targetPath = path.join(__dirname, '../sandbox/test-add-folder') -const dummyRawStorage = { - name: 'test1', - key: crypto.randomBytes(6).toString('hex'), - path: targetPath -} -test.before(function () { - localStorage.setItem('storages', JSON.stringify([dummyRawStorage])) - - return copyFile(path.join(dummyStoragePath, 'boostnote.json'), path.join(targetPath, 'boostnote.json')) +test.beforeEach((t) => { + t.context.storage = TestDummy.dummyStorage(storagePath) + localStorage.setItem('storages', JSON.stringify([t.context.storage.cache])) }) -const input = { - name: 'test folder', - color: '#FF5555' -} -test('Add note to storage', (t) => { - return createFolder(dummyRawStorage.key, input) +test.serial('Create a folder', (t) => { + const stoargeKey = t.context.storage.cache.key + const input = { + name: 'created', + color: '#ff5555' + } + return Promise.resolve() + .then(function doTest () { + return createFolder(stoargeKey, input) + }) .then(function assert (data) { - t.not(data.storage, null) - - let targetFolder = _.find(data.storage.folders, { - name: input.name, - color: input.color - }) - - t.not(targetFolder, null) - - t.true(sander.statSync(path.join(targetPath, targetFolder.key)).isDirectory()) + t.true(_.find(data.storage.folders, input) != null) + let jsonData = CSON.readFileSync(path.join(data.storage.path, 'boostnote.json')) + console.log(path.join(data.storage.path, 'boostnote.json')) + t.true(_.find(jsonData.folders, input) != null) }) }) -test.after.always(function () { +test.after(function after () { localStorage.clear() - sander.rimrafSync(targetPath) + sander.rimrafSync(storagePath) })