|  | 
|  | 1 | +var CacheController = require('../src/Controllers/CacheController.js').default; | 
|  | 2 | + | 
|  | 3 | +describe('CacheController', function() { | 
|  | 4 | +  var FakeCacheAdapter; | 
|  | 5 | +  var FakeAppID = 'foo'; | 
|  | 6 | +  var KEY = 'hello'; | 
|  | 7 | + | 
|  | 8 | +  beforeEach(() => { | 
|  | 9 | +    FakeCacheAdapter = { | 
|  | 10 | +      get: () => Promise.resolve(null), | 
|  | 11 | +      put: jasmine.createSpy('put'), | 
|  | 12 | +      del: jasmine.createSpy('del'), | 
|  | 13 | +      clear: jasmine.createSpy('clear') | 
|  | 14 | +    } | 
|  | 15 | + | 
|  | 16 | +    spyOn(FakeCacheAdapter, 'get').and.callThrough(); | 
|  | 17 | +  }); | 
|  | 18 | + | 
|  | 19 | + | 
|  | 20 | +  it('should expose role and user caches', (done) => { | 
|  | 21 | +    var cache = new CacheController(FakeCacheAdapter, FakeAppID); | 
|  | 22 | + | 
|  | 23 | +    expect(cache.role).not.toEqual(null); | 
|  | 24 | +    expect(cache.role.get).not.toEqual(null); | 
|  | 25 | +    expect(cache.user).not.toEqual(null); | 
|  | 26 | +    expect(cache.user.get).not.toEqual(null); | 
|  | 27 | + | 
|  | 28 | +    done(); | 
|  | 29 | +  }); | 
|  | 30 | + | 
|  | 31 | + | 
|  | 32 | +  ['role', 'user'].forEach((cacheName) => { | 
|  | 33 | +    it('should prefix ' + cacheName + ' cache', () => { | 
|  | 34 | +      var cache = new CacheController(FakeCacheAdapter, FakeAppID)[cacheName]; | 
|  | 35 | + | 
|  | 36 | +      cache.put(KEY, 'world'); | 
|  | 37 | +      var firstPut = FakeCacheAdapter.put.calls.first(); | 
|  | 38 | +      expect(firstPut.args[0]).toEqual([FakeAppID, cacheName, KEY].join(':')); | 
|  | 39 | + | 
|  | 40 | +      cache.get(KEY); | 
|  | 41 | +      var firstGet = FakeCacheAdapter.get.calls.first(); | 
|  | 42 | +      expect(firstGet.args[0]).toEqual([FakeAppID, cacheName, KEY].join(':')); | 
|  | 43 | + | 
|  | 44 | +      cache.del(KEY); | 
|  | 45 | +      var firstDel = FakeCacheAdapter.del.calls.first(); | 
|  | 46 | +      expect(firstDel.args[0]).toEqual([FakeAppID, cacheName, KEY].join(':')); | 
|  | 47 | +    }); | 
|  | 48 | +  }); | 
|  | 49 | + | 
|  | 50 | +  it('should clear the entire cache', () => { | 
|  | 51 | +    var cache = new CacheController(FakeCacheAdapter, FakeAppID); | 
|  | 52 | + | 
|  | 53 | +    cache.clear(); | 
|  | 54 | +    expect(FakeCacheAdapter.clear.calls.count()).toEqual(1); | 
|  | 55 | + | 
|  | 56 | +    cache.user.clear(); | 
|  | 57 | +    expect(FakeCacheAdapter.clear.calls.count()).toEqual(2); | 
|  | 58 | + | 
|  | 59 | +    cache.role.clear(); | 
|  | 60 | +    expect(FakeCacheAdapter.clear.calls.count()).toEqual(3); | 
|  | 61 | +  }); | 
|  | 62 | + | 
|  | 63 | +  it('should handle cache rejections', (done) => { | 
|  | 64 | + | 
|  | 65 | +      FakeCacheAdapter.get = () => Promise.reject(); | 
|  | 66 | + | 
|  | 67 | +      var cache = new CacheController(FakeCacheAdapter, FakeAppID); | 
|  | 68 | + | 
|  | 69 | +      cache.get('foo').then(done, () => { | 
|  | 70 | +          fail('Promise should not be rejected.'); | 
|  | 71 | +      }); | 
|  | 72 | +  }); | 
|  | 73 | + | 
|  | 74 | +}); | 
0 commit comments