import { TestBed } from "@angular/core/testing" ; import { DataStore } from "../data-store/data-store" ; import { StorageLocationModel } from "../data-store/storage-location.model" ; import { Injectable } from "@angular/core" ; import { MockedDataStore } from "../data-store/impl/spec/mocked-data-store.service" ; import { BaseDao } from "./base.dao" ; describe ( "BaseDao" , ( ) => { class Foo extends Object { bar : string ; } @ Injectable ( ) class TestBaseDao extends BaseDao < Foo > { public static readonly STORAGE_LOCATION : StorageLocationModel = new StorageLocationModel ( "foo" ) ; public static readonly DEFAULT_STORAGE_VALUE : Foo [ ] = [ ] ; public getStorageLocation ( ) { return TestBaseDao . STORAGE_LOCATION ; } public getDefaultStorageValue ( ) { return TestBaseDao . DEFAULT_STORAGE_VALUE ; } } let baseDao ; let dataStore ; let checkStorageLocationSpy ; let dataStoreFetchSpy ; let dataStoreSaveSpy ; let dataStoreUpsertPropertySpy ; let dataStoreClearSpy ; let mockedDataStore ; beforeEach ( ( done ) => { mockedDataStore = new MockedDataStore ( ) ; TestBed . configureTestingModule ( { providers : [ { provide : BaseDao , useClass : TestBaseDao } , { provide : DataStore , useValue : mockedDataStore } ] } ) ; baseDao = TestBed . get ( BaseDao ) ; dataStore = TestBed . get ( DataStore ) ; checkStorageLocationSpy = spyOn ( baseDao , "checkCompliantDao" ) . and . callThrough ( ) ; dataStoreFetchSpy = spyOn ( dataStore , "fetch" ) . and . callThrough ( ) ; dataStoreSaveSpy = spyOn ( dataStore , "save" ) . and . callThrough ( ) ; dataStoreUpsertPropertySpy = spyOn ( dataStore , "upsertProperty" ) . and . callThrough ( ) ; dataStoreClearSpy = spyOn ( dataStore , "clear" ) . and . callThrough ( ) ; done ( ) ; } ) ; it ( "should be initialized" , ( done ) => { expect ( baseDao . storageLocation ) . toEqual ( TestBaseDao . STORAGE_LOCATION ) ; expect ( baseDao . defaultStorage ) . toEqual ( TestBaseDao . DEFAULT_STORAGE_VALUE ) ; done ( ) ; } ) ; it ( "should resolve StorageLocationModel provided" , ( done ) => { const promise = baseDao . checkCompliantDao ( ) ; promise . then ( ( ) => { done ( ) ; } , error => { expect ( error ) . toBeNull ( ) ; done ( ) ; } ) ; } ) ; it ( "should reject StorageLocationModel not provided" , ( done ) => { baseDao . storageLocation = null ; const promise = baseDao . checkCompliantDao ( ) ; promise . then ( ( ) => { expect ( false ) . toBeTruthy ( "Whoops! I should not be here!" ) ; done ( ) ; } , error => { expect ( error ) . toEqual ( "StorageLocationModel not set in 'TestBaseDao'. Please override init method to assign a StorageLocationModel." ) ; expect ( dataStoreFetchSpy ) . not . toHaveBeenCalled ( ) ; done ( ) ; } ) ; } ) ; it ( "should fetch data as vector" , ( done ) => { const promise = < Promise < Foo [ ] > > baseDao . fetch ( ) ; promise . then ( ( ) => { expect ( checkStorageLocationSpy ) . toHaveBeenCalledTimes ( 1 ) ; expect ( dataStoreFetchSpy ) . toHaveBeenCalledTimes ( 1 ) ; done ( ) ; } , error => { expect ( error ) . toBeNull ( ) ; done ( ) ; } ) ; } ) ; it ( "should fetch data as object" , ( done ) => { mockedDataStore . initWithObject ( { bar : "john doe" } ) ; const promise = < Promise < Foo > > baseDao . fetch ( ) ; promise . then ( ( ) => { expect ( checkStorageLocationSpy ) . toHaveBeenCalledTimes ( 1 ) ; expect ( dataStoreFetchSpy ) . toHaveBeenCalledTimes ( 1 ) ; done ( ) ; } , error => { expect ( error ) . toBeNull ( ) ; done ( ) ; } ) ; } ) ; it ( "should save data" , ( done ) => { const foo = [ { bar : "john doe" } ] ; const promise = < Promise < Foo [ ] > > baseDao . save ( foo ) ; promise . then ( ( ) => { expect ( checkStorageLocationSpy ) . toHaveBeenCalledTimes ( 1 ) ; expect ( dataStoreSaveSpy ) . toHaveBeenCalledTimes ( 1 ) ; done ( ) ; } , error => { expect ( error ) . toBeNull ( ) ; done ( ) ; } ) ; } ) ; it ( "should save property data" , ( done ) => { mockedDataStore . initWithObject ( { bar : "john doe" } ) ; const path = "bar" ; const newValue = "jack" ; const promise = baseDao . upsertProperty < string > ( path , newValue ) ; promise . then ( ( ) => { expect ( checkStorageLocationSpy ) . toHaveBeenCalledTimes ( 1 ) ; expect ( dataStoreUpsertPropertySpy ) . toHaveBeenCalledTimes ( 1 ) ; done ( ) ; } , error => { expect ( error ) . toBeNull ( ) ; done ( ) ; } ) ; } ) ; it ( "should clear data" , ( done ) => { const promise = baseDao . clear ( ) ; promise . then ( ( ) => { expect ( checkStorageLocationSpy ) . toHaveBeenCalledTimes ( 1 ) ; expect ( dataStoreClearSpy ) . toHaveBeenCalledTimes ( 1 ) ; done ( ) ; } , error => { expect ( error ) . toBeNull ( ) ; done ( ) ; } ) ; } ) ; } ) ;