Skip to content
This repository has been archived by the owner on Jul 1, 2024. It is now read-only.

Test coverage for Gateways Controllers #1167

Merged
merged 40 commits into from
Mar 2, 2015
Merged
Show file tree
Hide file tree
Changes from 31 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
b6162b5
added test files
gterzian Feb 21, 2015
57e4337
added test files to conf, first test passing
gterzian Feb 21, 2015
13af3c2
mocking out gateway service
gterzian Feb 21, 2015
b8dd0b6
removing console.log
gterzian Feb 21, 2015
651752c
test for no gateway found
gterzian Feb 21, 2015
03df8ed
tests for GatewayListCtrl
gterzian Feb 21, 2015
3040f40
added an else for clarity
gterzian Feb 21, 2015
73e271b
putting service mocks into their own files
gterzian Feb 21, 2015
cdc8ab1
separate modules for each mock service
gterzian Feb 21, 2015
75f5a23
renamed file for consistency
gterzian Feb 21, 2015
859318a
add files to conf
gterzian Feb 21, 2015
4774ae0
excluding underscore
gterzian Feb 21, 2015
ed2e06f
adding removing gateway to mocks
gterzian Feb 21, 2015
cbcf38f
remove log statement
gterzian Feb 21, 2015
f5e718b
mocking out the stellar network
gterzian Feb 21, 2015
fd0d4dd
mocking out some functionalities of the network
gterzian Feb 21, 2015
4dcc25d
align stuff
gterzian Feb 21, 2015
2b352fa
tests for remove gateway
gterzian Feb 21, 2015
066668e
remove redundant mockBackend
gterzian Feb 21, 2015
38f56aa
no returning promise anymore
gterzian Feb 21, 2015
831a9d2
better writing
gterzian Feb 21, 2015
77c7229
comments
gterzian Feb 21, 2015
80565e5
removing whitespace
gterzian Feb 21, 2015
7e5307b
we're only mocking out search function of gateways service
gterzian Feb 21, 2015
49ab28a
better english
gterzian Feb 21, 2015
b0832d9
removing timeout dep
gterzian Feb 21, 2015
7038c7c
extra assertion
gterzian Feb 21, 2015
1293166
added comment on gateway status when removing, test for adding
gterzian Feb 22, 2015
b4dfa8b
test for retry adding
gterzian Feb 22, 2015
feb50a7
added a change to the gateway service to set status of gateway in ses…
gterzian Feb 22, 2015
cc5ed43
test for cancel adding a gateway
gterzian Feb 22, 2015
d5f2c06
keeping things compact
gterzian Feb 22, 2015
cdc1453
test for currencyNames, updated mocks
gterzian Feb 23, 2015
c7b50c5
test for adding non existing gateway
gterzian Feb 23, 2015
7c1eb48
moving the add gateway stuff into a separate suite for more targetted…
gterzian Feb 23, 2015
049be10
comments and update of mocks
gterzian Feb 23, 2015
a0ad7fc
removed the un-necessary timeout
gterzian Feb 23, 2015
716c08b
better suite description
gterzian Feb 23, 2015
5f1354c
checking that alerts were shown
gterzian Feb 23, 2015
52ab668
fix alignment
gterzian Feb 23, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/scripts/services/gateways-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ sc.service('Gateways', function($q, $analytics, session, StellarNetwork, rpStell

Gateways.remove = function(gateway) {
gateway.status = "removing";
walletGateways()[gateway.domain].status = "removing";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this just matches what goes on in the 'add' method. Without this the tests would not pass (unless I would beforehand set the status of the gateway in the mocked out session to 'removing')

return Gateways.syncTrustlines(gateway.domain).then(function() {
if(!_.has(walletGateways(), gateway.domain)) {
track(gateway, 'Gateway Removed');
Expand Down
16 changes: 13 additions & 3 deletions test/unit/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,26 @@ module.exports = function(config) {
'app/scripts/**/*.js',

//test-only mocks
'test/unit/mocks/service-mocks.js',
'test/unit/mocks/gateway-mocks.js',
'test/unit/mocks/session-mocks.js',
'test/unit/mocks/stellar-network-mocks.js',

//specs
'test/unit/specs/controllers/add-email-controller-test.js'
'test/unit/specs/controllers/add-email-controller-test.js',

//gateways
'test/unit/specs/controllers/add-gateway-controller-test.js',
'test/unit/specs/controllers/gateway-list-controller-test.js',
'test/unit/specs/controllers/gateway-list-item-controller-test.js'

],


// list of files / patterns to exclude
exclude: ['app/bower_components/URIjs/**/**.js',],//couldn't get this URI to work server side
exclude: [
'app/bower_components/URIjs/**/**.js',//couldn't get this URI to work server side
'app/scripts/libraries/underscore.js'//Why do we have underscore in there?
],

// web server port
port: 8080,
Expand Down
25 changes: 25 additions & 0 deletions test/unit/mocks/gateway-mocks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

angular.module('mockGateway', [])
.factory('Gateways', ['$q', function($q) {
var gateway = {
search: function (domain) {
var found = {
domain: domain,
currencies: ['usd', 'cny']
};
var deferred = $q.defer();
setTimeout(function() {
if (domain === 'failing-gateway') {
deferred.reject(found);
}
else {
deferred.resolve(found);
};
}, 10);
return deferred.promise;
},
};
return gateway
}]);

7 changes: 0 additions & 7 deletions test/unit/mocks/service-mocks.js

This file was deleted.

24 changes: 24 additions & 0 deletions test/unit/mocks/session-mocks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict';

angular.module('mockSession', [])
.factory('session', [function() {
var session_data = {
keychainData : {
updateToken: true
},
mainData: {
email: '',
gateways: {
'test-gateway': {
domain: 'test-gateway',
currencies: ['usd', 'cny']
},
'removing-gateway': {
domain: 'removing-gateway',
currencies: ['usd', 'cny'],
}
}
}
};
return {get: function () {return session_data}, syncWallet: function () {}}
}]);
21 changes: 21 additions & 0 deletions test/unit/mocks/stellar-network-mocks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

angular.module('mockStellarNetwork', [])
.factory('StellarNetwork', [function() {
var transaction = {
trustSet: function () {},
setFlags: function () {},
submit: function () {},
on: function (event, callback) {
if (event === 'success'){
callback('ok')
};
}
}
var network = {
remote: {
transaction: function () {return transaction}
}
};
return network
}]);
4 changes: 3 additions & 1 deletion test/unit/specs/controllers/add-email-controller-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ describe('Controller: AddEmailCtrl', function () {

// load the controller's module
beforeEach(module('stellarClient'));

//load the mocks for services
beforeEach(module('serviceMocks'));
beforeEach(module('mockSession'));
beforeEach(module('mockGateway'));


var AddEmailCtrl, scope, mockBackend;
Expand Down
61 changes: 61 additions & 0 deletions test/unit/specs/controllers/add-gateway-controller-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
'use strict';

describe('Controller: AddGatewayCtrl, mocking out search', function () {

// load the controller's module
beforeEach(module('stellarClient'));

//load the mocks for services
beforeEach(module('mockSession'));
beforeEach(module('mockGateway'));

var AddGatewayCtrl, scope;

// Initialize the controller and a mock scope
beforeEach(inject(function ($controller, $rootScope) {
scope = $rootScope.$new();
AddGatewayCtrl = $controller('AddGatewayCtrl', {
$scope: scope
});
}));

it('Initially, the search params should always be reset', function () {
expect(scope.gatewaySearch).to.equal('');
expect(scope.searchStatus).to.equal('');
expect(scope.foundGateway).to.equal(null);
expect(scope.fromActionLink).to.equal(false);
});

it('If an empty search is triggered, nothing should happen', function () {
expect(scope.gatewaySearch).to.equal('');
scope.loadCurrencies();
expect(scope.foundGateway).to.equal(null);
});

it('When searching for a gateway that had already been added, the searchStatus should reflect that', function () {
scope.gateways = {'test-gateway': true};
scope.gatewaySearch = 'test-gateway'
scope.loadCurrencies();
expect(scope.searchStatus).to.equal('already_added');
});

it('If a gateway is found, the result should be added to foundGateway', function () {
scope.gateways = {};
scope.gatewaySearch = 'test-gateway'
var promise = scope.loadCurrencies();
promise.then(function (){
expect(scope.foundGateway).to.equal({domain: 'test gateway', curencies: ['usd', 'cny']});
expect(scope.searchStatus).to.equal('found');
});
});

it('If no gateway is found, the search status should resolve to not_found', function () {
scope.gateways = {};
scope.gatewaySearch = 'failing-gateway'
var promise = scope.loadCurrencies();
promise.then(function (){
expect(scope.foundGateway).to.equal(null);
expect(scope.searchStatus).to.equal('not_found');
});
});
});
32 changes: 32 additions & 0 deletions test/unit/specs/controllers/gateway-list-controller-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';

describe('Controller: GatewayListCtrl', function () {

// load the controller's module
beforeEach(module('stellarClient'));

//load the mocks for services
beforeEach(module('mockSession'));
beforeEach(module('mockGateway'));

var GatewayListCtrl, scope;

// Initialize the controller and a mock scope
beforeEach(inject(function ($controller, $rootScope) {
scope = $rootScope.$new();
GatewayListCtrl = $controller('GatewayListCtrl', {
$scope: scope
});
}));

it('If there are any gateways, hasGateways should resolve to true', function () {
scope.gateways = ['one_gateway', 'two_gateway']
expect(scope.hasGateways()).to.equal(true);
});

it("If there aren't any gateways, hasGateways should resolve to false", function () {
scope.gateways = []
expect(scope.hasGateways()).to.equal(false);
});

});
61 changes: 61 additions & 0 deletions test/unit/specs/controllers/gateway-list-item-controller-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
'use strict';

describe('Controller: GatewayListItemCtrl', function () {

// load the controller's module
beforeEach(module('stellarClient'));

//load the mocks for services
beforeEach(module('mockSession'));
beforeEach(module('mockStellarNetwork'));

var GatewayListItemCtrl, scope, rootScope, inner_session;

// Initialize the controller and a mock scope
beforeEach(inject(function ($controller, $rootScope, session) {
rootScope = $rootScope;
inner_session = session;
scope = $rootScope.$new();
GatewayListItemCtrl = $controller('GatewayListItemCtrl', {
$scope: scope
});
}));

it('You should be able to remove the current gateway', function () {
scope.gateway = {
domain: 'removing-gateway',
currencies: ['usd', 'cny'],
};
expect(inner_session.get().mainData.gateways['removing-gateway'].domain).to.equal(scope.gateway.domain);
scope.remove();
expect(scope.gateway.status).to.equal('removing')
expect(inner_session.get().mainData.gateways['removing-gateway'].status).to.equal('removing');
rootScope.$apply();
expect(inner_session.get().mainData.gateways['removing-gateway']).to.equal(undefined);
//note we're not actually removing the gateway from the scope, we're only removing it from the session
expect(scope.gateway.status).to.equal('removing')
});

it('You should be able to retry adding a gateway', function () {
scope.gateway = {
domain: 'new-gateway',
currencies: ['usd', 'cny'],
};
expect(inner_session.get().mainData.gateways['new-gateway']).to.equal(undefined);
scope.retryAdd();
expect(inner_session.get().mainData.gateways['new-gateway'].status).to.equal('adding');
rootScope.$apply();
expect(inner_session.get().mainData.gateways['new-gateway'].status).to.equal('added');
});

it('You should be able to cancel adding a gateway', function () {
scope.gateway = {
domain: 'test-gateway',
currencies: ['usd', 'cny']
};
expect(inner_session.get().mainData.gateways['test-gateway']).to.exists;
scope.cancelAdd();
expect(inner_session.get().mainData.gateways['test-gateway']).to.be.undefined;
});

});