import { CacheStore } from '@/data/protocols/cache' ; import { SavePurchases } from '@/domain/usecases' ; export class CacheStoreSpy implements CacheStore { actions : Array < CacheStoreSpy . Action > = [ ] ; deleteKey : string ; insertKey : string ; fetchKey : string ; insertValues : Array < SavePurchases . Params > = [ ] ; fetchResult : any ; fetch ( key ) { this . actions . push ( CacheStoreSpy . Action . fetch ) ; this . fetchKey = key ; return this . fetchResult ; } delete ( key ) { this . actions . push ( CacheStoreSpy . Action . delete ) ; this . deleteKey = key ; } insert ( key , value ) { this . actions . push ( CacheStoreSpy . Action . insert ) ; this . insertKey = key ; this . insertValues = value ; } replace ( key , value ) { this . delete ( key ) ; this . insert ( key , value ) ; } simulateDeleteError ( ) { jest . spyOn ( CacheStoreSpy . prototype , 'delete' ) . mockImplementationOnce ( ( ) => { this . actions . push ( CacheStoreSpy . Action . delete ) ; throw new Error ( ) ; } ) ; } simulateInsertError ( ) { jest . spyOn ( CacheStoreSpy . prototype , 'insert' ) . mockImplementationOnce ( ( ) => { this . actions . push ( CacheStoreSpy . Action . insert ) ; throw new Error ( ) ; } ) ; } simulateFetchError ( ) { jest . spyOn ( CacheStoreSpy . prototype , 'fetch' ) . mockImplementationOnce ( ( ) => { this . actions . push ( CacheStoreSpy . Action . fetch ) ; throw new Error ( ) ; } ) ; } } export namespace CacheStoreSpy { export enum Action { delete , insert , fetch } }