import { PgUserProfileRepository } from '@/infra/repos/postgres' ; import { PgUser } from '@/infra/repos/postgres/entities' ; import { PgConnection } from '@/infra/repos/postgres/helpers' ; import { PgRepository } from '@/infra/repos/postgres/repository' ; import { makeFakeDb } from '@/tests/infra/repos/postgres/mocks' ; import { IBackup } from 'pg-mem' ; import { Repository } from 'typeorm' ; describe ( 'PgUserProfileRepository' , ( ) => { let sut ; let connection ; let pgUserRepo ; let backup ; beforeAll ( async ( ) => { connection = PgConnection . getInstance ( ) ; const db = await makeFakeDb ( [ PgUser ] ) ; backup = db . backup ( ) ; pgUserRepo = connection . getRepository ( PgUser ) ; } ) ; afterAll ( async ( ) => { await connection . disconnect ( ) ; } ) ; beforeEach ( ( ) => { backup . restore ( ) ; sut = new PgUserProfileRepository ( ) ; } ) ; it ( 'should extend PgRepository' , async ( ) => { expect ( sut ) . toBeInstanceOf ( PgRepository ) ; } ) ; describe ( 'savePicture' , ( ) => { it ( 'should update user profile' , async ( ) => { const { id } = await pgUserRepo . save ( { email : 'any_email' , initials : 'any_initials' } ) ; await sut . savePicture ( { id : id . toString ( ) , pictureUrl : 'any_url' } ) ; const pgUser = await pgUserRepo . findOne ( { id } ) ; expect ( pgUser ) . toMatchObject ( { id , pictureUrl : 'any_url' , initials : null } ) ; } ) ; } ) ; describe ( 'load' , ( ) => { it ( 'should load user profile' , async ( ) => { const { id } = await pgUserRepo . save ( { email : 'any_email' , name : 'any_name' } ) ; const userProfile = await sut . load ( { id : id . toString ( ) } ) ; expect ( userProfile ?. name ) . toBe ( 'any_name' ) ; } ) ; it ( 'should load user profile' , async ( ) => { const { id } = await pgUserRepo . save ( { email : 'any_email' } ) ; const userProfile = await sut . load ( { id : id . toString ( ) } ) ; expect ( userProfile ?. name ) . toBeUndefined ( ) ; } ) ; it ( 'should return undefined' , async ( ) => { const userProfile = await sut . load ( { id : '1' } ) ; expect ( userProfile ) . toBeUndefined ( ) ; } ) ; } ) ; } ) ;