1
0
mirror of https://github.com/BoostIo/Boostnote synced 2025-12-13 09:46:22 +00:00
Files
Boostnote/tests/dataApi/addStorage.js
Dick Choi 8d624459d4 add assertions to addStorage test
check version and folders attribute from resolved data and json
2016-08-28 00:20:08 +09:00

73 lines
2.4 KiB
JavaScript

const test = require('ava')
const addStorage = require('browser/main/lib/dataApi/addStorage')
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 _ = require('lodash')
const os = require('os')
const CSON = require('season')
const v1StoragePath = path.join(os.tmpdir(), 'test/addStorage-v1-storage')
// const legacyStoragePath = path.join(os.tmpdir(), 'test/addStorage-legacy-storage')
// const emptyDirPath = path.join(os.tmpdir(), 'test/addStorage-empty-storage')
test.beforeEach((t) => {
t.context.v1StorageData = TestDummy.dummyStorage(v1StoragePath)
// t.context.legacyStorageData = TestDummy.dummyLegacyStorage(legacyStoragePath)
localStorage.setItem('storages', JSON.stringify([]))
})
test.serial('Add Storage', (t) => {
const input = {
type: 'FILESYSTEM',
name: 'add-storage1',
path: v1StoragePath
}
return Promise.resolve()
.then(function doTest () {
return addStorage(input)
})
.then(function validateResult (data) {
let { storage, notes } = data
// Check data.storage
t.true(_.isString(storage.key))
t.is(storage.name, input.name)
t.is(storage.type, input.type)
t.is(storage.path, input.path)
t.is(storage.version, '1.0')
t.is(storage.folders.length, t.context.v1StorageData.json.folders.length)
// Check data.notes
t.is(notes.length, t.context.v1StorageData.notes.length)
notes.forEach(function validateNote (note) {
t.is(note.storage, storage.key)
})
// Check localStorage
let cacheData = _.find(JSON.parse(localStorage.getItem('storages')), {key: data.storage.key})
t.is(cacheData.name, input.name)
t.is(cacheData.type, input.type)
t.is(cacheData.path, input.path)
// Check boostnote.json
let jsonData = CSON.readFileSync(path.join(storage.path, 'boostnote.json'))
t.true(_.isArray(jsonData.folders))
t.is(jsonData.version, '1.0')
t.is(jsonData.folders.length, t.context.v1StorageData.json.folders.length)
})
})
test.after.always(() => {
localStorage.clear()
sander.rimrafSync(v1StoragePath)
})