-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(store): implement store.clear()
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
Showing
7 changed files
with
147 additions
and
52 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
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; | ||
}); | ||
}); |