1
0
mirror of https://github.com/BoostIo/Boostnote synced 2025-12-13 17:56:25 +00:00

extract renameStorage, removeStorage methods

and tests for each of them
This commit is contained in:
Dick Choi
2016-08-26 10:26:22 +09:00
parent eacd01e77e
commit c5414aadd1
6 changed files with 144 additions and 1 deletions

View File

@@ -3,6 +3,15 @@ const _ = require('lodash')
const sander = require('sander')
const path = require('path')
/**
* @return {Object} all storages and notes
* ```
* {
* storages: [...],
* notes: [...]
* }
* ```
*/
function init () {
let fetchStorages = function () {
let rawStorages

View File

@@ -0,0 +1,28 @@
const _ = require('lodash')
/**
* @param {String} key
* @return {key}
*/
function removeStorage (key) {
let rawStorages
try {
rawStorages = JSON.parse(localStorage.getItem('storages'))
if (!_.isArray(rawStorages)) throw new Error('invalid storages')
} catch (e) {
console.warn(e)
rawStorages = []
}
rawStorages = rawStorages
.filter(function excludeTargetStorage (rawStorage) {
return rawStorage.key !== key
})
localStorage.setItem('storages', JSON.stringify(rawStorages))
return Promise.resolve(key)
}
module.exports = removeStorage

View File

@@ -0,0 +1,33 @@
const _ = require('lodash')
/**
* @param {String} key
* @param {String} name
* @return {Object} Storage meta data
*/
function renameStorage (key, name) {
if (!_.isString(name)) return Promise.reject(new Error('Name must be a string.'))
let rawStorages
try {
rawStorages = JSON.parse(localStorage.getItem('storages'))
if (!_.isArray(rawStorages)) throw new Error('invalid storages')
} catch (e) {
console.warn(e)
rawStorages = []
}
let targetStorage
for (let i = 0; i < rawStorages.length; i++) {
if (rawStorages[i].key === key) {
rawStorages[i].name = name
targetStorage = rawStorages[i]
}
}
localStorage.setItem('storages', JSON.stringify(rawStorages))
return Promise.resolve(targetStorage)
}
module.exports = renameStorage

View File

@@ -58,7 +58,6 @@ class StoragesTab extends React.Component {
return <StorageItem
key={storage.key}
storage={storage}
test={true}
hostBoundingBox={boundingBox}
/>
})

View File

@@ -0,0 +1,36 @@
const test = require('ava')
const removeStorage = require('browser/main/lib/dataApi/removeStorage')
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 crypto = require('crypto')
test('Remove a storage', (t) => {
const dummyStoragePath = path.join(__dirname, '..', 'dummy/dummyStorage')
const dummyStorageKey = crypto.randomBytes(6).toString('hex')
const dummyRawStorage = {
name: 'test1',
key: dummyStorageKey,
path: dummyStoragePath
}
return Promise.resolve()
.then(function before () {
localStorage.setItem('storages', JSON.stringify([dummyRawStorage]))
})
.then(function test () {
return removeStorage(dummyStorageKey)
})
.then(function assert (data) {
t.is(JSON.parse(localStorage.getItem('storages')).length, 0)
})
})
test.after(function after () {
localStorage.clear()
})

View File

@@ -0,0 +1,38 @@
const test = require('ava')
const renameStorage = require('browser/main/lib/dataApi/renameStorage')
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 crypto = require('crypto')
const _ = require('lodash')
test('Rename a storage', (t) => {
const dummyStoragePath = path.join(__dirname, '..', 'dummy/dummyStorage')
const dummyStorageKey = crypto.randomBytes(6).toString('hex')
const dummyRawStorage = {
name: 'test1',
key: dummyStorageKey,
path: dummyStoragePath
}
return Promise.resolve()
.then(function before () {
localStorage.setItem('storages', JSON.stringify([dummyRawStorage]))
})
.then(function test () {
return renameStorage(dummyStorageKey, 'test2')
})
.then(function assert (data) {
let rawStorages = JSON.parse(localStorage.getItem('storages'))
t.true(_.find(rawStorages, {key: dummyStorageKey}).name === 'test2')
})
})
test.after(function after () {
localStorage.clear()
})