1
0
mirror of https://github.com/BoostIo/Boostnote synced 2025-12-14 02:06:29 +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') const createNoteFromUrl = require('browser/main/lib/dataApi/createNoteFromUrl')
global.document = require('jsdom').jsdom('<body></body>') 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') const storagePath = path.join(os.tmpdir(), 'test/create-note-from-url')
test.beforeEach(t => { let storageContext
t.context.storage = TestDummy.dummyStorage(storagePath)
localStorage.setItem('storages', JSON.stringify([t.context.storage.cache])) beforeEach(() => {
storageContext = TestDummy.dummyStorage(storagePath)
localStorage.setItem('storages', JSON.stringify([storageContext.cache]))
}) })
test.serial('Create a note from URL', t => { it('Create a note from URL', () => {
const storageKey = t.context.storage.cache.key const storageKey = storageContext.cache.key
const folderKey = t.context.storage.json.folders[0].key const folderKey = storageContext.json.folders[0].key
const url = 'https://shapeshed.com/writing-cross-platform-node/' const url = 'https://shapeshed.com/writing-cross-platform-node/'
return createNoteFromUrl(url, storageKey, folderKey).then(function assert({ return createNoteFromUrl(url, storageKey, folderKey).then(function assert({
note note
}) { }) {
t.is(storageKey, note.storage) expect(storageKey).toEqual(note.storage)
const jsonData = CSON.readFileSync( const jsonData = CSON.readFileSync(
path.join(storagePath, 'notes', note.key + '.cson') path.join(storagePath, 'notes', note.key + '.cson')
) )
// Test if saved content is matching the created in memory note // Test if saved content is matching the created in memory note
t.is(note.content, jsonData.content) expect(note.content).toEqual(jsonData.content)
t.is(note.tags.length, jsonData.tags.length) expect(note.tags.length).toEqual(jsonData.tags.length)
}) })
}) })
test.after(function after() { afterAll(function after() {
localStorage.clear() localStorage.clear()
sander.rimrafSync(storagePath) sander.rimrafSync(storagePath)
}) })

View File

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

View File

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