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

migrate more tests to jest

This commit is contained in:
ZeroX-DG
2020-05-08 20:13:39 +12:00
parent d269f1e8fd
commit 634fec39c0
3 changed files with 35 additions and 37 deletions

View File

@@ -1,4 +1,3 @@
const test = require('ava')
const createNoteFromUrl = require('browser/main/lib/dataApi/createNoteFromUrl')
global.document = require('jsdom').jsdom('<body></body>')
@@ -18,32 +17,34 @@ const CSON = require('@rokt33r/season')
const storagePath = path.join(os.tmpdir(), 'test/create-note-from-url')
test.beforeEach(t => {
t.context.storage = TestDummy.dummyStorage(storagePath)
localStorage.setItem('storages', JSON.stringify([t.context.storage.cache]))
let storageContext
beforeEach(() => {
storageContext = TestDummy.dummyStorage(storagePath)
localStorage.setItem('storages', JSON.stringify([storageContext.cache]))
})
test.serial('Create a note from URL', t => {
const storageKey = t.context.storage.cache.key
const folderKey = t.context.storage.json.folders[0].key
it('Create a note from URL', () => {
const storageKey = storageContext.cache.key
const folderKey = storageContext.json.folders[0].key
const url = 'https://shapeshed.com/writing-cross-platform-node/'
return createNoteFromUrl(url, storageKey, folderKey).then(function assert({
note
}) {
t.is(storageKey, note.storage)
expect(storageKey).toEqual(note.storage)
const jsonData = CSON.readFileSync(
path.join(storagePath, 'notes', note.key + '.cson')
)
// Test if saved content is matching the created in memory note
t.is(note.content, jsonData.content)
t.is(note.tags.length, jsonData.tags.length)
expect(note.content).toEqual(jsonData.content)
expect(note.tags.length).toEqual(jsonData.tags.length)
})
})
test.after(function after() {
afterAll(function after() {
localStorage.clear()
sander.rimrafSync(storagePath)
})

View File

@@ -1,4 +1,3 @@
const test = require('ava')
const createSnippet = require('browser/main/lib/dataApi/createSnippet')
const sander = require('sander')
const os = require('os')
@@ -7,29 +6,27 @@ const path = require('path')
const snippetFilePath = path.join(os.tmpdir(), 'test', 'create-snippet')
const snippetFile = path.join(snippetFilePath, 'snippets.json')
test.beforeEach(t => {
beforeEach(() => {
sander.writeFileSync(snippetFile, '[]')
})
test.serial('Create a snippet', t => {
it('Create a snippet', () => {
return Promise.resolve()
.then(function doTest() {
return Promise.all([createSnippet(snippetFile)])
})
.then(() => 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)
t.deepEqual(snippet.linesHighlighted, data.linesHighlighted)
expect(snippet).not.toBeUndefined()
expect(snippet.name).toEqual(data.name)
expect(snippet.prefix).toEqual(data.prefix)
expect(snippet.content).toEqual(data.content)
expect(snippet.linesHighlighted).toEqual(data.linesHighlighted)
})
})
test.after.always(() => {
afterAll(() => {
sander.rimrafSync(snippetFilePath)
})

View File

@@ -1,4 +1,3 @@
const test = require('ava')
const deleteFolder = require('browser/main/lib/dataApi/deleteFolder')
const attachmentManagement = require('browser/main/lib/dataApi/attachmentManagement')
const createNote = require('browser/main/lib/dataApi/createNote')
@@ -23,14 +22,16 @@ const CSON = require('@rokt33r/season')
const storagePath = path.join(os.tmpdir(), 'test/delete-folder')
test.beforeEach(t => {
t.context.storage = TestDummy.dummyStorage(storagePath)
localStorage.setItem('storages', JSON.stringify([t.context.storage.cache]))
let storageContext
beforeEach(() => {
storageContext = TestDummy.dummyStorage(storagePath)
localStorage.setItem('storages', JSON.stringify([storageContext.cache]))
})
test.serial('Delete a folder', t => {
const storageKey = t.context.storage.cache.key
const folderKey = t.context.storage.json.folders[0].key
it('Delete a folder', () => {
const storageKey = storageContext.cache.key
const folderKey = storageContext.json.folders[0].key
let noteKey
const input1 = {
@@ -72,16 +73,15 @@ test.serial('Delete a folder', t => {
return deleteFolder(storageKey, folderKey)
})
.then(function assert(data) {
t.true(_.find(data.storage.folders, { key: folderKey }) == null)
expect(_.find(data.storage.folders, { key: folderKey })).toBeUndefined()
const jsonData = CSON.readFileSync(
path.join(data.storage.path, 'boostnote.json')
)
t.true(_.find(jsonData.folders, { key: folderKey }) == null)
expect(_.find(jsonData.folders, { key: folderKey })).toBeUndefined()
const notePaths = sander.readdirSync(data.storage.path, 'notes')
t.is(
notePaths.length,
t.context.storage.notes.filter(note => note.folder !== folderKey).length
expect(notePaths.length).toBe(
storageContext.notes.filter(note => note.folder !== folderKey).length
)
const attachmentFolderPath = path.join(
@@ -89,11 +89,11 @@ test.serial('Delete a folder', t => {
attachmentManagement.DESTINATION_FOLDER,
noteKey
)
t.false(fs.existsSync(attachmentFolderPath))
expect(fs.existsSync(attachmentFolderPath)).toBe(false)
})
})
test.after.always(function after() {
afterAll(() => {
localStorage.clear()
sander.rimrafSync(storagePath)
})