From 1abf1dc90d86f91d9196b1d3fe0dcf9476c04e2a Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Tue, 24 Jun 2014 12:57:15 -0300 Subject: [PATCH 1/2] small refactor on rootScope --- js/controllers/addresses.js | 17 ++-- js/models/core/PublicKeyRing.js | 134 +++++++++++++++++--------------- js/services/controllerUtils.js | 52 ++++++++++--- js/services/socket.js | 14 +++- 4 files changed, 130 insertions(+), 87 deletions(-) diff --git a/js/controllers/addresses.js b/js/controllers/addresses.js index 8e9e0a213..3a0f7bacd 100644 --- a/js/controllers/addresses.js +++ b/js/controllers/addresses.js @@ -12,27 +12,27 @@ angular.module('copayApp.controllers').controller('AddressesController', controllerUtils.setSocketHandlers(); controllerUtils.updateAddressList(); $scope.loading = false; - },1); + }, 1); }); }; - $scope.selectAddress = function (addr) { + $scope.selectAddress = function(addr) { $scope.selectedAddr = addr; }; $rootScope.$watch('addrInfos', function() { $scope.addressList(); - }); + }); - $scope.addressList = function () { + $scope.addressList = function() { $scope.addresses = []; var addrInfos = $rootScope.addrInfos; if (addrInfos) { - for(var i=0;i 0) { + notification.info('New Transaction', ($rootScope.txAlertCount == 1) ? 'You have a pending transaction proposal' : 'You have ' + $rootScope.txAlertCount + ' pending transaction proposals', txAlertCount); + } + }); + }; + root.startNetwork = function(w, $scope) { + Socket.removeAllListeners(); + root.setupRootVariables(); root.installStartupHandlers(w, $scope); + root.setSocketHandlers(); var handlePeerVideo = function(err, peerID, url) { if (err) { @@ -103,8 +130,8 @@ angular.module('copayApp.services') }); w.on('publicKeyRingUpdated', function(dontDigest) { - root.setSocketHandlers(); root.updateAddressList(); + root.setSocketHandlers(); if (!dontDigest) { $rootScope.$digest(); } @@ -122,7 +149,7 @@ angular.module('copayApp.services') }); }, 3000); }); - w.on('txProposalEvent', function(e){ + w.on('txProposalEvent', function(e) { switch (e.type) { case 'signed': var user = w.publicKeyRing.nicknameForCopayer(e.cId); @@ -273,20 +300,23 @@ angular.module('copayApp.services') if (!$rootScope.wallet) return; var currentAddrs = Socket.getListeners(); - var addrs = $rootScope.wallet.getAddressesStr(); + var allAddrs = $rootScope.addrInfos; var newAddrs = []; - for (var i in addrs) { - var a = addrs[i]; - if (!currentAddrs[a]) + for (var i in allAddrs) { + var a = allAddrs[i]; + if (!currentAddrs[a.addressStr]) newAddrs.push(a); } for (var i = 0; i < newAddrs.length; i++) { - Socket.emit('subscribe', newAddrs[i]); + Socket.emit('subscribe', newAddrs[i].addressStr); } - newAddrs.forEach(function(addr) { - Socket.on(addr, function(txid) { - $rootScope.receivedFund = [txid, addr]; + newAddrs.forEach(function(a) { + Socket.on(a.addressStr, function(txid) { + + if (!a.isChange) + notification.funds('Received fund', a.addressStr); + root.updateBalance(function() { $rootScope.$digest(); }); @@ -297,7 +327,7 @@ angular.module('copayApp.services') Socket.emit('subscribe', 'inv'); Socket.on('block', function(block) { root.updateBalance(function() { - $rootScope.$digest(); + $rootScope.$digest(); }); }); } diff --git a/js/services/socket.js b/js/services/socket.js index 10f843b6f..99dce34b4 100644 --- a/js/services/socket.js +++ b/js/services/socket.js @@ -38,8 +38,12 @@ angular.module('copayApp.services').factory('Socket', var ret = {}; var addrList = listeners - .filter(function(i) { return i.event != 'block'; }) - .map(function(i) {return i.event;}); + .filter(function(i) { + return i.event != 'block'; + }) + .map(function(i) { + return i.event; + }); for (var i in addrList) { ret[addrList[i]] = 1; @@ -47,7 +51,9 @@ angular.module('copayApp.services').factory('Socket', return ret; }, isListeningBlocks: function() { - return listeners.filter(function(i) { return i.event == 'block'; }).length > 0; + return listeners.filter(function(i) { + return i.event == 'block'; + }).length > 0; }, emit: function(event, data, callback) { socket.emit(event, data, function() { @@ -62,7 +68,7 @@ angular.module('copayApp.services').factory('Socket', removeAllListeners: function() { for (var i = 0; i < listeners.length; i++) { var details = listeners[i]; - socket.removeListener(details.event, details.fn); + socket.removeAllListeners(details.event); } listeners = []; From 6b7b00769d7b57752c69d64f495a55d15c5fa41c Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Tue, 24 Jun 2014 13:18:13 -0300 Subject: [PATCH 2/2] add tests --- test/test.PublicKeyRing.js | 6 +- test/unit/services/servicesSpec.js | 117 +++++++++++++++-------------- 2 files changed, 66 insertions(+), 57 deletions(-) diff --git a/test/test.PublicKeyRing.js b/test/test.PublicKeyRing.js index ea9811443..36d84ee15 100644 --- a/test/test.PublicKeyRing.js +++ b/test/test.PublicKeyRing.js @@ -147,11 +147,13 @@ describe('PublicKeyRing model', function() { for (var i = 0; i < 2; i++) w.generateAddress(isChange); - var as = w.getAddresses(); + var as = w.getAddressesInfo(); as.length.should.equal(4); for (var j in as) { var a = as[j]; - a.isValid().should.equal(true); + a.address.isValid().should.equal(true); + a.addressStr.should.equal(a.address.toString()); + a.isChange.should.equal([false, false, true, true][j]); } }); diff --git a/test/unit/services/servicesSpec.js b/test/unit/services/servicesSpec.js index 4bb1e959b..eae710e15 100644 --- a/test/unit/services/servicesSpec.js +++ b/test/unit/services/servicesSpec.js @@ -70,65 +70,72 @@ describe("Unit: Walletfactory Service", function() { }); describe("Unit: controllerUtils", function() { - beforeEach(angular.mock.module('copayApp.services')); + beforeEach(angular.mock.module('copayApp.services')); - it('should updateBalance in bits', inject(function(controllerUtils, $rootScope) { - expect(controllerUtils.updateBalance).not.to.equal(null); - scope = $rootScope.$new(); + it('should updateBalance in bits', inject(function(controllerUtils, $rootScope) { + expect(controllerUtils.updateBalance).not.to.equal(null); + scope = $rootScope.$new(); - $rootScope.wallet = new FakeWallet(); - var addr = '1CjPR7Z5ZSyWk6WtXvSFgkptmpoi4UM9BC'; - var a = {}; - a[addr] = 100; - //SATs - $rootScope.wallet.set(100000001, 90000002, a); + $rootScope.wallet = new FakeWallet(); + var addr = '1CjPR7Z5ZSyWk6WtXvSFgkptmpoi4UM9BC'; + var a = {}; + a[addr] = 100; + //SATs + $rootScope.wallet.set(100000001, 90000002, a); - //retuns values in DEFAULT UNIT(bits) - controllerUtils.updateBalance(function() { - expect($rootScope.totalBalanceBTC).to.be.equal('1.0000'); - expect($rootScope.availableBalanceBTC).to.be.equal('0.9000'); - expect($rootScope.totalBalance).to.be.equal(1000000.01); - expect($rootScope.availableBalance).to.be.equal(900000.02); - expect($rootScope.addrInfos).not.to.equal(null); - expect($rootScope.addrInfos[0].address).to.equal(addr); + //retuns values in DEFAULT UNIT(bits) + controllerUtils.updateBalance(function() { + expect($rootScope.totalBalanceBTC).to.be.equal('1.0000'); + expect($rootScope.availableBalanceBTC).to.be.equal('0.9000'); + expect($rootScope.totalBalance).to.be.equal(1000000.01); + expect($rootScope.availableBalance).to.be.equal(900000.02); + expect($rootScope.addrInfos).not.to.equal(null); + expect($rootScope.addrInfos[0].address).to.equal(addr); + }); + })); + + it('should set the rootScope', inject(function(controllerUtils, $rootScope) { + controllerUtils.setupRootVariables(function() { + expect($rootScope.txAlertCount).to.be.equal(0); + expect($rootScope.insightError).to.be.equal(0); + expect($rootScope.isCollapsed).to.be.equal(0); + expect($rootScope.unitName).to.be.equal('bits'); + }); + })); }); - })); + describe("Unit: Notification Service", function() { + beforeEach(angular.mock.module('copayApp.services')); + it('should contain a notification service', inject(function(notification) { + expect(notification).not.to.equal(null); + })); + }); -}); + describe("Unit: Backup Service", function() { + beforeEach(angular.mock.module('copayApp.services')); + it('should contain a backup service', inject(function(backupService) { + expect(backupService).not.to.equal(null); + })); + it('should backup in file', inject(function(backupService) { + var mock = sinon.mock(window); + var expectation = mock.expects('saveAs'); + backupService.download(new FakeWallet()); + expectation.once(); + })); + }); -describe("Unit: Notification Service", function() { - beforeEach(angular.mock.module('copayApp.services')); - it('should contain a notification service', inject(function(notification) { - expect(notification).not.to.equal(null); - })); -}); - -describe("Unit: Backup Service", function() { - beforeEach(angular.mock.module('copayApp.services')); - it('should contain a backup service', inject(function(backupService) { - expect(backupService).not.to.equal(null); - })); - it('should backup in file', inject(function(backupService) { - var mock = sinon.mock(window); - var expectation = mock.expects('saveAs'); - backupService.download(new FakeWallet()); - expectation.once(); - })); -}); - -describe("Unit: isMobile Service", function() { - beforeEach(angular.mock.module('copayApp.services')); - it('should contain a isMobile service', inject(function(isMobile) { - expect(isMobile).not.to.equal(null); - })); - it('should not detect mobile by default', inject(function(isMobile) { - isMobile.any().should.equal(false); - })); - it('should detect mobile if user agent is Android', inject(function(isMobile) { - navigator.__defineGetter__('userAgent', function() { - return 'Android 2.2.3'; - }); - isMobile.any().should.equal(true); - })); -}); + describe("Unit: isMobile Service", function() { + beforeEach(angular.mock.module('copayApp.services')); + it('should contain a isMobile service', inject(function(isMobile) { + expect(isMobile).not.to.equal(null); + })); + it('should not detect mobile by default', inject(function(isMobile) { + isMobile.any().should.equal(false); + })); + it('should detect mobile if user agent is Android', inject(function(isMobile) { + navigator.__defineGetter__('userAgent', function() { + return 'Android 2.2.3'; + }); + isMobile.any().should.equal(true); + })); + });