mirror of
https://github.com/BoostIo/Boostnote
synced 2025-12-14 02:06:29 +00:00
updateFolder
This commit is contained in:
55
browser/main/lib/dataApi/updateFolder.js
Normal file
55
browser/main/lib/dataApi/updateFolder.js
Normal file
@@ -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
|
||||||
46
tests/dataApi/updateFolder.js
Normal file
46
tests/dataApi/updateFolder.js
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
const test = require('ava')
|
||||||
|
const updateFolder = require('browser/main/lib/dataApi/updateFolder')
|
||||||
|
|
||||||
|
global.document = require('jsdom').jsdom('<body></body>')
|
||||||
|
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)
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user