mirror of
https://github.com/BoostIo/Boostnote
synced 2025-12-13 01:36:22 +00:00
renew init method
This commit is contained in:
@@ -8,68 +8,58 @@ 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 TestDummy = require('../fixtures/TestDummy')
|
||||
const keygen = require('browser/lib/keygen')
|
||||
const sander = require('sander')
|
||||
const _ = require('lodash')
|
||||
const os = require('os')
|
||||
|
||||
const v1StoragePath = path.join(os.tmpdir(), 'test/init-v1-storage')
|
||||
const legacyStoragePath = path.join(os.tmpdir(), 'test/init-legacy-storage')
|
||||
const emptyDirPath = path.join(os.tmpdir(), 'test/init-empty-storage')
|
||||
|
||||
test.beforeEach((t) => {
|
||||
localStorage.clear()
|
||||
// Prepare 3 types of dir
|
||||
t.context.v1StorageData = TestDummy.dummyStorage(v1StoragePath, {cache: {name: 'v1'}})
|
||||
t.context.legacyStorageData = TestDummy.dummyLegacyStorage(legacyStoragePath, {cache: {name: 'legacy'}})
|
||||
t.context.emptyStorageData = {
|
||||
cache: {
|
||||
type: 'FILESYSTEM',
|
||||
name: 'empty',
|
||||
key: keygen(),
|
||||
path: emptyDirPath
|
||||
}
|
||||
}
|
||||
|
||||
localStorage.setItem('storages', JSON.stringify([t.context.v1StorageData.cache, t.context.legacyStorageData.cache, t.context.emptyStorageData.cache]))
|
||||
})
|
||||
|
||||
test.serial('Fetch storages and notes', (t) => {
|
||||
const dummyStoragePath = path.join(__dirname, '..', 'dummy/dummyStorage')
|
||||
const dummyRawStorage = {
|
||||
name: 'test1',
|
||||
key: crypto.randomBytes(6).toString('hex'),
|
||||
path: dummyStoragePath
|
||||
}
|
||||
const dummyFolderKey = 'fc6ba88e8ecf'
|
||||
|
||||
const { v1StorageData, legacyStorageData, emptyStorageData } = t.context
|
||||
return Promise.resolve()
|
||||
.then(function before () {
|
||||
localStorage.setItem('storages', JSON.stringify([dummyRawStorage]))
|
||||
})
|
||||
.then(function test () {
|
||||
return init()
|
||||
})
|
||||
.then(function assert (data) {
|
||||
t.true(Array.isArray(data.storages))
|
||||
var targetStorage = data.storages.filter((storage) => storage.key === dummyRawStorage.key)[0]
|
||||
t.not(targetStorage, null)
|
||||
t.is(targetStorage.name, dummyRawStorage.name)
|
||||
t.is(targetStorage.key, dummyRawStorage.key)
|
||||
t.is(targetStorage.path, dummyRawStorage.path)
|
||||
t.is(data.notes.length, 2)
|
||||
data.notes.forEach((note) => {
|
||||
t.is(note.folder, dummyFolderKey)
|
||||
t.is(data.notes.length, v1StorageData.notes.length + legacyStorageData.notes.length)
|
||||
t.is(data.storages.length, 3)
|
||||
data.storages.forEach(function assertStorage (storage) {
|
||||
t.true(_.isString(storage.key))
|
||||
t.true(_.isString(storage.name))
|
||||
t.true(storage.type === 'FILESYSTEM')
|
||||
t.true(_.isString(storage.path))
|
||||
})
|
||||
|
||||
t.true(Array.isArray(data.notes))
|
||||
})
|
||||
.then(function after () {
|
||||
localStorage.clear()
|
||||
})
|
||||
})
|
||||
|
||||
test.serial('If storage path is a empty folder, return metadata with empty folder array and empty note array.', (t) => {
|
||||
const emptyFolderPath = path.join(__dirname, '..', 'dummy/empty')
|
||||
const dummyRawStorage = {
|
||||
name: 'test2',
|
||||
key: crypto.randomBytes(6).toString('hex'),
|
||||
path: emptyFolderPath
|
||||
}
|
||||
return Promise.resolve()
|
||||
.then(function before () {
|
||||
localStorage.setItem('storages', JSON.stringify([dummyRawStorage]))
|
||||
})
|
||||
.then(function test () {
|
||||
return init()
|
||||
})
|
||||
.then(function assert (data) {
|
||||
t.true(Array.isArray(data.storages))
|
||||
var targetStorage = data.storages.filter((storage) => storage.key === dummyRawStorage.key)[0]
|
||||
t.not(targetStorage, null)
|
||||
t.is(targetStorage.name, dummyRawStorage.name)
|
||||
t.is(targetStorage.key, dummyRawStorage.key)
|
||||
t.is(targetStorage.path, dummyRawStorage.path)
|
||||
|
||||
t.true(Array.isArray(data.notes))
|
||||
})
|
||||
.then(function after () {
|
||||
localStorage.clear()
|
||||
})
|
||||
test.after.always(() => {
|
||||
localStorage.clear()
|
||||
sander.rimrafSync(v1StoragePath)
|
||||
sander.rimrafSync(legacyStoragePath)
|
||||
sander.rimrafSync(emptyDirPath)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user