Skip to content

Instantly share code, notes, and snippets.

@evxn
Last active February 26, 2018 06:37
Show Gist options
  • Save evxn/41e2fd351c4476b6dfa6 to your computer and use it in GitHub Desktop.
Save evxn/41e2fd351c4476b6dfa6 to your computer and use it in GitHub Desktop.
Simple storage (with test spec) for pure objects (including Function-objects) written in JavaScript
function Factory(){
var _collection = [];
var API = {};
API.contains = function (obj) {
var found = _collection.indexOf(obj) !== -1;
return found;
};
API.find = function(obj){
var index = _collection.indexOf(obj);
if (index === -1){
return null;
}
return _collection[index];
};
API.store = function(obj){
if (this.contains(obj)) {
return false;
}
var expectedLength = _collection.length+1;
return _collection.push(obj) === expectedLength;
};
API.remove = function(obj){
if (this.contains(obj)) {
return _collection.splice(_collection.indexOf(obj), 1).length === 1;
};
return false;
};
return API;
}
module.exports.factory = Factory;
Factory = require('./verySimpleStorage').factory;
describe('Very Simple Storage API', function(){
var storage, o1, o2;
beforeEach(function(){
storage = Factory();
o1 = {value: 1};
o2 = {value: 2};
});
describe('factory function', function(){
it('should instantiate storage object', function(){
expect(storage).toBeDefined();
});
});
describe('storage object', function(){
it('should expose contains, find, store and remove methods', function(){
expect(storage.contains).toBeDefined();
expect(storage.find).toBeDefined();
expect(storage.store).toBeDefined();
expect(storage.remove).toBeDefined();
});
describe('contains method', function(){
it('should return true when element is in the collection',function(){
storage.store(o1);
storage.store(o2);
expect(storage.contains(o1)).toEqual(true);
expect(storage.contains(o2)).toEqual(true);
});
it('should return false when element is not in the collection',function(){
storage.store(o1);
expect(storage.contains(o2)).toEqual(false)
});
it('should be falsy when collection is empty',function(){
expect(storage.contains(o1)).toEqual(false)
});
});
describe('find method', function(){
it('should return first equal element if it exists in the collection',function(){
storage.store(o1);
expect(storage.find(o1)).toEqual(o1);
});
it('should return null if nothing is found',function(){
storage.store(o2);
expect(storage.find(o1)).toBeNull()
});
it('should not find an element in the empty collection',function(){
expect(storage.find(o1)).toBeNull()
});
});
describe('store method', function(){
it('should store objects',function(){
storage.store(o1);
storage.store(o2);
expect(storage.contains(o1)).toBeTruthy();
expect(storage.contains(o2)).toBeTruthy();
});
it('should not store element if it already exists',function(){
storage.store(o1);
storage.store(o1);
storage.remove(o1);
expect(storage.contains(o1)).toBeFalsy();
});
it('should return true on successful insert',function(){
expect(storage.store(o1)).toEqual(true);
});
it('should return false on failed insert',function(){
storage.store(o1);
expect(storage.store(o1)).toEqual(false);
});
});
describe('remove method', function(){
it('should remove element from the collection',function(){
storage.store(o1);
storage.remove(o1);
expect(storage.contains(o1)).toBeFalsy();
});
it('should not remove other elements in collection',function(){
storage.store(o1);
storage.store(o2);
storage.remove(o1);
expect(storage.contains(o1)).toBeFalsy();
expect(storage.contains(o2)).toBeTruthy();
});
it('should return true on successful removal',function(){
storage.store(o1);
expect(storage.remove(o1)).toEqual(true);
});
it('should return false if nothing was removed',function(){
expect(storage.remove(o1)).toEqual(false);
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment