This repository has been archived by the owner on Oct 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1380 from blockchain/exchange-tests
Tests
- Loading branch information
Showing
14 changed files
with
1,225 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
132
tests/components/exchange/exchange-recurring-trades_spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
Oops, something went wrong.