mirror of
https://github.com/BoostIo/Boostnote
synced 2025-12-13 17:56:25 +00:00
refactored snippet dataApi for easy testing and added some test. Fixed old snippet still display when deleted
This commit is contained in:
@@ -3,20 +3,22 @@ import crypto from 'crypto'
|
|||||||
import consts from 'browser/lib/consts'
|
import consts from 'browser/lib/consts'
|
||||||
import fetchSnippet from 'browser/main/lib/dataApi/fetchSnippet'
|
import fetchSnippet from 'browser/main/lib/dataApi/fetchSnippet'
|
||||||
|
|
||||||
function createSnippet () {
|
function createSnippet (snippetFile) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
fetchSnippet().then((snippets) => {
|
const newSnippet = {
|
||||||
const newSnippet = {
|
id: crypto.randomBytes(16).toString('hex'),
|
||||||
id: crypto.randomBytes(16).toString('hex'),
|
name: 'Unnamed snippet',
|
||||||
name: 'Unnamed snippet',
|
prefix: [],
|
||||||
prefix: [],
|
content: ''
|
||||||
content: ''
|
}
|
||||||
}
|
fetchSnippet(null, snippetFile).then((snippets) => {
|
||||||
snippets.push(newSnippet)
|
snippets.push(newSnippet)
|
||||||
fs.writeFile(consts.SNIPPET_FILE, JSON.stringify(snippets, null, 4), (err) => {
|
fs.writeFile(snippetFile || consts.SNIPPET_FILE, JSON.stringify(snippets, null, 4), (err) => {
|
||||||
if (err) reject(err)
|
if (err) reject(err)
|
||||||
resolve(newSnippet)
|
resolve(newSnippet)
|
||||||
})
|
})
|
||||||
|
}).catch((err) => {
|
||||||
|
reject(err)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,11 +2,11 @@ import fs from 'fs'
|
|||||||
import consts from 'browser/lib/consts'
|
import consts from 'browser/lib/consts'
|
||||||
import fetchSnippet from 'browser/main/lib/dataApi/fetchSnippet'
|
import fetchSnippet from 'browser/main/lib/dataApi/fetchSnippet'
|
||||||
|
|
||||||
function deleteSnippet (snippet) {
|
function deleteSnippet (snippet, snippetFile) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
fetchSnippet().then((snippets) => {
|
fetchSnippet(null, snippetFile).then((snippets) => {
|
||||||
snippets = snippets.filter(currentSnippet => currentSnippet.id !== snippet.id)
|
snippets = snippets.filter(currentSnippet => currentSnippet.id !== snippet.id)
|
||||||
fs.writeFile(consts.SNIPPET_FILE, JSON.stringify(snippets, null, 4), (err) => {
|
fs.writeFile(snippetFile || consts.SNIPPET_FILE, JSON.stringify(snippets, null, 4), (err) => {
|
||||||
if (err) reject(err)
|
if (err) reject(err)
|
||||||
resolve(snippet)
|
resolve(snippet)
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -2,9 +2,9 @@ import fs from 'fs'
|
|||||||
import crypto from 'crypto'
|
import crypto from 'crypto'
|
||||||
import consts from 'browser/lib/consts'
|
import consts from 'browser/lib/consts'
|
||||||
|
|
||||||
function fetchSnippet (id) {
|
function fetchSnippet (id, snippetFile) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
fs.readFile(consts.SNIPPET_FILE, 'utf8', (err, data) => {
|
fs.readFile(snippetFile || consts.SNIPPET_FILE, 'utf8', (err, data) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
reject(err)
|
reject(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
import fs from 'fs'
|
import fs from 'fs'
|
||||||
import consts from 'browser/lib/consts'
|
import consts from 'browser/lib/consts'
|
||||||
|
|
||||||
function updateSnippet (snippet) {
|
function updateSnippet (snippet, snippetFile) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const snippets = JSON.parse(fs.readFileSync(consts.SNIPPET_FILE, 'utf-8'))
|
const snippets = JSON.parse(fs.readFileSync(snippetFile || consts.SNIPPET_FILE, 'utf-8'))
|
||||||
|
|
||||||
for (let i = 0; i < snippets.length; i++) {
|
for (let i = 0; i < snippets.length; i++) {
|
||||||
const currentSnippet = snippets[i]
|
const currentSnippet = snippets[i]
|
||||||
@@ -20,7 +20,7 @@ function updateSnippet (snippet) {
|
|||||||
currentSnippet.name = snippet.name
|
currentSnippet.name = snippet.name
|
||||||
currentSnippet.prefix = snippet.prefix
|
currentSnippet.prefix = snippet.prefix
|
||||||
currentSnippet.content = snippet.content
|
currentSnippet.content = snippet.content
|
||||||
fs.writeFile(consts.SNIPPET_FILE, JSON.stringify(snippets, null, 4), (err) => {
|
fs.writeFile(snippetFile || consts.SNIPPET_FILE, JSON.stringify(snippets, null, 4), (err) => {
|
||||||
if (err) reject(err)
|
if (err) reject(err)
|
||||||
resolve(snippets)
|
resolve(snippets)
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -63,6 +63,10 @@ class SnippetTab extends React.Component {
|
|||||||
deleteSnippet (snippet) {
|
deleteSnippet (snippet) {
|
||||||
dataApi.deleteSnippet(snippet).then(() => {
|
dataApi.deleteSnippet(snippet).then(() => {
|
||||||
this.reloadSnippetList()
|
this.reloadSnippetList()
|
||||||
|
// prevent old snippet still display when deleted
|
||||||
|
if (snippet.id === this.state.currentSnippet.id) {
|
||||||
|
this.setState({currentSnippet: null})
|
||||||
|
}
|
||||||
}).catch(err => { throw err })
|
}).catch(err => { throw err })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
34
tests/dataApi/createSnippet-test.js
Normal file
34
tests/dataApi/createSnippet-test.js
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
const test = require('ava')
|
||||||
|
const createSnippet = require('browser/main/lib/dataApi/createSnippet')
|
||||||
|
const sander = require('sander')
|
||||||
|
const os = require('os')
|
||||||
|
const path = require('path')
|
||||||
|
|
||||||
|
const snippetFilePath = path.join(os.tmpdir(), `test${path.sep}create-snippet`)
|
||||||
|
const snippetFile = path.join(snippetFilePath, 'snippets.json')
|
||||||
|
|
||||||
|
test.beforeEach((t) => {
|
||||||
|
sander.writeFileSync(snippetFile, '[]')
|
||||||
|
})
|
||||||
|
|
||||||
|
test.serial('Create a snippet', (t) => {
|
||||||
|
return Promise.resolve()
|
||||||
|
.then(function doTest () {
|
||||||
|
return Promise.all([
|
||||||
|
createSnippet(snippetFile)
|
||||||
|
])
|
||||||
|
})
|
||||||
|
.then(function assert (data) {
|
||||||
|
data = data[0]
|
||||||
|
const snippets = JSON.parse(sander.readFileSync(snippetFile))
|
||||||
|
const snippet = snippets.find(currentSnippet => currentSnippet.id === data.id)
|
||||||
|
t.not(snippet, undefined)
|
||||||
|
t.is(snippet.name, data.name)
|
||||||
|
t.deepEqual(snippet.prefix, data.prefix)
|
||||||
|
t.is(snippet.content, data.content)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
test.after.always(() => {
|
||||||
|
sander.rimrafSync(snippetFilePath)
|
||||||
|
})
|
||||||
38
tests/dataApi/deleteSnippet-test.js
Normal file
38
tests/dataApi/deleteSnippet-test.js
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
const test = require('ava')
|
||||||
|
const deleteSnippet = require('browser/main/lib/dataApi/deleteSnippet')
|
||||||
|
const sander = require('sander')
|
||||||
|
const os = require('os')
|
||||||
|
const path = require('path')
|
||||||
|
const crypto = require('crypto')
|
||||||
|
|
||||||
|
const snippetFilePath = path.join(os.tmpdir(), `test${path.sep}delete-snippet`)
|
||||||
|
const snippetFile = path.join(snippetFilePath, 'snippets.json')
|
||||||
|
const newSnippet = {
|
||||||
|
id: crypto.randomBytes(16).toString('hex'),
|
||||||
|
name: 'Unnamed snippet',
|
||||||
|
prefix: [],
|
||||||
|
content: ''
|
||||||
|
}
|
||||||
|
|
||||||
|
test.beforeEach((t) => {
|
||||||
|
sander.writeFileSync(snippetFile, JSON.stringify([newSnippet]))
|
||||||
|
})
|
||||||
|
|
||||||
|
test.serial('Delete a snippet', (t) => {
|
||||||
|
return Promise.resolve()
|
||||||
|
.then(function doTest () {
|
||||||
|
return Promise.all([
|
||||||
|
deleteSnippet(newSnippet, snippetFile)
|
||||||
|
])
|
||||||
|
})
|
||||||
|
.then(function assert (data) {
|
||||||
|
data = data[0]
|
||||||
|
const snippets = JSON.parse(sander.readFileSync(snippetFile))
|
||||||
|
const snippet = snippets.find(currentSnippet => currentSnippet.id === data.id)
|
||||||
|
t.is(snippets.length, 0)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
test.after.always(() => {
|
||||||
|
sander.rimrafSync(snippetFilePath)
|
||||||
|
})
|
||||||
48
tests/dataApi/updateSnippet-test.js
Normal file
48
tests/dataApi/updateSnippet-test.js
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
const test = require('ava')
|
||||||
|
const updateSnippet = require('browser/main/lib/dataApi/updateSnippet')
|
||||||
|
const sander = require('sander')
|
||||||
|
const os = require('os')
|
||||||
|
const path = require('path')
|
||||||
|
const crypto = require('crypto')
|
||||||
|
|
||||||
|
const snippetFilePath = path.join(os.tmpdir(), `test${path.sep}update-snippet`)
|
||||||
|
const snippetFile = path.join(snippetFilePath, 'snippets.json')
|
||||||
|
const oldSnippet = {
|
||||||
|
id: crypto.randomBytes(16).toString('hex'),
|
||||||
|
name: 'Initial snippet',
|
||||||
|
prefix: [],
|
||||||
|
content: ''
|
||||||
|
}
|
||||||
|
|
||||||
|
const newSnippet = {
|
||||||
|
id: oldSnippet.id,
|
||||||
|
name: 'new name',
|
||||||
|
prefix: ['prefix'],
|
||||||
|
content: 'new content'
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
test.beforeEach((t) => {
|
||||||
|
sander.writeFileSync(snippetFile, JSON.stringify([oldSnippet]))
|
||||||
|
})
|
||||||
|
|
||||||
|
test.serial('Update a snippet', (t) => {
|
||||||
|
return Promise.resolve()
|
||||||
|
.then(function doTest () {
|
||||||
|
return Promise.all([
|
||||||
|
updateSnippet(newSnippet, snippetFile)
|
||||||
|
])
|
||||||
|
})
|
||||||
|
.then(function assert () {
|
||||||
|
const snippets = JSON.parse(sander.readFileSync(snippetFile))
|
||||||
|
const snippet = snippets.find(currentSnippet => currentSnippet.id === newSnippet.id)
|
||||||
|
t.not(snippet, undefined)
|
||||||
|
t.is(snippet.name, newSnippet.name)
|
||||||
|
t.deepEqual(snippet.prefix, newSnippet.prefix)
|
||||||
|
t.is(snippet.content, newSnippet.content)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
test.after.always(() => {
|
||||||
|
sander.rimrafSync(snippetFilePath)
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user