From ff123be8952c3a2acd792773e4202cd24561bcdd Mon Sep 17 00:00:00 2001 From: Dick Choi Date: Sat, 27 Aug 2016 17:38:43 +0900 Subject: [PATCH] updateFolder --- browser/main/lib/dataApi/updateFolder.js | 55 ++++++++++++++++++++++++ tests/dataApi/updateFolder.js | 46 ++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 browser/main/lib/dataApi/updateFolder.js create mode 100644 tests/dataApi/updateFolder.js diff --git a/browser/main/lib/dataApi/updateFolder.js b/browser/main/lib/dataApi/updateFolder.js new file mode 100644 index 00000000..b6dd4485 --- /dev/null +++ b/browser/main/lib/dataApi/updateFolder.js @@ -0,0 +1,55 @@ +const _ = require('lodash') +const path = require('path') +const resolveStorageData = require('./resolveStorageData') +const CSON = require('season') + +/** + * @param {String} storageKey + * @param {String} folderKey + * @param {Object} input + * ``` + * { + * color: String, + * name: String + * } + * ``` + * + * @return {Object} + * ``` + * { + * storage: Object + * } + * ``` + */ +function updateFolder (storageKey, folderKey, input) { + let rawStorages + let targetStorage + try { + if (input == null) throw new Error('No input found.') + if (!_.isString(input.name)) throw new Error('Name must be a string.') + if (!_.isString(input.color)) throw new Error('Color must be a string.') + + rawStorages = JSON.parse(localStorage.getItem('storages')) + if (!_.isArray(rawStorages)) throw new Error('Target storage doesn\'t exist.') + + targetStorage = _.find(rawStorages, {key: storageKey}) + if (targetStorage == null) throw new Error('Target storage doesn\'t exist.') + } catch (e) { + return Promise.reject(e) + } + + return resolveStorageData(targetStorage) + .then(function updateFolder (storage) { + let targetFolder = _.find(storage.folders, {key: folderKey}) + targetFolder.name = input.name + targetFolder.color = input.color + + CSON.writeFileSync(path.join(storage.path, 'boostnote.json'), _.pick(storage, ['folders', 'version'])) + + return { + storage + } + }) +} + +module.exports = updateFolder diff --git a/tests/dataApi/updateFolder.js b/tests/dataApi/updateFolder.js new file mode 100644 index 00000000..c8aacb60 --- /dev/null +++ b/tests/dataApi/updateFolder.js @@ -0,0 +1,46 @@ +const test = require('ava') +const updateFolder = require('browser/main/lib/dataApi/updateFolder') + +global.document = require('jsdom').jsdom('') +global.window = document.defaultView +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 _ = require('lodash') +const TestDummy = require('../fixtures/TestDummy') +const sander = require('sander') +const os = require('os') +const CSON = require('season') + +const storagePath = path.join(os.tmpdir(), 'test/update-folder') + +test.beforeEach((t) => { + t.context.storage = TestDummy.dummyStorage(storagePath) + localStorage.setItem('storages', JSON.stringify([t.context.storage.cache])) +}) + +test.serial('Create a folder', (t) => { + const stoargeKey = t.context.storage.cache.key + const folderKey = t.context.storage.json.folders[0].key + const input = { + name: 'changed', + color: '#FF0000' + } + return Promise.resolve() + .then(function doTest () { + return updateFolder(stoargeKey, folderKey, input) + }) + .then(function assert (data) { + 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(function after () { + localStorage.clear() + sander.rimrafSync(storagePath) +})