Skip to content

Commit

Permalink
feat(store): implement store.clear()
Browse files Browse the repository at this point in the history
This commit implements and tests the store.clear() method, useful for
removing all data from a store without destroying the reference to the
array.  Also contains some minor fixes for the unit tests of both the
PoolStore and the Store.

Finally, the services depending on store have been updated so as not to
break with the new store improvements.
  • Loading branch information
jniles committed Jun 3, 2016
1 parent 49f556b commit 6134793
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 52 deletions.
1 change: 1 addition & 0 deletions client/src/js/services/PoolStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ function PoolStoreService(Store) {
return item;
};


// return the unavailable item to the pool
PoolStore.prototype.free = function free(id) {
var item = this.unavailable.get(id);
Expand Down
2 changes: 1 addition & 1 deletion client/src/js/services/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ angular.module('bhima.services')
.factory('connect', ConnectFactory);

ConnectFactory.$inject = [
'$http', '$q', 'store',
'$http', '$q', 'Store',
];

function ConnectFactory($http, $q, Store) {
Expand Down
8 changes: 2 additions & 6 deletions client/src/js/services/currencyFormat.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ angular.module('bhima.services')
.factory('currencyFormat', currencyFormat);

currencyFormat.$inject = [
'CurrencyService', '$http', 'store'
'CurrencyService', '$http', 'Store'
];

function currencyFormat(Currencies, $http, Store) {
Expand Down Expand Up @@ -59,11 +59,7 @@ function currencyFormat(Currencies, $http, Store) {

function addFormat(formatObject) {
/** @FIXME Resolve issue with initial Store data to just allow post. */
if (angular.isUndefined(currentFormats.data.length)) {
currentFormats.setData([formatObject]);
} else {
currentFormats.post(formatObject);
}
currentFormats.post(formatObject);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion client/src/js/services/journal/TransactionService.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ angular.module('bhima.services')

// @todo uuid is currently only used for creating mock transactions - this should
// be removed as soon as this is no longer needed
TransactionService.$inject = ['$http', 'store', 'uuid'];
TransactionService.$inject = ['$http', 'Store', 'uuid'];

/**
* Transactions Service
Expand Down
23 changes: 15 additions & 8 deletions client/src/js/services/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,8 @@ function StoreService() {
* @param {Array} data - an array of objects that will be stored in the instance.
*/
Store.prototype.setData = function setData(data) {
var index = this.index = {};
this.data = angular.copy(data);
var identifier = this.identifier;
for (var i = 0, l = data.length; i < l; i += 1) {
index[data[i][identifier]] = i;
}
this.data = data;
this.recalculateIndex();
};

/**
Expand Down Expand Up @@ -78,7 +74,7 @@ function StoreService() {
var identifier = this.identifier;

// default to an empty array if data not provided
if (!this.data) { this.data = []; }
if (!data) { this.data = []; }

var id = object[identifier];

Expand Down Expand Up @@ -125,6 +121,17 @@ function StoreService() {
return !!this.get(id);
};

/**
* @method clear
*
* @description
* Clears all data from the store and recalculates the index.
*/
Store.prototype.clear = function clear() {
this.data.length = 0;
this.recalculateIndex();
};

/**
* @method recalculateIndex
*
Expand All @@ -133,7 +140,7 @@ function StoreService() {
*/
Store.prototype.recalculateIndex = function recalculateIndex() {
var data = this.data;
var index = this.index;
var index = this.index = {};
var identifier = this.identifier;

for (var i = 0, l = data.length; i < l; i += 1) {
Expand Down
14 changes: 6 additions & 8 deletions client/test/unit/services/PoolStore.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,20 @@
describe('PoolStore', function () {
'use strict';

const data = [{
id: 1,
name: 'Bob'
}, {
id: 2,
name: 'Sarah'
}];

let PoolStore;
let data;

beforeEach(() => {
module('bhima.services');
});

beforeEach(inject((_PoolStore_) => {
PoolStore = _PoolStore_;

data = [
{ id: 1, name: 'Bob' },
{ id: 2, name: 'Sarah' }
];
}));

it('#constructor() runs with sane defaults', () => {
Expand Down
149 changes: 121 additions & 28 deletions client/test/unit/services/Store.spec.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,146 @@
/* jshint expr: true */
/* global inject, expect */
describe.skip('PoolStore', function () {
describe('Store', function () {
'use strict';

let PS;
const data = [{
id: 1,
name: 'Bob'
}, {
id: 2,
name: 'Sarah'
}];
let Store;
let data;


beforeEach(() => {
module('bhima.services');
});

beforeEach(inject((_PoolStore_) => {
let PoolStore = _PoolStore_;
PS = new PoolStore('id', data);
beforeEach(inject((_Store_) => {
Store = _Store_;
data = [
{ id: 1, name: 'Bob' },
{ id: 2, name: 'Sarah' }
];
}));

it('retrieve items stored in it', () => {
it('constructs a store', () => {
let defaultStore = new Store();
let customStore = new Store({ identifier : 'x' });
let dataStore = new Store({ data : [{ id : 1 }] });

expect(defaultStore.identifier).to.equal('id');
expect(defaultStore.data).to.be.empty;

expect(customStore.identifier).to.equal('x');
expect(customStore.data).to.be.empty;

expect(dataStore.data).to.have.length(1);
});

it('#setData() sets data in a store', () => {
let emptyStore = new Store();

emptyStore.setData(data);
expect(emptyStore.data).to.have.length(2);

emptyStore.setData([]);
expect(emptyStore.data).to.have.length(0);
});


let bob = PS.use(1);
it('#get() retrieves items stored in it', () => {
let store = new Store({ data : data });

let bob = store.get(1);
expect(bob.id).to.equal(1);
expect(bob.name).to.equal('Bob');

let sarah = PS.use(2);
let sarah = store.get(2);
expect(sarah.id).to.equal(2);
expect(sarah.name).to.equal('Sarah');

// cannot retrieve the same item twice
let bobDuplicate = PS.use(1);
expect(bobDuplicate).to.be.undefined;
// set new data
store.setData([{ id : 1, name : 'Marshall' }]);

let marshall = store.get(1);
expect(marshall.name).to.equal('Marshall');

// store should give undefined if the id does not exist
let nonexistant = store.get(2);
expect(nonexistant).to.be.undefined;
});

it('returns free items to the pool', () => {
let bob = PS.use(1);
expect(bob.id).to.equal(1);
it('#remove() removes items stored in it', () => {
let store = new Store({ data : data });
expect(store.data).to.have.length(2);

let sarah = store.get(2);
expect(sarah.id).to.equal(2);
expect(sarah.name).to.equal('Sarah');

let dup = PS.use(1);
expect(dup).to.be.undefined;
// remove sarah
store.remove(2);
expect(store.data).to.have.length(1);
let nonexistant = store.get(2);
expect(nonexistant).to.be.undefined;

// return bob to the pool
PS.free(1);
// should be able to remove all data
store.remove(1);
expect(store.data).to.have.length(0);

let bobAgain = PS.use(1);
expect(bobAgain.id).to.equal(1);
// if an undefined id is attempted to be remove, fail silently
store.remove(undefined);
expect(store.data).to.have.length(0);
});
});

it('#post() adds items to the store', () => {
let store = new Store({ data : data });
expect(store.data).to.have.length(2);

// add another item
let benjamin = { id : 3, name : 'Benjamin' };
store.post(benjamin);
expect(store.data).to.have.length(3);
expect(store.get(3)).to.eql(benjamin);

// add an item without an id should throw an error
try {
store.post({ name : 'Error' });
} catch (error) {
expect(error).to.be.defined;
}
});

it('#contains() reflects stores contents', () => {
let store = new Store({ data : data });
expect(store.data).to.have.length(2);

// check the validity of the items
expect(store.contains(1)).to.be.true;
expect(store.contains(3)).to.be.false;

// flip the boolean results from above
store.remove(1);
store.post({ id : 3, name : 'Bill' });

// assert that they flipped
expect(store.contains(1)).to.be.false;
expect(store.contains(3)).to.be.true;

// make sure setData also clears the store
store.setData([]);
expect(store.contains(1)).to.be.false;
expect(store.contains(3)).to.be.false;
});

it('#clear() removes all data from the store', () => {
let store = new Store({ data : data });
expect(store.data).to.have.length(2);

store.clear();
expect(store.data).to.have.length(0);
expect(store.contains(1)).to.be.false;
expect(store.contains(2)).to.be.false;
expect(store.contains(3)).to.be.false;

store.post({ id : 1 });
expect(store.data).to.have.length(1);
expect(store.contains(1)).to.be.true;
});
});

0 comments on commit 6134793

Please sign in to comment.