mirror of
https://github.com/BoostIo/Boostnote
synced 2025-12-14 10:16:26 +00:00
deleteNote
This commit is contained in:
34
browser/main/lib/dataApi/deleteNote.js
Normal file
34
browser/main/lib/dataApi/deleteNote.js
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
const resolveStorageData = require('./resolveStorageData')
|
||||||
|
const _ = require('lodash')
|
||||||
|
const path = require('path')
|
||||||
|
const sander = require('sander')
|
||||||
|
|
||||||
|
function deleteNote (storageKey, noteKey) {
|
||||||
|
let targetStorage
|
||||||
|
try {
|
||||||
|
let cachedStorageList = JSON.parse(localStorage.getItem('storages'))
|
||||||
|
if (!_.isArray(cachedStorageList)) throw new Error('Target storage doesn\'t exist.')
|
||||||
|
|
||||||
|
targetStorage = _.find(cachedStorageList, {key: storageKey})
|
||||||
|
if (targetStorage == null) throw new Error('Target storage doesn\'t exist.')
|
||||||
|
} catch (e) {
|
||||||
|
return Promise.reject(e)
|
||||||
|
}
|
||||||
|
|
||||||
|
return resolveStorageData(targetStorage)
|
||||||
|
.then(function deleteNoteFile (storage) {
|
||||||
|
let notePath = path.join(storage.path, 'notes', noteKey + '.cson')
|
||||||
|
|
||||||
|
try {
|
||||||
|
sander.unlinkSync(notePath)
|
||||||
|
} catch (err) {
|
||||||
|
console.warn('Failed to delete note cson', err)
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
noteKey,
|
||||||
|
storageKey
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = deleteNote
|
||||||
62
tests/dataApi/deleteNote.js
Normal file
62
tests/dataApi/deleteNote.js
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
const test = require('ava')
|
||||||
|
const createNote = require('browser/main/lib/dataApi/createNote')
|
||||||
|
const deleteNote = require('browser/main/lib/dataApi/deleteNote')
|
||||||
|
|
||||||
|
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 TestDummy = require('../fixtures/TestDummy')
|
||||||
|
const sander = require('sander')
|
||||||
|
const os = require('os')
|
||||||
|
const CSON = require('season')
|
||||||
|
const faker = require('faker')
|
||||||
|
|
||||||
|
const storagePath = path.join(os.tmpdir(), 'test/delete-note')
|
||||||
|
|
||||||
|
test.beforeEach((t) => {
|
||||||
|
t.context.storage = TestDummy.dummyStorage(storagePath)
|
||||||
|
localStorage.setItem('storages', JSON.stringify([t.context.storage.cache]))
|
||||||
|
})
|
||||||
|
|
||||||
|
test.serial('Create a note', (t) => {
|
||||||
|
const storageKey = t.context.storage.cache.key
|
||||||
|
const folderKey = t.context.storage.json.folders[0].key
|
||||||
|
|
||||||
|
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 doTest () {
|
||||||
|
return createNote(storageKey, input1)
|
||||||
|
.then(function (data) {
|
||||||
|
return deleteNote(storageKey, data.key)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.then(function assert (data) {
|
||||||
|
try {
|
||||||
|
CSON.readFileSync(path.join(storagePath, 'notes', data.noteKey + '.cson'))
|
||||||
|
t.fail('note cson must be deleted.')
|
||||||
|
} catch (err) {
|
||||||
|
t.is(err.code, 'ENOENT')
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
test.after(function after () {
|
||||||
|
localStorage.clear()
|
||||||
|
sander.rimrafSync(storagePath)
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user