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

Commit

Permalink
Merge pull request #1380 from blockchain/exchange-tests
Browse files Browse the repository at this point in the history
Tests
  • Loading branch information
plondon authored Dec 18, 2017
2 parents 42d3eae + 077e1f2 commit 2f3dbe8
Show file tree
Hide file tree
Showing 14 changed files with 1,225 additions and 2 deletions.
2 changes: 2 additions & 0 deletions assets/js/controllers/sfox/sfoxCheckout.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ function SfoxCheckoutController ($scope, $timeout, $stateParams, $q, Wallet, MyW

$scope.selling = sfox.selling;
$scope.sellQuoteHandler = sfox.fetchSellQuote.bind(null, exchange);

$scope.sellHandler = (quote) => sfox.sell($scope.state.account, quote)
.then(submitTx)
.then(enableSiftScience)
Expand All @@ -35,6 +36,7 @@ function SfoxCheckoutController ($scope, $timeout, $stateParams, $q, Wallet, MyW
const setRate = (res) => { $scope.rate = Math.abs(res.quoteAmount); };
$scope.getRate = () => $scope.sellQuoteHandler(1e8, 'BTC', $scope.dollars.code).then(setRate);
$scope.getRate().then(() => sfox.setSellMin($scope.sellLimits($scope.rate).min));

$scope.updateRate = (quote) => $scope.rate = quote.rate;

$scope.sellLimits = (rate) => {
Expand Down
121 changes: 121 additions & 0 deletions tests/components/exchange/exchange-confirm_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
describe('exchange-confirm.component', () => {
let $rootScope;
let $compile;
let $templateCache;
let $componentController;
let scope;
let Wallet;
let $q;

let getTrades = () => [
{
id: 1,
amount: 100,
subscriptionId: 1,
fiatAmount: 75
},
{
id: 2,
amount: 200,
subscriptionId: 2,
fiatAmount: 275
}
];

let mockMediums = () =>
({
ach: {
buy () { return $q.resolve(); }
}
})
;

let mockQuote = fail =>
({
quoteAmount: 150,
rate: 867,
expiresAt: new Date(Date.now() + 1e8),
getPaymentMediums () { if (fail) { return $q.reject(fail); } else { return $q.resolve(mockMediums()); } }
})
;

let handlers = {
quote: mockQuote(),
viewDetails () { return $q.resolve(); },
onSuccess () { return $q.resolve(); },
handleTrade: (quote) => $q.resolve().then(handlers.onSuccess),
exTrade: getTrades()[0],
onExpiration: () => $q.resolve().then(() => { return $q.resolve(); })
};

let getControllerScope = function (bindings) {
scope = $rootScope.$new(true);
$componentController('exchangeConfirm', {$scope: scope}, bindings);
let template = $templateCache.get('templates/exchange/confirm.pug');
$compile(template)(scope);
return scope;
};

beforeEach(module('walletApp'));
beforeEach(() =>
angular.mock.inject(function ($injector, _$rootScope_, _$compile_, _$templateCache_, _$componentController_, $httpBackend) {
// TODO: use Wallet mock, so we don't need to mock this $httpBackend call
$httpBackend.whenGET('/Resources/wallet-options.json').respond();

$rootScope = _$rootScope_;
$compile = _$compile_;
$templateCache = _$templateCache_;
$componentController = _$componentController_;

$q = $injector.get('$q');
Wallet = $injector.get('Wallet');
let MyWallet = $injector.get('MyWallet');

MyWallet.wallet = {};
Wallet.accounts = () => [];
Wallet.getDefaultAccount = () => ({});

MyWallet.wallet.external = {
sfox: {
profile: {}
}
};
}));

it('should set trade to exTrade', () => {
scope = getControllerScope(handlers);
});

describe('.getTimeToExpiration()', () => {
beforeEach(function () {
scope = getControllerScope(handlers);
});

it('should return the diff between quote expiration and now', () => {
expect(scope.getTimeToExpiration()).toBeDefined();
});
});

describe('.onExpiration()', () => {
beforeEach(function () {
scope = getControllerScope(handlers);
});

it('should return the diff between quote expiration and now', () => {
scope.onExpiration();
expect(scope.$ctrl.quote).toBe(null);
expect(scope.locked).toBe(true);
});
});

describe('.trade()', () => {
beforeEach(function () {
scope = getControllerScope(handlers);
});

it('should return the diff between quote expiration and now', () => {
scope.trade();
expect(scope.locked).toBe(true);
});
});
});
132 changes: 132 additions & 0 deletions tests/components/exchange/exchange-recurring-trades_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
describe('exchange-recurring-trades.component', () => {
let $rootScope;
let $compile;
let $templateCache;
let $componentController;
let scope;
let Wallet;
let $q;

let getTrades = () => [
{
id: 1,
amount: 100,
subscriptionId: 1
},
{
id: 2,
amount: 200,
subscriptionId: 2
}
];

let handlers = {
buy () { return true; },
subscription: { id: 1, isActive: true },
partnerService: {
cancelTrade: () => $q.resolve().then(() => { return false; })
},
namespace: 'COINIFY',
fiat: {code: 'USD'},
recurringBuyLimit () { return 100; },
trades () { return getTrades; }
};

let getControllerScope = function (bindings) {
scope = $rootScope.$new(true);
$componentController('exchangeRecurringTrades', {$scope: scope}, bindings);
let template = $templateCache.get('templates/exchange/recurring-trades.pug');
$compile(template)(scope);
return scope;
};

beforeEach(module('walletApp'));
beforeEach(() =>
angular.mock.inject(function ($injector, _$rootScope_, _$compile_, _$templateCache_, _$componentController_, $httpBackend) {
// TODO: use Wallet mock, so we don't need to mock this $httpBackend call
$httpBackend.whenGET('/Resources/wallet-options.json').respond();

$rootScope = _$rootScope_;
$compile = _$compile_;
$templateCache = _$templateCache_;
$componentController = _$componentController_;

$q = $injector.get('$q');
Wallet = $injector.get('Wallet');
let MyWallet = $injector.get('MyWallet');

MyWallet.wallet = {};
Wallet.accounts = () => [];
Wallet.getDefaultAccount = () => ({});

MyWallet.wallet.external = {
sfox: {
profile: {}
}
};
}));

it('should get trades that match the subscription', () => {
scope = getControllerScope(handlers);
expect(scope.trades).toBeDefined();
});

describe('.canCancel()', () => {
beforeEach(function () {
scope = getControllerScope(handlers);
});

it('should return true if state is "awaiting_transfer_in"', () => {
expect(scope.canCancel({id: 1, state: 'awaiting_transfer_in'})).toBe(true);
});
});

describe('.buyHandler()', () => {
beforeEach(function () {
scope = getControllerScope(handlers);
});

it('should call this.buy() with trade, frequency, endTime', () => {
scope = getControllerScope(handlers);
spyOn(scope.$ctrl, 'buy');
scope.buyHandler();
expect(scope.$ctrl.buy).toHaveBeenCalled();
});
});

describe('displayHelper()', () => {
it('should return a string with the namespace and trade state', () => {
scope = getControllerScope(handlers);
let trade = { state: 'complete' };
expect(scope.displayHelper(trade)).toBe('COINIFY.buy.complete.DISPLAY');
});
});

describe('onCancel()', () => {
it('should set the subscriptions active state', () => {
scope = getControllerScope(handlers);

let response = { isActive: false };
scope.onCancel(response);
expect(scope.$ctrl.subscription.isActive).toBe(false);
});
});

describe('cancelTrade()', () => {
it('should cancel the trade', () => {
scope = getControllerScope(handlers);
let trade = { state: 'awaiting_transfer_in', id: 10 };
let message = 'message';
scope.cancelTrade(trade, message, scope.$ctrl.subscription);
});
});

describe('cancel()', () => {
it('should cancel the trade', () => {
scope = getControllerScope(handlers);
let trade = { state: 'awaiting_transfer_in', id: 10 };
let message = 'message';
scope.cancelTrade(trade, message, scope.$ctrl.subscription);
});
});
});
70 changes: 70 additions & 0 deletions tests/components/exchange/exchange-recurring_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
describe('exchange-recurring.component', () => {
let $rootScope;
let $compile;
let $templateCache;
let $componentController;
let scope;
let Wallet;

let getTrades = () => [
{
id: 1,
amount: 100,
subscriptionId: 1,
fiatAmount: 75
},
{
id: 2,
amount: 200,
subscriptionId: 2,
fiatAmount: 275
}
];

let handlers = {
buy () { return true; },
subscription: 1,
partnerService: 'coinify',
frequency: 'weekly',
recurringBuyLimit () { return 100; },
trade () { return getTrades()[0]; }
};

let getControllerScope = function (bindings) {
scope = $rootScope.$new(true);
$componentController('exchangeRecurring', {$scope: scope}, bindings);
let template = $templateCache.get('templates/exchange/recurring.pug');
$compile(template)(scope);
return scope;
};

beforeEach(module('walletApp'));
beforeEach(() =>
angular.mock.inject(function ($injector, _$rootScope_, _$compile_, _$templateCache_, _$componentController_, $httpBackend) {
// TODO: use Wallet mock, so we don't need to mock this $httpBackend call
$httpBackend.whenGET('/Resources/wallet-options.json').respond();

$rootScope = _$rootScope_;
$compile = _$compile_;
$templateCache = _$templateCache_;
$componentController = _$componentController_;

Wallet = $injector.get('Wallet');
let MyWallet = $injector.get('MyWallet');

MyWallet.wallet = {};
Wallet.accounts = () => [];
Wallet.getDefaultAccount = () => ({});

MyWallet.wallet.external = {
sfox: {
profile: {}
}
};
}));

it('should set fiatAmount', () => {
scope = getControllerScope(handlers);
expect(scope.$ctrl.trade().fiatAmount).toBe(75);
});
});
Loading

0 comments on commit 2f3dbe8

Please sign in to comment.