Skip to content

Commit

Permalink
Expand unit tests for keyring.
Browse files Browse the repository at this point in the history
  • Loading branch information
caedesvvv committed Oct 27, 2014
1 parent 8159a50 commit baa4c1a
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
4 changes: 2 additions & 2 deletions js/model/keyring.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ IdentityKeyRing.prototype.close = function(name) {
*/
IdentityKeyRing.prototype.rename = function(name, newName, callback) {
var self = this;
var store = this.identities[name].store;
if (!store) {
if (!this.identities[name]) {
throw Error("Identity must be loaded to rename");
}
var store = this.identities[name].store;
var oldIdx = this.availableIdentities.indexOf(name);
// First save under the new name
store.set('name', newName);
Expand Down
57 changes: 57 additions & 0 deletions test/unit/model/keyringSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ define(['testUtils'], function(testUtils) {
clear: function() {
chrome_storage = {};
},
getBytesInUse: function(name, cb) {
cb(42);
},
remove: function(key, callback) {
chrome_storage = {};
callback?callback():null;
},
_: function() {
return chrome_storage;
}
Expand All @@ -53,6 +60,8 @@ define(['testUtils'], function(testUtils) {
testUtils.stub('model/store', function(data, keyring) {
this.store = data;
this.keyring = keyring;
this.set = function(name) {this.name=name;};
this.save = function(cb) {keyring.availableIdentities.push(this.name); cb();};
});

testUtils.stub('model/upgrade', function(store) {
Expand Down Expand Up @@ -174,6 +183,54 @@ define(['testUtils'], function(testUtils) {
expect(chrome.storage.local._()['dw:identity:Satoshi']).toBe(data);
});
});

it('saves new', function() { // private
var data = {name: 'Satoshi2'};
keyring.save('Satoshi2', data, function() {
expect(chrome.storage.local._()['dw:identity:Satoshi2']).toBe(data);
});
});


it('cant rename an identity that is not loaded', function() { // private
var data = {};
expect(function() {
keyring.rename('Satoshi', 'Alex', function() {
});
}).toThrow();
});

it('renames', function() { // private
keyring.load('Satoshi');
keyring.rename('Satoshi', 'Alex', function() {
expect(keyring.availableIdentities).toEqual(['Alex', 'Dorian']);
});
});

it('gets size', function() {
keyring.getSize('Satoshi', function(size) {
expect(size).toBe(42);
});
});

it('gets raw data', function() {
keyring.getRaw('Satoshi', function(data) {
expect(data["dw:identity:Satoshi"].name).toBe('Satoshi');
});
});

it('removes not existing', function() {
expect(function() {
keyring.remove('Alex', function(){});
}).toThrow();
});

it('removes', function() {
expect(keyring.availableIdentities.length).toBe(2);
keyring.remove('Satoshi', function() {
expect(keyring.availableIdentities.length).toBe(1);
});
});

it('clears', function() {
keyring.clear();
Expand Down

0 comments on commit baa4c1a

Please sign in to comment.