Extract Cache class from AjaxCache
This commit is contained in:
parent
e15aee2b51
commit
c102656736
|
@ -1,21 +1,11 @@
|
|||
class AjaxCache {
|
||||
import Cache from './cache';
|
||||
|
||||
class AjaxCache extends Cache {
|
||||
constructor() {
|
||||
this.internalStorage = { };
|
||||
super();
|
||||
this.pendingRequests = { };
|
||||
}
|
||||
|
||||
get(endpoint) {
|
||||
return this.internalStorage[endpoint];
|
||||
}
|
||||
|
||||
hasData(endpoint) {
|
||||
return Object.prototype.hasOwnProperty.call(this.internalStorage, endpoint);
|
||||
}
|
||||
|
||||
remove(endpoint) {
|
||||
delete this.internalStorage[endpoint];
|
||||
}
|
||||
|
||||
retrieve(endpoint) {
|
||||
if (this.hasData(endpoint)) {
|
||||
return Promise.resolve(this.get(endpoint));
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
class Cache {
|
||||
constructor() {
|
||||
this.internalStorage = { };
|
||||
}
|
||||
|
||||
get(key) {
|
||||
return this.internalStorage[key];
|
||||
}
|
||||
|
||||
hasData(key) {
|
||||
return Object.prototype.hasOwnProperty.call(this.internalStorage, key);
|
||||
}
|
||||
|
||||
remove(key) {
|
||||
delete this.internalStorage[key];
|
||||
}
|
||||
}
|
||||
|
||||
export default Cache;
|
|
@ -0,0 +1,65 @@
|
|||
import Cache from '~/lib/utils/cache';
|
||||
|
||||
describe('Cache', () => {
|
||||
const dummyKey = 'just some key';
|
||||
const dummyValue = 'more than a value';
|
||||
let cache;
|
||||
|
||||
beforeEach(() => {
|
||||
cache = new Cache();
|
||||
});
|
||||
|
||||
describe('get', () => {
|
||||
it('return cached data', () => {
|
||||
cache.internalStorage[dummyKey] = dummyValue;
|
||||
|
||||
expect(cache.get(dummyKey)).toBe(dummyValue);
|
||||
});
|
||||
|
||||
it('returns undefined for missing data', () => {
|
||||
expect(cache.internalStorage[dummyKey]).toBe(undefined);
|
||||
expect(cache.get(dummyKey)).toBe(undefined);
|
||||
});
|
||||
});
|
||||
|
||||
describe('hasData', () => {
|
||||
it('return true for cached data', () => {
|
||||
cache.internalStorage[dummyKey] = dummyValue;
|
||||
|
||||
expect(cache.hasData(dummyKey)).toBe(true);
|
||||
});
|
||||
|
||||
it('returns false for missing data', () => {
|
||||
expect(cache.internalStorage[dummyKey]).toBe(undefined);
|
||||
expect(cache.hasData(dummyKey)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('remove', () => {
|
||||
it('removes data from cache', () => {
|
||||
cache.internalStorage[dummyKey] = dummyValue;
|
||||
|
||||
cache.remove(dummyKey);
|
||||
|
||||
expect(cache.internalStorage[dummyKey]).toBe(undefined);
|
||||
});
|
||||
|
||||
it('does nothing for missing data', () => {
|
||||
expect(cache.internalStorage[dummyKey]).toBe(undefined);
|
||||
|
||||
cache.remove(dummyKey);
|
||||
|
||||
expect(cache.internalStorage[dummyKey]).toBe(undefined);
|
||||
});
|
||||
|
||||
it('does not remove wrong data', () => {
|
||||
cache.internalStorage[dummyKey] = dummyValue;
|
||||
cache.internalStorage[dummyKey + dummyKey] = dummyValue + dummyValue;
|
||||
|
||||
cache.remove(dummyKey);
|
||||
|
||||
expect(cache.internalStorage[dummyKey]).toBe(undefined);
|
||||
expect(cache.internalStorage[dummyKey + dummyKey]).toBe(dummyValue + dummyValue);
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue