import { CacheService , ICacheManager } from './cache.service' ; describe ( 'CacheService' , ( ) => { let service ; let store = { } ; const Manager = jest . fn < ICacheManager , any [ ] > ( ( ) => { return { get : jest . fn ( ( key ) => store [ key ] ) , set : jest . fn ( ( key , value , options ? ) => { store [ key ] = value ; return value ; } ) , del : jest . fn ( async ( key ) => { delete store [ key ] ; } ) , } ; } ) ; const manager = new Manager ( ) ; beforeAll ( async ( ) => { service = new CacheService ( manager ) ; } ) ; beforeEach ( async ( ) => { store = { } ; } ) ; it ( 'should be defined' , ( ) => { expect ( service ) . toBeDefined ( ) ; } ) ; it ( 'should call set' , ( ) => { service . set ( 'test' , 'test' ) ; expect ( manager . set ) . toHaveBeenCalledTimes ( 1 ) ; } ) ; it ( 'should call get' , ( ) => { const res = service . get ( 'test' ) ; expect ( res ) . toBeUndefined ( ) ; expect ( manager . get ) . toHaveBeenCalledTimes ( 1 ) ; } ) ; it ( 'should get set value' , ( ) => { store . testKey = 'test value' ; expect ( service . get ( 'testKey' ) ) . toEqual ( 'test value' ) ; } ) ; it ( 'should set delete value' , ( ) => { store . testKey = 'test value' ; expect ( service . get ( 'testKey' ) ) . toEqual ( 'test value' ) ; service . del ( 'testKey' ) ; expect ( service . get ( 'testKey' ) ) . toBeUndefined ( ) ; } ) ; it ( 'set should overwrite existing value' , ( ) => { store . current = 0 ; expect ( store . current ) . toEqual ( 0 ) ; service . set ( 'current' , 1 ) ; expect ( store . current ) . toEqual ( 1 ) ; } ) ; } ) ;