import { PgUserAccountRepository } 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 ( 'PgUserAccountRepository' , ( ) => { 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 PgUserAccountRepository ( ) ; } ) ; it ( 'should extend PgRepository' , async ( ) => { expect ( sut ) . toBeInstanceOf ( PgRepository ) ; } ) ; describe ( 'load' , ( ) => { it ( 'should return an account if email exists' , async ( ) => { await pgUserRepo . save ( { email : 'any_email' } ) ; const account = await sut . load ( { email : 'any_email' } ) ; expect ( account ) . toEqual ( { id : '1' } ) ; } ) ; it ( 'should return undefined if email does not exists' , async ( ) => { const account = await sut . load ( { email : 'any_email' } ) ; expect ( account ) . toBeUndefined ( ) ; } ) ; } ) ; describe ( 'saveWithFacebook' , ( ) => { it ( 'should create an account if id is undefined' , async ( ) => { const { id } = await sut . saveWithFacebook ( { email : 'any_email' , name : 'any_name' , facebookId : 'any_fb_id' } ) ; const pgUser = await pgUserRepo . findOne ( { email : 'any_email' } ) ; expect ( pgUser ?. id ) . toBe ( 1 ) ; expect ( id ) . toBe ( '1' ) ; } ) ; it ( 'should update account if id is defined' , async ( ) => { await pgUserRepo . save ( { email : 'any_email' , name : 'any_name' , facebookId : 'any_fb_id' } ) ; const { id } = await sut . saveWithFacebook ( { id : '1' , email : 'new_email' , name : 'new_name' , facebookId : 'new_fb_id' } ) ; const pgUser = await pgUserRepo . findOne ( { id : 1 } ) ; expect ( pgUser ) . toMatchObject ( { id : 1 , email : 'any_email' , name : 'new_name' , facebookId : 'new_fb_id' } ) ; expect ( id ) . toBe ( '1' ) ; } ) ; } ) ; } ) ;