From 36a24a5ed81decc65277391916f433a6ecd57ba0 Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Fri, 5 Jan 2024 18:21:24 +0000 Subject: [PATCH] Chore: Add more tests for tags --- packages/lib/models/Tag.test.ts | 61 ++++++++++++++++++++++++++------- packages/lib/models/Tag.ts | 2 +- 2 files changed, 50 insertions(+), 13 deletions(-) diff --git a/packages/lib/models/Tag.test.ts b/packages/lib/models/Tag.test.ts index ecd0352cbb..19c9680c26 100644 --- a/packages/lib/models/Tag.test.ts +++ b/packages/lib/models/Tag.test.ts @@ -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); - })); + }); }); diff --git a/packages/lib/models/Tag.ts b/packages/lib/models/Tag.ts index 2b1d794e4d..d3f0ac77d3 100644 --- a/packages/lib/models/Tag.ts +++ b/packages/lib/models/Tag.ts @@ -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 { return this.loadByField('title', title, { caseInsensitive: true }); }