1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-11-24 08:12:24 +02:00

Chore: Add more tests for tags

This commit is contained in:
Laurent Cozic 2024-01-05 18:21:24 +00:00
parent 091bf4a3d7
commit 36a24a5ed8
2 changed files with 50 additions and 13 deletions

View File

@ -10,7 +10,7 @@ describe('models/Tag', () => {
await switchClient(1);
});
it('should add tags by title', (async () => {
it('should add tags by title', async () => {
const folder1 = await Folder.save({ title: 'folder1' });
const note1 = await Note.save({ title: 'ma note', parent_id: folder1.id });
@ -18,9 +18,46 @@ describe('models/Tag', () => {
const noteTags = await Tag.tagsByNoteId(note1.id);
expect(noteTags.length).toBe(2);
}));
});
it('should not allow renaming tag to existing tag names', (async () => {
it('should get the notes associated with a tag', async () => {
const note1 = await Note.save({});
const note2 = await Note.save({});
const note3 = await Note.save({});
await Tag.setNoteTagsByTitles(note1.id, ['un']);
await Tag.setNoteTagsByTitles(note2.id, ['un']);
await Tag.setNoteTagsByTitles(note3.id, ['deux']);
await Tag.save({ title: 'trois' });
const tag1 = await Tag.loadByTitle('un');
const tag2 = await Tag.loadByTitle('deux');
const tag3 = await Tag.loadByTitle('trois');
expect((await Tag.noteIds(tag1.id)).sort()).toEqual([note1.id, note2.id].sort());
expect((await Tag.noteIds(tag2.id)).sort()).toEqual([note3.id].sort());
expect((await Tag.noteIds(tag3.id)).sort()).toEqual([].sort());
expect(await Tag.hasNote(tag1.id, note1.id)).toBe(true);
expect(await Tag.hasNote(tag1.id, note2.id)).toBe(true);
expect(await Tag.hasNote(tag1.id, note3.id)).toBe(false);
expect(await Tag.hasNote(tag2.id, note1.id)).toBe(false);
expect(await Tag.hasNote(tag2.id, note2.id)).toBe(false);
expect(await Tag.hasNote(tag2.id, note3.id)).toBe(true);
expect(await Tag.hasNote(tag3.id, note1.id)).toBe(false);
expect(await Tag.hasNote(tag3.id, note2.id)).toBe(false);
expect(await Tag.hasNote(tag3.id, note3.id)).toBe(false);
const notesTag1 = await Tag.notes(tag1.id);
const notesTag2 = await Tag.notes(tag2.id);
const notesTag3 = await Tag.notes(tag3.id);
expect(notesTag1.map(n => n.id).sort()).toEqual([note1.id, note2.id].sort());
expect(notesTag2.map(n => n.id).sort()).toEqual([note3.id].sort());
expect(notesTag3.map(n => n.id).sort()).toEqual([].sort());
});
it('should not allow renaming tag to existing tag names', async () => {
const folder1 = await Folder.save({ title: 'folder1' });
const note1 = await Note.save({ title: 'ma note', parent_id: folder1.id });
@ -30,9 +67,9 @@ describe('models/Tag', () => {
const hasThrown = await checkThrowAsync(async () => await Tag.save({ id: tagUn.id, title: 'deux' }, { userSideValidation: true }));
expect(hasThrown).toBe(true);
}));
});
it('should not return tags without notes', (async () => {
it('should not return tags without notes', async () => {
const folder1 = await Folder.save({ title: 'folder1' });
const note1 = await Note.save({ title: 'ma note', parent_id: folder1.id });
await Tag.setNoteTagsByTitles(note1.id, ['un']);
@ -44,9 +81,9 @@ describe('models/Tag', () => {
tags = await Tag.allWithNotes();
expect(tags.length).toBe(0);
}));
});
it('should return tags with note counts', (async () => {
it('should return tags with note counts', async () => {
const folder1 = await Folder.save({ title: 'folder1' });
const note1 = await Note.save({ title: 'ma note', parent_id: folder1.id });
const note2 = await Note.save({ title: 'ma 2nd note', parent_id: folder1.id });
@ -76,9 +113,9 @@ describe('models/Tag', () => {
tags = await Tag.allWithNotes();
expect(tags.length).toBe(0);
}));
});
it('should load individual tags with note count', (async () => {
it('should load individual tags with note count', async () => {
const folder1 = await Folder.save({ title: 'folder1' });
const note1 = await Note.save({ title: 'ma note', parent_id: folder1.id });
const note2 = await Note.save({ title: 'ma 2nd note', parent_id: folder1.id });
@ -99,9 +136,9 @@ describe('models/Tag', () => {
tagWithCount = await Tag.loadWithCount(tag.id);
expect(tagWithCount.note_count).toBe(4);
expect(tagWithCount.todo_completed_count).toBe(1);
}));
});
it('should get common tags for set of notes', (async () => {
it('should get common tags for set of notes', async () => {
const folder1 = await Folder.save({ title: 'folder1' });
const taga = await Tag.save({ title: 'mytaga' });
const tagb = await Tag.save({ title: 'mytagb' });
@ -153,6 +190,6 @@ describe('models/Tag', () => {
expect(commonTagIds.includes(taga.id)).toBe(true);
expect(commonTagIds.includes(tagb.id)).toBe(true);
expect(commonTagIds.includes(tagc.id)).toBe(true);
}));
});
});

View File

@ -145,7 +145,7 @@ export default class Tag extends BaseItem {
return this.modelSelectAll(`SELECT * FROM tags WHERE id IN ("${commonTagIds.join('","')}")`);
}
public static async loadByTitle(title: string) {
public static async loadByTitle(title: string): Promise<TagEntity> {
return this.loadByField('title', title, { caseInsensitive: true });
}