2018-05-03 14:11:45 +02:00
|
|
|
require('app-module-path').addPath(__dirname);
|
|
|
|
|
|
|
|
const { time } = require('lib/time-utils.js');
|
|
|
|
const { asyncTest, fileContentEqual, setupDatabase, setupDatabaseAndSynchronizer, db, synchronizer, fileApi, sleep, clearDatabase, switchClient, syncTargetId, objectsEqual, checkThrowAsync } = require('test-utils.js');
|
|
|
|
const Folder = require('lib/models/Folder.js');
|
|
|
|
const Note = require('lib/models/Note.js');
|
|
|
|
const BaseModel = require('lib/BaseModel.js');
|
2018-11-21 01:18:56 +02:00
|
|
|
const ArrayUtils = require('lib/ArrayUtils.js');
|
2018-05-03 14:11:45 +02:00
|
|
|
const { shim } = require('lib/shim');
|
|
|
|
|
|
|
|
process.on('unhandledRejection', (reason, p) => {
|
|
|
|
console.log('Unhandled Rejection at: Promise', p, 'reason:', reason);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('models_Note', function() {
|
|
|
|
|
|
|
|
beforeEach(async (done) => {
|
|
|
|
await setupDatabaseAndSynchronizer(1);
|
|
|
|
await switchClient(1);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should find resource and note IDs', asyncTest(async () => {
|
|
|
|
let folder1 = await Folder.save({ title: "folder1" });
|
|
|
|
let note1 = await Note.save({ title: 'ma note', parent_id: folder1.id });
|
|
|
|
let note2 = await Note.save({ title: 'ma deuxième note', body: 'Lien vers première note : ' + Note.markdownTag(note1), parent_id: folder1.id });
|
|
|
|
|
|
|
|
let items = await Note.linkedItems(note2.body);
|
|
|
|
expect(items.length).toBe(1);
|
|
|
|
expect(items[0].id).toBe(note1.id);
|
|
|
|
|
|
|
|
await shim.attachFileToNote(note2, __dirname + '/../tests/support/photo.jpg');
|
|
|
|
note2 = await Note.load(note2.id);
|
|
|
|
items = await Note.linkedItems(note2.body);
|
|
|
|
expect(items.length).toBe(2);
|
|
|
|
expect(items[0].type_).toBe(BaseModel.TYPE_NOTE);
|
|
|
|
expect(items[1].type_).toBe(BaseModel.TYPE_RESOURCE);
|
2018-09-30 20:24:02 +02:00
|
|
|
|
2018-11-21 01:18:56 +02:00
|
|
|
const resource2 = await shim.createResourceFromPath(__dirname + '/../tests/support/photo.jpg');
|
|
|
|
const resource3 = await shim.createResourceFromPath(__dirname + '/../tests/support/photo.jpg');
|
|
|
|
note2.body += '<img alt="bla" src=":/' + resource2.id + '"/>';
|
|
|
|
note2.body += '<img src=\':/' + resource3.id + '\' />';
|
2018-09-30 20:24:02 +02:00
|
|
|
items = await Note.linkedItems(note2.body);
|
|
|
|
expect(items.length).toBe(4);
|
2018-05-03 14:11:45 +02:00
|
|
|
}));
|
|
|
|
|
2018-11-21 01:18:56 +02:00
|
|
|
it('should find linked items', asyncTest(async () => {
|
|
|
|
const testCases = [
|
|
|
|
['[](:/06894e83b8f84d3d8cbe0f1587f9e226)', ['06894e83b8f84d3d8cbe0f1587f9e226']],
|
|
|
|
['[](:/06894e83b8f84d3d8cbe0f1587f9e226) [](:/06894e83b8f84d3d8cbe0f1587f9e226)', ['06894e83b8f84d3d8cbe0f1587f9e226']],
|
|
|
|
['[](:/06894e83b8f84d3d8cbe0f1587f9e226) [](:/06894e83b8f84d3d8cbe0f1587f9e227)', ['06894e83b8f84d3d8cbe0f1587f9e226', '06894e83b8f84d3d8cbe0f1587f9e227']],
|
|
|
|
['[](:/06894e83b8f84d3d8cbe0f1587f9e226 "some title")', ['06894e83b8f84d3d8cbe0f1587f9e226']],
|
|
|
|
];
|
|
|
|
|
|
|
|
for (let i = 0; i < testCases.length; i++) {
|
|
|
|
const t = testCases[i];
|
|
|
|
|
|
|
|
const input = t[0];
|
|
|
|
const expected = t[1];
|
|
|
|
const actual = Note.linkedItemIds(input);
|
|
|
|
const contentEquals = ArrayUtils.contentEquals(actual, expected);
|
|
|
|
|
|
|
|
// console.info(contentEquals, input, expected, actual);
|
|
|
|
|
|
|
|
expect(contentEquals).toBe(true);
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
2018-10-04 19:34:30 +02:00
|
|
|
it('should change the type of notes', asyncTest(async () => {
|
|
|
|
let folder1 = await Folder.save({ title: "folder1" });
|
|
|
|
let note1 = await Note.save({ title: 'ma note', parent_id: folder1.id });
|
|
|
|
note1 = await Note.load(note1.id);
|
|
|
|
|
|
|
|
let changedNote = Note.changeNoteType(note1, 'todo');
|
|
|
|
expect(changedNote === note1).toBe(false);
|
|
|
|
expect(!!changedNote.is_todo).toBe(true);
|
|
|
|
await Note.save(changedNote);
|
|
|
|
|
|
|
|
note1 = await Note.load(note1.id);
|
|
|
|
changedNote = Note.changeNoteType(note1, 'todo');
|
|
|
|
expect(changedNote === note1).toBe(true);
|
|
|
|
expect(!!changedNote.is_todo).toBe(true);
|
|
|
|
|
|
|
|
note1 = await Note.load(note1.id);
|
|
|
|
changedNote = Note.changeNoteType(note1, 'note');
|
|
|
|
expect(changedNote === note1).toBe(false);
|
|
|
|
expect(!!changedNote.is_todo).toBe(false);
|
|
|
|
}));
|
|
|
|
|
2018-05-03 14:11:45 +02:00
|
|
|
});
|