Skip to content
Snippets Groups Projects
Commit f85aa89d authored by Winnie Hellmann's avatar Winnie Hellmann
Browse files

Extract Cache class from AjaxCache

parent 36b33a20
No related branches found
No related tags found
No related merge requests found
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));
Loading
Loading
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;
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);
});
});
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment