import { Test , TestingModule } from '@nestjs/testing' ; import { Account , AccountNotFoundException , AccountService } from '@fc/account' ; import { CryptographyFcpService , IPivotIdentity } from '@fc/cryptography-fcp' ; import { LoggerService } from '@fc/logger' ; import { CsrmTracksNoTracksException } from '../exceptions' ; import { CsmrTracksService } from './csmr-tracks.service' ; import { CsmrTracksElasticsearchService } from './csmr-tracks-elasticsearch.service' ; const identityMock = { given_name : 'Jean Paul Henri' , family_name : 'Dupont' , gender : 'male' , birthdate : '1970-01-01' , birthplace : '95277' , birthcountry : '99100' , } as IPivotIdentity ; const identityHashMock = 'identityHashMockValue' ; const accountMock = { id : '42' , } as Account ; const tracksMock = 'tracksMockValue' ; const loggerServiceMock = { setContext : jest . fn ( ) , debug : jest . fn ( ) , trace : jest . fn ( ) , } as unknown as LoggerService ; const accountServiceMock = { storeInteraction : jest . fn ( ) , isBlocked : jest . fn ( ) , getAccountByIdentityHash : jest . fn ( ) , } ; const cryptographyFcpServiceMock = { computeSubV1 : jest . fn ( ) , computeIdentityHash : jest . fn ( ) , } ; const csmrTracksElasticsearchServiceMock = { getTracksByAccountId : jest . fn ( ) , } ; describe ( 'CsmrTracksService' , ( ) => { let service ; beforeEach ( async ( ) => { const app = await Test . createTestingModule ( { controllers : [ CsmrTracksService ] , providers : [ LoggerService , AccountService , CryptographyFcpService , CsmrTracksElasticsearchService , ] , } ) . overrideProvider ( LoggerService ) . useValue ( loggerServiceMock ) . overrideProvider ( AccountService ) . useValue ( accountServiceMock ) . overrideProvider ( CryptographyFcpService ) . useValue ( cryptographyFcpServiceMock ) . overrideProvider ( CsmrTracksElasticsearchService ) . useValue ( csmrTracksElasticsearchServiceMock ) . compile ( ) ; service = app . get < CsmrTracksService > ( CsmrTracksService ) ; } ) ; describe ( 'getList()' , ( ) => { beforeEach ( ( ) => { jest . resetAllMocks ( ) ; jest . restoreAllMocks ( ) ; cryptographyFcpServiceMock . computeIdentityHash . mockReturnValueOnce ( identityHashMock ) ; accountServiceMock . getAccountByIdentityHash . mockResolvedValueOnce ( accountMock ) ; csmrTracksElasticsearchServiceMock . getTracksByAccountId . mockResolvedValueOnce ( tracksMock ) ; } ) ; it ( 'Should compute the identityHash from the identity.' , async ( ) => { await service . getList ( identityMock ) ; expect ( cryptographyFcpServiceMock . computeIdentityHash ) . toHaveBeenCalledWith ( identityMock ) ; } ) ; it ( 'Should get an Account object from an identityHash' , async ( ) => { await service . getList ( identityMock ) ; expect ( accountServiceMock . getAccountByIdentityHash ) . toHaveBeenCalledWith ( identityHashMock ) ; } ) ; it ( 'Should throw an error `CsrmTracksNoTracksException` if no tracks have been found for this account' , async ( ) => { jest . resetAllMocks ( ) ; jest . restoreAllMocks ( ) ; cryptographyFcpServiceMock . computeIdentityHash . mockReturnValueOnce ( identityHashMock ) ; accountServiceMock . getAccountByIdentityHash . mockResolvedValueOnce ( accountMock ) ; csmrTracksElasticsearchServiceMock . getTracksByAccountId . mockResolvedValueOnce ( [ ] ) ; const exceptionMock = new CsrmTracksNoTracksException ( ) ; await expect ( ( ) => service . getList ( identityMock ) ) . rejects . toThrow ( exceptionMock ) ; } ) ; it ( 'Should throw an error `AccountNotFoundException` if no account found for an `identityHash`' , async ( ) => { jest . resetAllMocks ( ) ; jest . restoreAllMocks ( ) ; const accountEmptyMock = { id : null } ; const noAccountMock = new AccountNotFoundException ( ) ; accountServiceMock . getAccountByIdentityHash . mockResolvedValueOnce ( accountEmptyMock ) ; await expect ( ( ) => service . getList ( identityMock ) ) . rejects . toThrow ( noAccountMock ) ; } ) ; it ( 'Should get the coresponding tracks from account.id.' , async ( ) => { await service . getList ( identityMock ) ; expect ( csmrTracksElasticsearchServiceMock . getTracksByAccountId ) . toHaveBeenCalledWith ( accountMock . id ) ; } ) ; it ( 'Should return JSON mock data' , async ( ) => { const result = await service . getList ( identityMock ) ; expect ( result ) . toEqual ( tracksMock ) ; } ) ; } ) ; } ) ;