diff --git a/js/models/Identity.js b/js/models/Identity.js index 338d7152e..3799e329f 100644 --- a/js/models/Identity.js +++ b/js/models/Identity.js @@ -239,6 +239,7 @@ Identity.prototype.retrieveWalletFromStorage = function(walletId, opts, cb) { Identity.prototype.storeWallet = function(wallet, cb) { preconditions.checkArgument(wallet && _.isObject(wallet)); + wallet.setVersion(this.version); var val = wallet.toObj(); var key = wallet.getStorageKey(); log.debug('Storing wallet:' + wallet.getName()); diff --git a/js/models/Wallet.js b/js/models/Wallet.js index 010294bfc..e45a5edb9 100644 --- a/js/models/Wallet.js +++ b/js/models/Wallet.js @@ -2623,6 +2623,14 @@ Wallet.prototype.getTransactionHistoryCsv = function(cb) { } +/** + * @desc Sets the version of this wallet object + * + * @param {string} version - the new version for the wallet + */ +Wallet.prototype.setVersion = function(version) { + this.version = this.opts.version = version; +}; /** * @desc Return a list of past transactions diff --git a/test/Identity.js b/test/Identity.js index f39d3db42..caecc3f03 100644 --- a/test/Identity.js +++ b/test/Identity.js @@ -85,6 +85,7 @@ describe('Identity model', function() { obj: 1 }); w.getName = sinon.stub().returns('name'); + w.setVersion = sinon.stub(); w.on = sinon.stub(); w.netStart = sinon.stub(); w.args = args; @@ -234,8 +235,62 @@ describe('Identity model', function() { }); }); - describe.skip('#storeWallet', function() { - // TODO test storeWallet + describe('#storeWallet', function() { + var iden = null; + var storage = null; + beforeEach(function() { + storage = sinon.stub(); + storage.setCredentials = sinon.stub(); + + var opts = { + email: 'test@test.com', + password: '123', + network: { + testnet: { + url: 'https://test-insight.bitpay.com:443' + }, + livenet: { + url: 'https://insight.bitpay.com:443' + }, + }, + storage: storage, + }; + + iden = new Identity(opts); + }); + + it('should store a simple wallet', function (done) { + storage.setItem = sinon.stub().yields(null); + var w = { + toObj: sinon.stub().returns({ key1: 'val1' }), + getStorageKey: sinon.stub().returns('storage_key'), + getName: sinon.stub().returns('name'), + setVersion: sinon.spy(), + }; + iden.storeWallet(w, function (err) { + should.not.exist(err); + storage.setItem.calledOnce.should.be.true; + storage.setItem.calledWith('storage_key', { key1: 'val1' }); + done(); + }); + }); + it('should change wallet version when storing', function (done) { + storage.setItem = sinon.stub().yields(null); + var w = { + toObj: sinon.stub().returns({ key1: 'val1' }), + getStorageKey: sinon.stub().returns('storage_key'), + getName: sinon.stub().returns('name'), + setVersion: sinon.spy(), + version: '1.0', + opts: { version: '1.0' }, + }; + iden.version = '2.0'; + iden.storeWallet(w, function (err) { + should.not.exist(err); + w.setVersion.calledWith('2.0').should.be.true; + done(); + }); + }); });