|
| 1 | +const test = require('ava') |
| 2 | +const createNote = require('browser/main/lib/dataApi/createNote') |
| 3 | +const deleteNote = require('browser/main/lib/dataApi/deleteNote') |
| 4 | + |
| 5 | +global.document = require('jsdom').jsdom('<body></body>') |
| 6 | +global.window = document.defaultView |
| 7 | +global.navigator = window.navigator |
| 8 | + |
| 9 | +const Storage = require('dom-storage') |
| 10 | +const localStorage = window.localStorage = global.localStorage = new Storage(null, { strict: true }) |
| 11 | +const path = require('path') |
| 12 | +const TestDummy = require('../fixtures/TestDummy') |
| 13 | +const sander = require('sander') |
| 14 | +const os = require('os') |
| 15 | +const CSON = require('season') |
| 16 | +const faker = require('faker') |
| 17 | + |
| 18 | +const storagePath = path.join(os.tmpdir(), 'test/delete-note') |
| 19 | + |
| 20 | +test.beforeEach((t) => { |
| 21 | + t.context.storage = TestDummy.dummyStorage(storagePath) |
| 22 | + localStorage.setItem('storages', JSON.stringify([t.context.storage.cache])) |
| 23 | +}) |
| 24 | + |
| 25 | +test.serial('Create a note', (t) => { |
| 26 | + const storageKey = t.context.storage.cache.key |
| 27 | + const folderKey = t.context.storage.json.folders[0].key |
| 28 | + |
| 29 | + const input1 = { |
| 30 | + type: 'SNIPPET_NOTE', |
| 31 | + description: faker.lorem.lines(), |
| 32 | + snippets: [{ |
| 33 | + name: faker.system.fileName(), |
| 34 | + mode: 'text', |
| 35 | + content: faker.lorem.lines() |
| 36 | + }], |
| 37 | + tags: faker.lorem.words().split(' '), |
| 38 | + folder: folderKey |
| 39 | + } |
| 40 | + input1.title = input1.description.split('\n').shift() |
| 41 | + |
| 42 | + return Promise.resolve() |
| 43 | + .then(function doTest () { |
| 44 | + return createNote(storageKey, input1) |
| 45 | + .then(function (data) { |
| 46 | + return deleteNote(storageKey, data.key) |
| 47 | + }) |
| 48 | + }) |
| 49 | + .then(function assert (data) { |
| 50 | + try { |
| 51 | + CSON.readFileSync(path.join(storagePath, 'notes', data.noteKey + '.cson')) |
| 52 | + t.fail('note cson must be deleted.') |
| 53 | + } catch (err) { |
| 54 | + t.is(err.code, 'ENOENT') |
| 55 | + } |
| 56 | + }) |
| 57 | +}) |
| 58 | + |
| 59 | +test.after(function after () { |
| 60 | + localStorage.clear() |
| 61 | + sander.rimrafSync(storagePath) |
| 62 | +}) |
0 commit comments