From f731d2d455ed32255137909cfd18e19c49c357a9 Mon Sep 17 00:00:00 2001 From: Philip Molares Date: Sun, 11 Apr 2021 22:06:36 +0200 Subject: [PATCH] HistoryService: Add test for setHistory Signed-off-by: Philip Molares --- src/history/history.service.spec.ts | 59 ++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 2 deletions(-) diff --git a/src/history/history.service.spec.ts b/src/history/history.service.spec.ts index 14ca1d82e..e7fc8a48b 100644 --- a/src/history/history.service.spec.ts +++ b/src/history/history.service.spec.ts @@ -9,7 +9,7 @@ import { LoggerModule } from '../logger/logger.module'; import { HistoryService } from './history.service'; import { UsersModule } from '../users/users.module'; import { NotesModule } from '../notes/notes.module'; -import { getRepositoryToken } from '@nestjs/typeorm'; +import { getConnectionToken, getRepositoryToken } from '@nestjs/typeorm'; import { Identity } from '../users/identity.entity'; import { User } from '../users/user.entity'; import { AuthorColor } from '../notes/author-color.entity'; @@ -19,23 +19,39 @@ import { Note } from '../notes/note.entity'; import { Tag } from '../notes/tag.entity'; import { AuthToken } from '../auth/auth-token.entity'; import { Revision } from '../revisions/revision.entity'; -import { Repository } from 'typeorm'; +import { Connection, Repository } from 'typeorm'; import { NotInDBError } from '../errors/errors'; import { NoteGroupPermission } from '../permissions/note-group-permission.entity'; import { NoteUserPermission } from '../permissions/note-user-permission.entity'; import { Group } from '../groups/group.entity'; import { ConfigModule } from '@nestjs/config'; import appConfigMock from '../config/mock/app.config.mock'; +import { HistoryEntryImportDto } from './history-entry-import.dto'; describe('HistoryService', () => { let service: HistoryService; let historyRepo: Repository; + let connection; let noteRepo: Repository; + type MockConnection = { + transaction: () => void; + }; + + function mockConnection(): MockConnection { + return { + transaction: jest.fn(), + }; + } + beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ providers: [ HistoryService, + { + provide: getConnectionToken(), + useFactory: mockConnection, + }, { provide: getRepositoryToken(HistoryEntry), useClass: Repository, @@ -79,6 +95,7 @@ describe('HistoryService', () => { historyRepo = module.get>( getRepositoryToken(HistoryEntry), ); + connection = module.get(Connection); noteRepo = module.get>(getRepositoryToken(Note)); }); @@ -310,6 +327,44 @@ describe('HistoryService', () => { }); }); + describe('setHistory', () => { + it('works', async () => { + const user = {} as User; + const alias = 'alias'; + const note = Note.create(user, alias); + const historyEntry = HistoryEntry.create(user, note); + const historyEntryImport: HistoryEntryImportDto = { + lastVisited: new Date('2020-12-01 12:23:34'), + note: alias, + pinStatus: true, + }; + const newlyCreatedHistoryEntry: HistoryEntry = { + ...historyEntry, + pinStatus: historyEntryImport.pinStatus, + updatedAt: historyEntryImport.lastVisited, + }; + const mockedManager = { + find: jest.fn().mockResolvedValueOnce([historyEntry]), + findOne: jest.fn().mockResolvedValueOnce(note), + remove: jest.fn().mockImplementationOnce((entry: HistoryEntry) => { + expect(entry.note.alias).toEqual(alias); + expect(entry.pinStatus).toEqual(false); + }), + save: jest.fn().mockImplementationOnce((entry: HistoryEntry) => { + expect(entry.note.alias).toEqual(newlyCreatedHistoryEntry.note.alias); + expect(entry.pinStatus).toEqual(newlyCreatedHistoryEntry.pinStatus); + expect(entry.updatedAt).toEqual(newlyCreatedHistoryEntry.updatedAt); + }), + }; + // eslint-disable-next-line @typescript-eslint/no-unsafe-call + connection.transaction.mockImplementation((cb) => { + // eslint-disable-next-line @typescript-eslint/no-unsafe-call + cb(mockedManager); + }); + await service.setHistory(user, [historyEntryImport]); + }); + }); + describe('toHistoryEntryDto', () => { describe('works', () => { it('with aliased note', async () => {