import { TestBed } from '@angular/core/testing' ; import Dexie from 'dexie' ; import { createDummies , fastTestSetup , sample } from '../../../test/helpers' ; import { VcsAccountDummy } from '../../core/dummies' ; import { VcsAccount } from '../../core/vcs' ; import { VCS_ACCOUNT_DATABASE , VcsAccountDatabase , VcsAccountDatabaseProvider } from './vcs-account-database' ; describe ( 'browser.vcs.VcsAccountDatabase' , ( ) => { let accountDB ; const vcsAccountDummy = new VcsAccountDummy ( ) ; fastTestSetup ( ) ; beforeAll ( async ( ) => { await Dexie . delete ( 'VcsAccount' ) ; TestBed . configureTestingModule ( { providers : [ VcsAccountDatabaseProvider ] , } ) ; } ) ; beforeEach ( ( ) => { accountDB = TestBed . get ( VCS_ACCOUNT_DATABASE ) ; } ) ; afterEach ( async ( ) => { await accountDB . accounts . clear ( ) ; await accountDB . metadata . clear ( ) ; } ) ; describe ( '#getAllAccounts' , ( ) => { it ( 'should return all accounts.' , async ( ) => { const accounts = createDummies ( vcsAccountDummy , 5 ) ; await accountDB . accounts . bulkAdd ( accounts ) ; const result = await accountDB . getAllAccounts ( ) ; expect ( result ) . toEqual ( accounts ) ; } ) ; } ) ; describe ( '#deleteAccountByEmail' , ( ) => { it ( 'should delete account which matches with given email.' , async ( ) => { const accounts = createDummies ( vcsAccountDummy , 5 ) ; await accountDB . accounts . bulkAdd ( accounts ) ; const target = sample ( accounts ) ; const deleteCount = await accountDB . deleteAccountByEmail ( target . email ) ; expect ( deleteCount ) . toBe ( 1 ) ; const allAccounts = await accountDB . getAllAccounts ( ) ; expect ( allAccounts . includes ( target ) ) . toBe ( false ) ; } ) ; } ) ; describe ( '#isAccountExists' , ( ) => { it ( 'should return true as promise if account exists with given email.' , async ( ) => { const account = vcsAccountDummy . create ( ) ; await accountDB . accounts . add ( account ) ; const result = await accountDB . isAccountExists ( account . email ) ; expect ( result ) . toBe ( true ) ; } ) ; it ( 'should return false as promise if account not exists with given email.' , async ( ) => { const result = await accountDB . isAccountExists ( 'some_email' ) ; expect ( result ) . toBe ( false ) ; } ) ; } ) ; describe ( 'getRepositoryFetchAccount' , ( ) => { it ( 'should return vcs account if repository fetch account exists in metadata table.' , async ( ) => { const account = vcsAccountDummy . create ( ) ; await accountDB . accounts . put ( account ) ; await accountDB . metadata . put ( { accountEmail : account . email , isRepositoryFetchAccount : 1 , } ) ; const result = await accountDB . getRepositoryFetchAccount ( ) ; expect ( result ) . toEqual ( account ) ; } ) ; it ( 'should return undefined if repository fetch account does not exist in metadata table.' , async ( ) => { const account = vcsAccountDummy . create ( ) ; await accountDB . accounts . put ( account ) ; await accountDB . metadata . put ( { accountEmail : account . email , isRepositoryFetchAccount : 0 , } ) ; const result = await accountDB . getRepositoryFetchAccount ( ) ; expect ( result ) . toEqual ( undefined ) ; } ) ; } ) ; describe ( 'setRepositoryFetchAccountAs' , ( ) => { it ( 'should remove previous data.' , async ( ) => { const prevAccount = vcsAccountDummy . create ( ) ; await accountDB . metadata . put ( { accountEmail : prevAccount . email , isRepositoryFetchAccount : 1 , } ) ; const nextAccount = vcsAccountDummy . create ( ) ; await accountDB . setRepositoryFetchAccountAs ( nextAccount ) ; expect ( await accountDB . metadata . get ( { isRepositoryFetchAccount : 1 , } ) ) . toEqual ( { accountEmail : nextAccount . email , isRepositoryFetchAccount : 1 , } ) ; } ) ; } ) ; } ) ;