From 521ddc36c63c83e76a8426f56e4e395713756eb9 Mon Sep 17 00:00:00 2001 From: Philip Molares Date: Fri, 5 Mar 2021 12:01:04 +0100 Subject: [PATCH] HistoryService: Add unit test for getEntryByNoteIdOrAlias Also add extra test to deleteHistoryEntry Signed-off-by: Philip Molares --- src/history/history.service.spec.ts | 41 ++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/src/history/history.service.spec.ts b/src/history/history.service.spec.ts index 9956ecc74..97d129c02 100644 --- a/src/history/history.service.spec.ts +++ b/src/history/history.service.spec.ts @@ -121,6 +121,32 @@ describe('HistoryService', () => { }); }); + describe('getEntryByNoteIdOrAlias', () => { + const user = {} as User; + const alias = 'alias'; + describe('works', () => { + it('with history entry', async () => { + const note = Note.create(user, alias); + const historyEntry = HistoryEntry.create(user, note); + jest.spyOn(historyRepo, 'findOne').mockResolvedValueOnce(historyEntry); + jest.spyOn(noteRepo, 'findOne').mockResolvedValueOnce(note); + expect(await service.getEntryByNoteIdOrAlias(alias, user)).toEqual( + historyEntry, + ); + }); + }); + describe('fails', () => { + it('with an non-existing note', async () => { + jest.spyOn(noteRepo, 'findOne').mockResolvedValueOnce(undefined); + try { + await service.getEntryByNoteIdOrAlias(alias, {} as User); + } catch (e) { + expect(e).toBeInstanceOf(NotInDBError); + } + }); + }); + }); + describe('createOrUpdateHistoryEntry', () => { describe('works', () => { it('without an preexisting entry', async () => { @@ -231,10 +257,11 @@ describe('HistoryService', () => { ); await service.deleteHistoryEntry(alias, user); }); - + }); + describe('fails', () => { + const user = {} as User; + const alias = 'alias'; it('without an entry', async () => { - const user = {} as User; - const alias = 'alias'; const note = Note.create(user, alias); jest.spyOn(historyRepo, 'findOne').mockResolvedValueOnce(undefined); jest.spyOn(noteRepo, 'findOne').mockResolvedValueOnce(note); @@ -244,6 +271,14 @@ describe('HistoryService', () => { expect(e).toBeInstanceOf(NotInDBError); } }); + it('without a note', async () => { + jest.spyOn(noteRepo, 'findOne').mockResolvedValueOnce(undefined); + try { + await service.getEntryByNoteIdOrAlias(alias, {} as User); + } catch (e) { + expect(e).toBeInstanceOf(NotInDBError); + } + }); }); });