Created
July 2, 2021 02:33
-
-
Save danielkellyio/94d659e6351e0e79dcfac396f84ee8fe to your computer and use it in GitHub Desktop.
Cache Class Helper Unit Test Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Cache from '~/helper/Cache' | |
describe('Cache class', ()=>{ | |
test('get method retrieves items from the cache', ()=>{ | |
const cache = new Cache() | |
cache.put('hello', 'world') | |
expect(cache.get('hello')).toBe('world') | |
}) | |
test('put method adds items to the cache', ()=>{ | |
(new Cache).put('hello', 'world') | |
expect((new Cache).get('hello')).toBe('world') | |
}) | |
test('forget method removes items from the cache', ()=>{ | |
const cache = new Cache | |
cache.put('hello', 'world') | |
cache.forget('hello') | |
expect(cache.get('hello')).toBeNull() | |
}) | |
test('pull method return and removes', ()=>{ | |
const cache = new Cache | |
cache.put('hello', 'world') | |
const fromCache = cache.pull('hello') | |
// retrieved first | |
expect(fromCache).toBe('world') | |
// then removed from cache | |
expect(cache.get('hello')).toBeNull() | |
}) | |
test('flush method completely eliminates all items from cache', ()=>{ | |
const cache = new Cache() | |
cache.put('hello', 'world') | |
cache.put('hola', 'spanish for world') | |
cache.put('something else', 'im in the cache') | |
cache.flush() | |
// expect the default group to be empty | |
expect( JSON.stringify(cache.all().default) ).toBe('{}') | |
// and not other group to exist | |
expect( Object.keys(cache.all()).length ).toBe(1) | |
}) | |
test('expired items do not return from cache', (done)=>{ | |
const cache = new Cache() | |
cache.expires(100).put('hello', 'world') | |
// can still get before expires | |
expect(cache.get('hello')).toBe('world') | |
setTimeout(()=>{ | |
//null after expires | |
expect(cache.get('hello')).toBeNull() | |
done() | |
}, 101) | |
}) | |
test('group method adds items to named group', ()=>{ | |
const cache = new Cache() | |
cache.group('cats') | |
.put('fluffy', 'calico') | |
.put('coco', 'siamese') | |
expect( JSON.stringify(cache.getGroup('default'))).toBe('{}') | |
expect( cache.group('cats').get('fluffy') ).toBe('calico') | |
}) | |
test('method flushGroup flushes one group without altering other groups', ()=>{ | |
const cache = new Cache() | |
cache.group('group 1').put('hello', 'world').put('another item', 'here i am ') | |
cache.group('group 2').put('group 2 hello', 'world group 2').put('group 2 another item', 'here i am group 2') | |
cache.flushGroup('group 2') | |
expect( JSON.stringify(cache.getGroup('group 1')) ).not.toBe('{}') | |
expect( JSON.stringify(cache.getGroup('group 2')) ).toBe('{}') | |
}) | |
test('default value used if item not found in cache', ()=>{ | |
const cache = new Cache() | |
const dude = cache.get('noexist', 'dude') | |
expect(dude).toBe('dude') | |
}) | |
test('default value can be callback functions', ()=>{ | |
const cache = new Cache() | |
const dude = cache.get('noexist', ()=>{ | |
return 'dude' | |
}) | |
expect(dude).toBe('dude') | |
}) | |
}) | |
afterEach(() => { | |
(new Cache).flush() | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment