diff --git a/src/app/components/main/main.js b/src/app/components/main/main.js index 79e29b67b..6ca1dbb6e 100644 --- a/src/app/components/main/main.js +++ b/src/app/components/main/main.js @@ -66,7 +66,7 @@ app.component('main', { update() { this.$rootScope.reset(); - return this.$peers.active.getAccountPromise(this.account.get().address) + return this.account.getAccountPromise(this.account.get().address) .then((res) => { this.account.set({ balance: res.balance }); }) diff --git a/src/app/components/main/setSecondPassDirective.js b/src/app/components/main/setSecondPassDirective.js index 39001cc5f..792475505 100644 --- a/src/app/components/main/setSecondPassDirective.js +++ b/src/app/components/main/setSecondPassDirective.js @@ -1,6 +1,6 @@ import './secondPass.less'; -app.directive('setSecondPass', (setSecondPass, $peers, $rootScope, success, error) => { +app.directive('setSecondPass', (setSecondPass, Account, $rootScope, success, error) => { /* eslint no-param-reassign: ["error", { "props": false }] */ const SetSecondPassLink = function (scope, element, attrs) { element.bind('click', () => { @@ -8,7 +8,7 @@ app.directive('setSecondPass', (setSecondPass, $peers, $rootScope, success, erro }); scope.passConfirmSubmit = (secondsecret) => { - $peers.active.setSignature(secondsecret, attrs.publicKey, attrs.passphrase) + Account.setSignature(secondsecret, attrs.publicKey, attrs.passphrase) .then(() => { success.dialog('Your second passphrase was successfully registered.'); }) diff --git a/src/app/components/send/send.js b/src/app/components/send/send.js index a617c991c..7f4cc1294 100644 --- a/src/app/components/send/send.js +++ b/src/app/components/send/send.js @@ -80,7 +80,7 @@ app.component('send', { this.promptSecondPassphrase() .then((secondPassphrase) => { - this.$peers.active.sendLSKPromise( + this.account.sendLSK( this.recipient.value, this.amount.raw, this.account.get().passphrase, diff --git a/src/app/components/transactions/transactions.js b/src/app/components/transactions/transactions.js index 58e49f3d0..de967f66e 100644 --- a/src/app/components/transactions/transactions.js +++ b/src/app/components/transactions/transactions.js @@ -66,7 +66,7 @@ app.component('transactions', { limit = 10; } - return this.$peers.listTransactions(this.account.get().address, limit) + return this.account.listTransactions(this.account.get().address, limit) .then(this._processTransactionsResponse.bind(this)) .catch(() => { this.transactions = []; diff --git a/src/app/services/account.js b/src/app/services/account.js index afdaada8b..b77b4d18b 100644 --- a/src/app/services/account.js +++ b/src/app/services/account.js @@ -1,6 +1,6 @@ import lisk from 'lisk-js'; -app.factory('Account', function ($rootScope) { +app.factory('Account', function ($rootScope, $peers, $q) { this.account = {}; const merge = (obj) => { @@ -33,5 +33,49 @@ app.factory('Account', function ($rootScope) { }); }; + this.getAccountPromise = (address) => { + const deferred = $q.defer(); + $peers.active.getAccount(this.account.address, (data) => { + if (data.success) { + deferred.resolve(data.account); + } else { + deferred.resolve({ + address, + balance: 0, + }); + } + }); + return deferred.promise; + }; + + this.sendLSK = (recipient, amount, passphrase, secondPassphrase) => { + const deferred = $q.defer(); + $peers.active.sendLSK(recipient, amount, passphrase, secondPassphrase, (data) => { + if (data.success) { + return deferred.resolve(data); + } + return deferred.reject(data); + }); + return deferred.promise; + }; + + this.listTransactions = (address, limit, offset) => $peers.sendRequestPromise('transactions', { + senderId: address, + recipientId: address, + limit: limit || 20, + offset: offset || 0, + }); + + this.setSignature = (secondSecret, publicKey, secret) => { + const deferred = $q.defer(); + $peers.active.sendRequest('signatures', { secondSecret, publicKey, secret }, (res) => { + if (res.success) { + deferred.resolve(res); + } + deferred.reject(res); + }); + return deferred.promise; + }; + return this; }); diff --git a/src/app/services/peers/peers.js b/src/app/services/peers/peers.js index b1d270e52..7d2a47bb7 100644 --- a/src/app/services/peers/peers.js +++ b/src/app/services/peers/peers.js @@ -31,7 +31,7 @@ app.factory('$peers', ($timeout, $cookies, $location, $q) => { conf.testnet = true; } - this.setPeerAPIObject(conf); + this.active = lisk.api(conf); if (!this.stack) { this.stack = this.active.listPeers(); this.stack.localhost = [localhostConf, { @@ -64,67 +64,8 @@ app.factory('$peers', ($timeout, $cookies, $location, $q) => { return deferred.promise; } - listTransactions(address, limit, offset) { - return this.sendRequestPromise('transactions', { - senderId: address, - recipientId: address, - limit: limit || 20, - offset: offset || 0, - }); - } - - setPeerAPIObject(config) { - this.active = lisk.api(config); - - this.active.getStatusPromise = () => this.sendRequestPromise('loader/status', {}); - - this.active.getAccountPromise = (address) => { - const deferred = $q.defer(); - this.active.getAccount(address, (data) => { - if (data.success) { - deferred.resolve(data.account); - } else { - deferred.resolve({ - address, - balance: 0, - }); - } - }); - return deferred.promise; - }; - - this.active.sendLSKPromise = (recipient, amount, passphrase, secondPassphrase) => { - const deferred = $q.defer(); - this.active.sendLSK(recipient, amount, passphrase, secondPassphrase, (data) => { - if (data.success) { - return deferred.resolve(data); - } - return deferred.reject(data); - }); - return deferred.promise; - }; - - this.active.listTransactionsPromise = (address, limit, offset) => { - const deferred = $q.defer(); - this.active.listTransactions(address, limit, offset, (data) => { - if (data.success) { - return deferred.resolve(data); - } - return deferred.reject(data); - }); - return deferred.promise; - }; - - this.active.setSignature = (secondSecret, publicKey, secret) => { - const deferred = $q.defer(); - this.active.sendRequest('signatures', { secondSecret, publicKey, secret }, (res) => { - if (res.success) { - deferred.resolve(res); - } - deferred.reject(res); - }); - return deferred.promise; - }; + getStatusPromise() { + return this.sendRequestPromise('loader/status', {}); } check() { @@ -137,7 +78,7 @@ app.factory('$peers', ($timeout, $cookies, $location, $q) => { return; } - this.active.getStatusPromise() + this.getStatusPromise() .then(() => this.online = true) .catch(() => this.online = false) .finally(() => next()); diff --git a/src/test/components/main/main.spec.js b/src/test/components/main/main.spec.js index 653afe58f..a7da47309 100644 --- a/src/test/components/main/main.spec.js +++ b/src/test/components/main/main.spec.js @@ -155,10 +155,9 @@ describe('main component controller', () => { balance: '0', passphrase: 'wagon stock borrow episode laundry kitten salute link globe zero feed marble', }); - controller.$peers.active = { - getAccountPromise() { - return deffered.promise; - }, + const mock = sinon.mock(controller.account); + mock.expects('getAccountPromise').returns(deffered.promise); + controller.$peers = { getStatusPromise() { return $q.defer().promise; }, @@ -167,7 +166,7 @@ describe('main component controller', () => { account.reset(); }); - it('calls this.$peers.active.getAccountPromise(this.address) and then sets balance', () => { + it('calls this.account.getAccountPromise(this.address) and then sets balance', () => { expect(account.get().balance).to.equal(undefined); controller.update(); deffered.resolve({ balance: 12345 }); @@ -175,7 +174,7 @@ describe('main component controller', () => { expect(account.get().balance).to.equal(12345); }); - it('calls this.$peers.active.getAccountPromise(this.address) and if it fails, then resets this.account.balance and reject the promise that update() returns', () => { + it('calls this.account.getAccountPromise(this.address) and if it fails, then resets this.account.balance and reject the promise that update() returns', () => { const spy = sinon.spy(controller.$q, 'reject'); controller.update(); deffered.reject(); diff --git a/src/test/components/main/setSecondPassDirective.spec.js b/src/test/components/main/setSecondPassDirective.spec.js index 5f2697567..addb80397 100644 --- a/src/test/components/main/setSecondPassDirective.spec.js +++ b/src/test/components/main/setSecondPassDirective.spec.js @@ -10,7 +10,7 @@ describe('setSecondPass Directive', () => { let $scope; let $rootScope; let element; - let $peers; + let account; let setSecondPass; let $q; let success; @@ -22,12 +22,12 @@ describe('setSecondPass Directive', () => { // Store references to $rootScope and $compile // so they are available to all tests in this describe block - inject((_$compile_, _$rootScope_, _setSecondPass_, _$peers_, _$q_, _success_, _error_) => { + inject((_$compile_, _$rootScope_, _setSecondPass_, _Account_, _$q_, _success_, _error_) => { // The injector unwraps the underscores (_) from around the parameter names when matching $compile = _$compile_; $rootScope = _$rootScope_; setSecondPass = _setSecondPass_; - $peers = _$peers_; + account = _Account_; $q = _$q_; success = _success_; error = _error_; @@ -41,8 +41,7 @@ describe('setSecondPass Directive', () => { describe('SetSecondPassLink', () => { it('listens for an onAfterSignup event', () => { - $peers.active = { setSignature() {} }; - const mock = sinon.mock($peers.active); + const mock = sinon.mock(account); const deffered = $q.defer(); mock.expects('setSignature').returns(deffered.promise); @@ -69,9 +68,8 @@ describe('setSecondPass Directive', () => { }); describe('scope.passConfirmSubmit', () => { - it('should call $peers.active.setSignature', () => { - $peers.active = { setSignature() {} }; - const mock = sinon.mock($peers.active); + it('should call account.setSignature', () => { + const mock = sinon.mock(account); const deffered = $q.defer(); mock.expects('setSignature').returns(deffered.promise); @@ -84,9 +82,8 @@ describe('setSecondPass Directive', () => { expect(spy).to.have.been.calledWith(); }); - it('should show error dialog if trying to set second passphrase mulpiple times', () => { - $peers.active = { setSignature() {} }; - const mock = sinon.mock($peers.active); + it('should show error dialog if trying to set second passphrase multiple times', () => { + const mock = sinon.mock(account); const deffered = $q.defer(); mock.expects('setSignature').returns(deffered.promise); @@ -107,8 +104,7 @@ describe('setSecondPass Directive', () => { }); it('should show error dialog if account does not have enough LSK', () => { - $peers.active = { setSignature() {} }; - const mock = sinon.mock($peers.active); + const mock = sinon.mock(account); const deffered = $q.defer(); mock.expects('setSignature').returns(deffered.promise); @@ -121,8 +117,7 @@ describe('setSecondPass Directive', () => { }); it('should show error dialog for all the other errors', () => { - $peers.active = { setSignature() {} }; - const mock = sinon.mock($peers.active); + const mock = sinon.mock(account); const deffered = $q.defer(); mock.expects('setSignature').returns(deffered.promise); diff --git a/src/test/components/send/send.spec.js b/src/test/components/send/send.spec.js index 2825c9089..dcbf50206 100644 --- a/src/test/components/send/send.spec.js +++ b/src/test/components/send/send.spec.js @@ -60,23 +60,20 @@ describe('Send component', () => { describe('send transaction', () => { let $q; - let $peers; let success; - beforeEach(inject((_success_, _$q_, _$peers_) => { + beforeEach(inject((_success_, _$q_) => { success = _success_; $q = _$q_; - $peers = _$peers_; })); it('should allow to send a transaction', () => { const RECIPIENT_ADDRESS = '5932438298200837883L'; const AMOUNT = '10'; - $peers.active = { sendLSKPromise() {} }; - const mock = sinon.mock($peers.active); + const mock = sinon.mock(account); const deffered = $q.defer(); - mock.expects('sendLSKPromise').returns(deffered.promise); + mock.expects('sendLSK').returns(deffered.promise); const spy = sinon.spy(success, 'dialog'); @@ -95,10 +92,9 @@ describe('Send component', () => { const RECIPIENT_ADDRESS = '5932438298200837883L'; const AMOUNT = lsk.normalize(account.get().balance - 10000000); - $peers.active = { sendLSKPromise() {} }; - const mock = sinon.mock($peers.active); + const mock = sinon.mock(account); const deffered = $q.defer(); - mock.expects('sendLSKPromise').returns(deffered.promise); + mock.expects('sendLSK').returns(deffered.promise); const spy = sinon.spy(success, 'dialog'); @@ -182,11 +178,10 @@ describe('send component controller', () => { expect(spy).to.have.been.calledWith(); }); - it('calls this.$peers.active.sendLSKPromise() and success.dialog on success', () => { - controller.$peers = { active: { sendLSKPromise() {} } }; - const mock = sinon.mock(controller.$peers.active); + it('calls this.account.sendLSK() and success.dialog on success', () => { + const mock = sinon.mock(controller.account); const deffered = $q.defer(); - mock.expects('sendLSKPromise').returns(deffered.promise); + mock.expects('sendLSK').returns(deffered.promise); controller.go(); const spy = sinon.spy(controller.success, 'dialog'); @@ -197,11 +192,10 @@ describe('send component controller', () => { }); }); - it('calls this.$peers.active.sendLSKPromise() and error.dialog on error', () => { - controller.$peers = { active: { sendLSKPromise() {} } }; - const mock = sinon.mock(controller.$peers.active); + it('calls this.account.sendLSK() and error.dialog on error', () => { + const mock = sinon.mock(controller.account); const deffered = $q.defer(); - mock.expects('sendLSKPromise').returns(deffered.promise); + mock.expects('sendLSK').returns(deffered.promise); controller.go(); const spy = sinon.spy(controller.error, 'dialog'); diff --git a/src/test/components/transactions/transactions.spec.js b/src/test/components/transactions/transactions.spec.js index 79670626d..64f89bf20 100644 --- a/src/test/components/transactions/transactions.spec.js +++ b/src/test/components/transactions/transactions.spec.js @@ -14,19 +14,18 @@ describe('transactions component controller', () => { let controller; let $componentController; let account; - let $peers; + let mock; - beforeEach(inject((_$componentController_, _$rootScope_, _$q_, _Account_, _$peers_) => { + beforeEach(inject((_$componentController_, _$rootScope_, _$q_, _Account_) => { $componentController = _$componentController_; $rootScope = _$rootScope_; $q = _$q_; account = _Account_; - $peers = _$peers_; })); beforeEach(() => { $scope = $rootScope.$new(); - const mock = sinon.mock($peers); + mock = sinon.mock(account); const deffered = $q.defer(); mock.expects('listTransactions').returns(deffered.promise); mock.expects('listTransactions').returns(deffered.promise); @@ -37,6 +36,11 @@ describe('transactions component controller', () => { }); }); + afterEach(() => { + mock.verify(); + mock.restore(); + }); + describe('$onDestroy()', () => { it('cancels update timeout', () => { const spy = sinon.spy(controller.$timeout, 'cancel'); @@ -54,46 +58,43 @@ describe('transactions component controller', () => { }); describe('update(show, more)', () => { - let mock; + let transactionsDeferred; beforeEach(() => { - controller.$peers.listTransactions = () => {}; - mock = sinon.mock(controller.$peers); - mock.expects('listTransactions').returns($q(() => {})); + transactionsDeferred = $q.defer(); }); it('sets this.loading = true', () => { + mock.expects('listTransactions').returns(transactionsDeferred.promise); controller.update(); expect(controller.loading).to.equal(true); }); it('sets this.loading_show = true if show == true', () => { + mock.expects('listTransactions').returns(transactionsDeferred.promise); controller.update(true); expect(controller.loading_show).to.equal(true); }); it('doesn\'t change this.loading_show if show == false', () => { + mock.expects('listTransactions').returns(transactionsDeferred.promise); controller.update(false); expect(controller.loading_show).to.equal(undefined); }); it('cancels update timeout', () => { + mock.expects('listTransactions').returns(transactionsDeferred.promise); const spy = sinon.spy(controller.$timeout, 'cancel'); controller.update(); expect(spy).to.have.been.calledWith(controller.timeout); }); - it('calls this.$peers.listTransactions(account.get().address, limit) with limit = 10 by default', () => { - controller.$peers.listTransactions = () => {}; - mock = sinon.mock(controller.$peers); - const transactionsDeferred = $q.defer(); + it('calls this.account.listTransactions(account.get().address, limit) with limit = 10 by default', () => { mock.expects('listTransactions').withArgs(account.get().address, 10).returns(transactionsDeferred.promise); controller.update(); transactionsDeferred.reject(); $scope.$apply(); - mock.verify(); - mock.restore(); }); }); @@ -119,21 +120,17 @@ describe('transactions component controller', () => { describe('constructor()', () => { it('sets $watch on acount to run init()', () => { - const mock = sinon.mock(controller); + mock = sinon.mock(controller); mock.expects('init').withArgs(); account.set({ balance: 1000 }); $scope.$apply(); - mock.verify(); - mock.restore(); }); it('sets to run reset() and update() $on "peerUpdate" is $emited', () => { - const mock = sinon.mock(controller); + mock = sinon.mock(controller); mock.expects('reset').withArgs(); mock.expects('update').withArgs(true); controller.$scope.$emit('peerUpdate'); - mock.verify(); - mock.restore(); }); }); }); diff --git a/src/test/services/peers/peers.spec.js b/src/test/services/peers/peers.spec.js index 4061a2dba..5af2242d0 100644 --- a/src/test/services/peers/peers.spec.js +++ b/src/test/services/peers/peers.spec.js @@ -27,68 +27,15 @@ describe('Factory: $peers', () => { }); }); - describe('setPeerAPIObject', () => { - const RECIPIENT_ADDRESS = '5932438298200837883L'; - const AMOUNT = '10'; - const PASSPHRASE = 'robust swift grocery peasant forget share enable convince deputy road keep cheap'; - - beforeEach(() => { - $peers.setActive(); - $peers.setPeerAPIObject({ - node: 'localhost', - port: 4000, - testnet: true, - nethash: '198f2b61a8eb95fbeed58b8216780b68f697f26b849acf00c8c93bb9b24f783d', - }); - }); - - it('sets getter/setter methods on $peers.active if it\'s called with correct configs', () => { - expect($peers.active.getStatusPromise).not.to.equal(undefined); - expect($peers.active.getAccountPromise).not.to.equal(undefined); - expect($peers.active.sendLSKPromise).not.to.equal(undefined); - }); - - it('creates $peers.active.getStatusPromise which returns a promise', () => { - const promise = $peers.active.getStatusPromise(); - $rootScope.$apply(); - expect(promise.then.constructor).to.be.equal(Function); - }); - - it('creates $peers.active.getAccountPromise which returns a promise', () => { - const promise = $peers.active.getAccountPromise(RECIPIENT_ADDRESS); - $rootScope.$apply(); - expect(promise.then.constructor).to.be.equal(Function); - }); - - it('creates $peers.active.sendLSKPromise which returns a promise', () => { - const promise = $peers.active.sendLSKPromise(RECIPIENT_ADDRESS, AMOUNT, PASSPHRASE); - $rootScope.$apply(); - expect(promise.then.constructor).to.be.equal(Function); - }); - - it.skip('creates $peers.active.listTransactionsPromise which returns a promise', () => { - const promise = $peers.active.listTransactionsPromise(RECIPIENT_ADDRESS, AMOUNT); - $rootScope.$apply(); - expect(promise.then.constructor).to.be.equal(Function); - }); - }); - describe('check()', () => { let deffered; let mock; beforeEach(() => { deffered = $q.defer(); - $peers.active = { - getAccountPromise() { - return deffered.promise; - }, - getStatusPromise() { - return $q.defer().promise; - }, - }; - mock = sinon.mock($peers.active); - mock.expects('getStatusPromise').withArgs().returns(deffered.promise); + mock = sinon.mock($peers); + mock.expects('getStatusPromise').returns(deffered.promise); + $peers.active = {}; }); afterEach(() => {