spec.plugins.cache.js
This file
Path
spec/spec.plugins.cache.js
110 lines · 2.8 KiB · JavaScript
Last changed by
Memory cache now allows objects as valuesvisionmediabefore adoption8207e70
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
describe 'Express' before_each reset() use(require('express/plugins/cache').Cache) cache = require('express/plugins/cache') end describe 'Cache' describe 'Request' describe '#cache' it 'should use memory store by default' get('/item', function(){ return this.cache.toString() }) get('/item').body.should.eql '[Memory Store]' end end end end describe 'Store' describe 'Memory' before_each store = new cache.Store.Memory end describe '#toString()' it 'should return [Memory Store]' store.toString().should.eql '[Memory Store]' end end describe '#set()' describe 'given a key and value' it 'should set the cache data' store.set('foo', 'bar') store.get('foo').should.eql 'bar' end it 'should override existing data' store.set('foo', 'bar') store.set('foo', 'baz') store.get('foo').should.eql 'baz' end it 'should return data' store.set('foo', 'bar').should.eql 'bar' end end describe 'given an abitrary key' it 'should throw an error' -{ store.set({}, 'foo') }.should.throw_error end end describe 'given an abitrary value' it 'should serialize as JSON' store.set('user', { name: 'tj' }).should.eql { name: 'tj' } end end end describe '#get()' describe 'given a key' it 'should return cached value' store.set('foo', 'bar') store.get('foo').should.eql 'bar' end it 'should unserialize JSON data' store.set('user', { name: 'tj' }) store.get('user').should.eql { name: 'tj' } end end describe 'given wildcards' it 'should return a set of caches' store.set('user:1', 'a') store.set('user:2', 'b') store.set('foo', 'bar') store.get('user:*').should.eql { 'user:1': 'a', 'user:2': 'b' } end end end describe '#clear()' describe 'given a key' it 'should delete previous data' store.set('foo', 'bar') store.clear('foo') store.get('foo').should.be_null end end describe 'given wildcards' it 'should clear a set of caches' store.set('user:one', '1') store.set('user:two', '2') store.clear('user:*') store.get('user:one').should.be_null store.get('user:two').should.be_null end end end end endend