Skip to content
This repository has been archived by the owner on Apr 15, 2019. It is now read-only.

Commit

Permalink
Move $peers service account-related methods to Account #169
Browse files Browse the repository at this point in the history
  • Loading branch information
slaweet committed May 4, 2017
1 parent 99b6c1c commit 3028fbe
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 177 deletions.
2 changes: 1 addition & 1 deletion src/app/components/main/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
})
Expand Down
4 changes: 2 additions & 2 deletions src/app/components/main/setSecondPassDirective.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
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', () => {
setSecondPass.show();
});

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.');
})
Expand Down
2 changes: 1 addition & 1 deletion src/app/components/send/send.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/app/components/transactions/transactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down
46 changes: 45 additions & 1 deletion src/app/services/account.js
Original file line number Diff line number Diff line change
@@ -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) => {
Expand Down Expand Up @@ -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;
});
67 changes: 4 additions & 63 deletions src/app/services/peers/peers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down Expand Up @@ -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() {
Expand All @@ -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());
Expand Down
11 changes: 5 additions & 6 deletions src/test/components/main/main.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
Expand All @@ -167,15 +166,15 @@ 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 });
$scope.$apply();
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();
Expand Down
25 changes: 10 additions & 15 deletions src/test/components/main/setSecondPassDirective.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('setSecondPass Directive', () => {
let $scope;
let $rootScope;
let element;
let $peers;
let account;
let setSecondPass;
let $q;
let success;
Expand All @@ -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_;
Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand Down
30 changes: 13 additions & 17 deletions src/test/components/send/send.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,22 @@ describe('Send component', () => {

describe('send transaction', () => {
let $q;
let $peers;
let account;
let success;

beforeEach(inject((_success_, _$q_, _$peers_) => {
beforeEach(inject((_success_, _$q_, _Account_) => {
success = _success_;
$q = _$q_;
$peers = _$peers_;
account = _Account_;
}));

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');

Expand All @@ -95,10 +94,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');

Expand Down Expand Up @@ -182,11 +180,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');
Expand All @@ -197,11 +194,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');
Expand Down
Loading

0 comments on commit 3028fbe

Please sign in to comment.