2019-07-30 09:35:42 +02:00
|
|
|
/* eslint-disable no-unused-vars */
|
|
|
|
|
2018-05-09 10:53:47 +02:00
|
|
|
|
2020-11-07 17:59:37 +02:00
|
|
|
const time = require('@joplin/lib/time').default;
|
2020-11-05 18:58:23 +02:00
|
|
|
const { createNTestNotes, asyncTest, fileContentEqual, setupDatabase, setupDatabaseAndSynchronizer, db, synchronizer, fileApi, sleep, clearDatabase, switchClient, syncTargetId, objectsEqual, checkThrowAsync } = require('./test-utils.js');
|
2020-11-07 17:59:37 +02:00
|
|
|
const Folder = require('@joplin/lib/models/Folder.js');
|
|
|
|
const Note = require('@joplin/lib/models/Note.js');
|
|
|
|
const BaseModel = require('@joplin/lib/BaseModel').default;
|
|
|
|
const shim = require('@joplin/lib/shim').default;
|
2018-05-09 10:53:47 +02:00
|
|
|
|
|
|
|
process.on('unhandledRejection', (reason, p) => {
|
2020-11-10 17:59:30 +02:00
|
|
|
console.log('Unhandled Rejection at models_Folder: Promise', p, 'reason:', reason);
|
2018-05-09 10:53:47 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
async function allItems() {
|
2020-03-14 01:46:14 +02:00
|
|
|
const folders = await Folder.all();
|
|
|
|
const notes = await Note.all();
|
2018-05-09 10:53:47 +02:00
|
|
|
return folders.concat(notes);
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('models_Folder', function() {
|
|
|
|
|
|
|
|
beforeEach(async (done) => {
|
|
|
|
await setupDatabaseAndSynchronizer(1);
|
|
|
|
await switchClient(1);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should tell if a notebook can be nested under another one', asyncTest(async () => {
|
2020-03-14 01:46:14 +02:00
|
|
|
const f1 = await Folder.save({ title: 'folder1' });
|
|
|
|
const f2 = await Folder.save({ title: 'folder2', parent_id: f1.id });
|
|
|
|
const f3 = await Folder.save({ title: 'folder3', parent_id: f2.id });
|
|
|
|
const f4 = await Folder.save({ title: 'folder4' });
|
2018-05-09 10:53:47 +02:00
|
|
|
|
|
|
|
expect(await Folder.canNestUnder(f1.id, f2.id)).toBe(false);
|
|
|
|
expect(await Folder.canNestUnder(f2.id, f2.id)).toBe(false);
|
|
|
|
expect(await Folder.canNestUnder(f3.id, f1.id)).toBe(true);
|
|
|
|
expect(await Folder.canNestUnder(f4.id, f1.id)).toBe(true);
|
|
|
|
expect(await Folder.canNestUnder(f2.id, f3.id)).toBe(false);
|
|
|
|
expect(await Folder.canNestUnder(f3.id, f2.id)).toBe(true);
|
|
|
|
expect(await Folder.canNestUnder(f1.id, '')).toBe(true);
|
|
|
|
expect(await Folder.canNestUnder(f2.id, '')).toBe(true);
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should recursively delete notes and sub-notebooks', asyncTest(async () => {
|
2020-03-14 01:46:14 +02:00
|
|
|
const f1 = await Folder.save({ title: 'folder1' });
|
|
|
|
const f2 = await Folder.save({ title: 'folder2', parent_id: f1.id });
|
2020-03-14 12:01:45 +02:00
|
|
|
const f3 = await Folder.save({ title: 'folder3', parent_id: f2.id });
|
|
|
|
const f4 = await Folder.save({ title: 'folder4', parent_id: f1.id });
|
|
|
|
|
|
|
|
const noOfNotes = 20;
|
|
|
|
await createNTestNotes(noOfNotes, f1, null, 'note1');
|
|
|
|
await createNTestNotes(noOfNotes, f2, null, 'note2');
|
|
|
|
await createNTestNotes(noOfNotes, f3, null, 'note3');
|
|
|
|
await createNTestNotes(noOfNotes, f4, null, 'note4');
|
2018-05-09 10:53:47 +02:00
|
|
|
|
|
|
|
await Folder.delete(f1.id);
|
|
|
|
|
|
|
|
const all = await allItems();
|
|
|
|
expect(all.length).toBe(0);
|
|
|
|
}));
|
|
|
|
|
2019-03-02 19:35:57 +02:00
|
|
|
it('should sort by last modified, based on content', asyncTest(async () => {
|
|
|
|
let folders;
|
|
|
|
|
2020-03-14 01:46:14 +02:00
|
|
|
const f1 = await Folder.save({ title: 'folder1' }); await sleep(0.1);
|
|
|
|
const f2 = await Folder.save({ title: 'folder2' }); await sleep(0.1);
|
|
|
|
const f3 = await Folder.save({ title: 'folder3' }); await sleep(0.1);
|
|
|
|
const n1 = await Note.save({ title: 'note1', parent_id: f2.id });
|
2019-03-02 19:35:57 +02:00
|
|
|
|
|
|
|
folders = await Folder.orderByLastModified(await Folder.all(), 'desc');
|
|
|
|
expect(folders.length).toBe(3);
|
|
|
|
expect(folders[0].id).toBe(f2.id);
|
|
|
|
expect(folders[1].id).toBe(f3.id);
|
|
|
|
expect(folders[2].id).toBe(f1.id);
|
|
|
|
|
2020-03-14 01:46:14 +02:00
|
|
|
const n2 = await Note.save({ title: 'note1', parent_id: f1.id });
|
2019-03-02 19:35:57 +02:00
|
|
|
|
|
|
|
folders = await Folder.orderByLastModified(await Folder.all(), 'desc');
|
|
|
|
expect(folders[0].id).toBe(f1.id);
|
|
|
|
expect(folders[1].id).toBe(f2.id);
|
|
|
|
expect(folders[2].id).toBe(f3.id);
|
|
|
|
|
|
|
|
await Note.save({ id: n1.id, title: 'note1 mod' });
|
|
|
|
|
|
|
|
folders = await Folder.orderByLastModified(await Folder.all(), 'desc');
|
|
|
|
expect(folders[0].id).toBe(f2.id);
|
|
|
|
expect(folders[1].id).toBe(f1.id);
|
|
|
|
expect(folders[2].id).toBe(f3.id);
|
|
|
|
|
|
|
|
folders = await Folder.orderByLastModified(await Folder.all(), 'asc');
|
|
|
|
expect(folders[0].id).toBe(f3.id);
|
|
|
|
expect(folders[1].id).toBe(f1.id);
|
|
|
|
expect(folders[2].id).toBe(f2.id);
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should sort by last modified, based on content (sub-folders too)', asyncTest(async () => {
|
|
|
|
let folders;
|
|
|
|
|
2020-03-14 01:46:14 +02:00
|
|
|
const f1 = await Folder.save({ title: 'folder1' }); await sleep(0.1);
|
|
|
|
const f2 = await Folder.save({ title: 'folder2' }); await sleep(0.1);
|
|
|
|
const f3 = await Folder.save({ title: 'folder3', parent_id: f1.id }); await sleep(0.1);
|
|
|
|
const n1 = await Note.save({ title: 'note1', parent_id: f3.id });
|
2019-03-02 19:35:57 +02:00
|
|
|
|
|
|
|
folders = await Folder.orderByLastModified(await Folder.all(), 'desc');
|
|
|
|
expect(folders.length).toBe(3);
|
|
|
|
expect(folders[0].id).toBe(f1.id);
|
|
|
|
expect(folders[1].id).toBe(f3.id);
|
|
|
|
expect(folders[2].id).toBe(f2.id);
|
2019-03-10 23:16:05 +02:00
|
|
|
|
2020-03-14 01:46:14 +02:00
|
|
|
const n2 = await Note.save({ title: 'note2', parent_id: f2.id });
|
2019-03-10 23:16:05 +02:00
|
|
|
folders = await Folder.orderByLastModified(await Folder.all(), 'desc');
|
|
|
|
|
|
|
|
expect(folders[0].id).toBe(f2.id);
|
|
|
|
expect(folders[1].id).toBe(f1.id);
|
|
|
|
expect(folders[2].id).toBe(f3.id);
|
|
|
|
|
|
|
|
await Note.save({ id: n1.id, title: 'note1 MOD' });
|
2019-07-30 09:35:42 +02:00
|
|
|
|
2019-03-10 23:16:05 +02:00
|
|
|
folders = await Folder.orderByLastModified(await Folder.all(), 'desc');
|
|
|
|
expect(folders[0].id).toBe(f1.id);
|
|
|
|
expect(folders[1].id).toBe(f3.id);
|
|
|
|
expect(folders[2].id).toBe(f2.id);
|
|
|
|
|
2020-03-14 01:46:14 +02:00
|
|
|
const f4 = await Folder.save({ title: 'folder4', parent_id: f1.id }); await sleep(0.1);
|
|
|
|
const n3 = await Note.save({ title: 'note3', parent_id: f4.id });
|
2019-03-10 23:16:05 +02:00
|
|
|
|
|
|
|
folders = await Folder.orderByLastModified(await Folder.all(), 'desc');
|
|
|
|
expect(folders.length).toBe(4);
|
2019-04-02 01:22:04 +02:00
|
|
|
expect(folders[0].id).toBe(f1.id);
|
|
|
|
expect(folders[1].id).toBe(f4.id);
|
2019-03-10 23:16:05 +02:00
|
|
|
expect(folders[2].id).toBe(f3.id);
|
|
|
|
expect(folders[3].id).toBe(f2.id);
|
2019-03-02 19:35:57 +02:00
|
|
|
}));
|
|
|
|
|
2019-11-11 08:14:56 +02:00
|
|
|
it('should add node counts', asyncTest(async () => {
|
2020-03-14 01:46:14 +02:00
|
|
|
const f1 = await Folder.save({ title: 'folder1' });
|
|
|
|
const f2 = await Folder.save({ title: 'folder2', parent_id: f1.id });
|
|
|
|
const f3 = await Folder.save({ title: 'folder3', parent_id: f2.id });
|
|
|
|
const f4 = await Folder.save({ title: 'folder4' });
|
2019-11-11 08:14:56 +02:00
|
|
|
|
2020-03-14 01:46:14 +02:00
|
|
|
const n1 = await Note.save({ title: 'note1', parent_id: f3.id });
|
|
|
|
const n2 = await Note.save({ title: 'note1', parent_id: f3.id });
|
|
|
|
const n3 = await Note.save({ title: 'note1', parent_id: f1.id });
|
2019-11-11 08:14:56 +02:00
|
|
|
|
|
|
|
const folders = await Folder.all();
|
|
|
|
await Folder.addNoteCounts(folders);
|
|
|
|
|
|
|
|
const foldersById = {};
|
|
|
|
folders.forEach((f) => { foldersById[f.id] = f; });
|
|
|
|
|
|
|
|
expect(folders.length).toBe(4);
|
|
|
|
expect(foldersById[f1.id].note_count).toBe(3);
|
|
|
|
expect(foldersById[f2.id].note_count).toBe(2);
|
|
|
|
expect(foldersById[f3.id].note_count).toBe(2);
|
|
|
|
expect(foldersById[f4.id].note_count).toBe(0);
|
|
|
|
}));
|
|
|
|
|
2020-01-18 15:46:04 +02:00
|
|
|
it('should not count completed to-dos', asyncTest(async () => {
|
|
|
|
|
2020-03-14 01:46:14 +02:00
|
|
|
const f1 = await Folder.save({ title: 'folder1' });
|
|
|
|
const f2 = await Folder.save({ title: 'folder2', parent_id: f1.id });
|
|
|
|
const f3 = await Folder.save({ title: 'folder3', parent_id: f2.id });
|
|
|
|
const f4 = await Folder.save({ title: 'folder4' });
|
2020-01-18 15:46:04 +02:00
|
|
|
|
2020-03-14 01:46:14 +02:00
|
|
|
const n1 = await Note.save({ title: 'note1', parent_id: f3.id });
|
|
|
|
const n2 = await Note.save({ title: 'note2', parent_id: f3.id });
|
|
|
|
const n3 = await Note.save({ title: 'note3', parent_id: f1.id });
|
|
|
|
const n4 = await Note.save({ title: 'note4', parent_id: f3.id, is_todo: true, todo_completed: 0 });
|
|
|
|
const n5 = await Note.save({ title: 'note5', parent_id: f3.id, is_todo: true, todo_completed: 999 });
|
|
|
|
const n6 = await Note.save({ title: 'note6', parent_id: f3.id, is_todo: true, todo_completed: 999 });
|
2020-01-18 15:46:04 +02:00
|
|
|
|
|
|
|
const folders = await Folder.all();
|
|
|
|
await Folder.addNoteCounts(folders, false);
|
|
|
|
|
|
|
|
const foldersById = {};
|
|
|
|
folders.forEach((f) => { foldersById[f.id] = f; });
|
|
|
|
|
|
|
|
expect(folders.length).toBe(4);
|
|
|
|
expect(foldersById[f1.id].note_count).toBe(4);
|
|
|
|
expect(foldersById[f2.id].note_count).toBe(3);
|
|
|
|
expect(foldersById[f3.id].note_count).toBe(3);
|
|
|
|
expect(foldersById[f4.id].note_count).toBe(0);
|
|
|
|
}));
|
|
|
|
|
2020-03-11 16:20:25 +02:00
|
|
|
it('should recursively find folder path', asyncTest(async () => {
|
2020-03-14 01:46:14 +02:00
|
|
|
const f1 = await Folder.save({ title: 'folder1' });
|
|
|
|
const f2 = await Folder.save({ title: 'folder2', parent_id: f1.id });
|
|
|
|
const f3 = await Folder.save({ title: 'folder3', parent_id: f2.id });
|
2020-03-11 16:20:25 +02:00
|
|
|
|
|
|
|
const folders = await Folder.all();
|
|
|
|
const folderPath = await Folder.folderPath(folders, f3.id);
|
|
|
|
|
|
|
|
expect(folderPath.length).toBe(3);
|
|
|
|
expect(folderPath[0].id).toBe(f1.id);
|
|
|
|
expect(folderPath[1].id).toBe(f2.id);
|
|
|
|
expect(folderPath[2].id).toBe(f3.id);
|
|
|
|
}));
|
|
|
|
|
2020-05-09 17:19:30 +02:00
|
|
|
it('should sort folders alphabetically', asyncTest(async () => {
|
|
|
|
const f1 = await Folder.save({ title: 'folder1' });
|
|
|
|
const f2 = await Folder.save({ title: 'folder2', parent_id: f1.id });
|
|
|
|
const f3 = await Folder.save({ title: 'folder3', parent_id: f1.id });
|
|
|
|
const f4 = await Folder.save({ title: 'folder4' });
|
|
|
|
const f5 = await Folder.save({ title: 'folder5', parent_id: f4.id });
|
|
|
|
const f6 = await Folder.save({ title: 'folder6' });
|
|
|
|
|
|
|
|
const folders = await Folder.allAsTree();
|
|
|
|
const sortedFolderTree = await Folder.sortFolderTree(folders);
|
|
|
|
|
|
|
|
expect(sortedFolderTree.length).toBe(3);
|
|
|
|
expect(sortedFolderTree[0].id).toBe(f1.id);
|
|
|
|
expect(sortedFolderTree[0].children[0].id).toBe(f2.id);
|
|
|
|
expect(sortedFolderTree[0].children[1].id).toBe(f3.id);
|
|
|
|
expect(sortedFolderTree[1].id).toBe(f4.id);
|
|
|
|
expect(sortedFolderTree[1].children[0].id).toBe(f5.id);
|
|
|
|
expect(sortedFolderTree[2].id).toBe(f6.id);
|
|
|
|
}));
|
2020-06-07 13:47:43 +02:00
|
|
|
|
|
|
|
it('should not allow setting a notebook parent as itself', asyncTest(async () => {
|
|
|
|
const f1 = await Folder.save({ title: 'folder1' });
|
|
|
|
const hasThrown = await checkThrowAsync(() => Folder.save({ id: f1.id, parent_id: f1.id }, { userSideValidation: true }));
|
|
|
|
expect(hasThrown).toBe(true);
|
|
|
|
}));
|
2019-07-30 09:35:42 +02:00
|
|
|
});
|