import { BaseRedisCache , RedisClient } from '../index' ; import { runKeyValueCacheTests } from 'apollo-server-caching' ; import FakeTimers from '@sinonjs/fake-timers' ; describe ( 'BaseRedisCache' , ( ) => { it ( 'run apollo-server-caching test suite' , async ( ) => { const store = { } ; const timeouts = [ ] ; const testRedisClient = { set : jest . fn ( ( key , value , option ? , ttl ? ) => { store [ key ] = value ; if ( option === 'EX' && ttl ) { timeouts . push ( setTimeout ( ( ) => delete store [ key ] , ttl * 1000 ) ) ; } return Promise . resolve ( ) ; } ) , mget : jest . fn ( ( ... keys ) => Promise . resolve ( keys . map ( ( key ) => store [ key ] ) ) ) , flushdb : jest . fn ( ( ) => Promise . resolve ( ) ) , del : jest . fn ( ( key ) => { const keysDeleted = store . hasOwnProperty ( key ) ? 1 : 0 ; delete store [ key ] ; return Promise . resolve ( keysDeleted ) ; } ) , quit : jest . fn ( ( ) => Promise . resolve ( ) ) , } ; const cache = new BaseRedisCache ( { client : testRedisClient } ) ; const clock = FakeTimers . install ( ) ; try { await runKeyValueCacheTests ( cache , ( ms ) => clock . tick ( ms ) ) ; } finally { timeouts . forEach ( ( t ) => clearTimeout ( t ) ) ; clock . uninstall ( ) ; await cache . close ( ) ; } } ) ; } ) ;