Skip to content

Commit 3b1684a

Browse files
committed
Adding ControllerApp unit tests
Fixing delete tests to reflect changes to it's state.go transition Updating other tests
1 parent 3e25107 commit 3b1684a

7 files changed

Lines changed: 180 additions & 63 deletions

File tree

client/controllers/abstractLayouts/controllerInstanceLayout.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ function ControllerInstanceLayout(
5959
};
6060
dataInstanceLayout.data.logoutURL = configLogoutURL();
6161

62-
var instanceListUnwatcher = $scope.$on('INSTANCE_LIST_FETCH', function(event, org) {
63-
fetchInstances(org.oauthName());
62+
var instanceListUnwatcher = $scope.$on('INSTANCE_LIST_FETCH', function(event, username) {
63+
fetchInstances(username);
6464
});
6565

6666
$scope.$on('$destroy', function () {

client/controllers/controllerApp.js

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,17 @@ require('app')
77
* @ngInject
88
*/
99
function ControllerApp(
10-
$log,
1110
$scope,
12-
$state,
13-
$stateParams,
1411
$rootScope,
1512
$window,
16-
async,
1713
configAPIHost,
1814
configEnvironment,
1915
configLoginURL,
2016
configLogoutURL,
2117
errs,
2218
fetchUser,
2319
fetchOrgs,
24-
hasKeypaths,
25-
keypather,
26-
QueryAssist,
27-
user
20+
keypather
2821
) {
2922

3023
var dataApp = $rootScope.dataApp = $scope.dataApp = {
@@ -45,29 +38,28 @@ function ControllerApp(
4538
data: {},
4639
actions: {}
4740
};
48-
function setActiveAccount() {
41+
function setActiveAccount(accountName) {
4942
$scope.$watch('dataApp.data.orgs', function(n) {
5043
if (n) {
51-
$rootScope.dataApp.data.instances = null;
44+
dataApp.data.instances = null;
5245
var accounts = [thisUser].concat(n.models);
5346
dataApp.data.activeAccount = accounts.find(function (org) {
54-
return (keypather.get(org, 'oauthName().toLowerCase()') ===
55-
keypather.get($stateParams, 'userName.toLowerCase()'));
47+
return (keypather.get(org, 'oauthName().toLowerCase()') === accountName.toLowerCase());
5648
});
5749
if (!dataApp.data.activeAccount) {
5850
dataApp.data.activeAccount = thisUser;
5951
}
60-
$scope.$broadcast('INSTANCE_LIST_FETCH', dataApp.data.activeAccount);
52+
$rootScope.$broadcast('INSTANCE_LIST_FETCH', dataApp.data.activeAccount.oauthName());
6153
$rootScope.safeApply();
6254
}
6355
});
6456
}
6557
// shows spinner overlay
6658
dataApp.data.loading = false;
6759
$scope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams, error) {
68-
if (!keypather.get($rootScope.dataApp, 'data.activeAccount.oauthName()') ||
60+
if (!keypather.get(dataApp, 'data.activeAccount.oauthName()') ||
6961
toParams.userName !== dataApp.data.activeAccount.oauthName()) {
70-
setActiveAccount();
62+
setActiveAccount(toParams.userName);
7163
}
7264
dataApp.data.loading = false;
7365
});

client/directives/accountsSelect/directiveAccountsSelect.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function accountsSelect (
3333
var username = userOrOrg.oauthName();
3434
//
3535
$scope.data.activeAccount = userOrOrg;
36-
$scope.$emit('INSTANCE_LIST_FETCH', userOrOrg);
36+
$scope.$emit('INSTANCE_LIST_FETCH', username);
3737
$state.go('^.instance', {
3838
userName: username,
3939
instanceName: ''

client/services/serviceHelperInstanceActionsModal.js

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ function HelperInstanceActionsModal(
137137
forkInstance: function (items, cb) {
138138
$scope.popoverGearMenu.data.show = false;
139139
$rootScope.dataApp.data.loading = true;
140-
function fork (instance, opts, cb) {
140+
function fork(instance, opts, cb) {
141141
instance.copy(opts, function (err) {
142142
if (err) { throw err; }
143143
$rootScope.safeApply();
@@ -157,6 +157,7 @@ function HelperInstanceActionsModal(
157157
userName: $stateParams.userName,
158158
instanceName: keypather.get(items[0], 'opts.name')
159159
});
160+
$scope.$emit('INSTANCE_LIST_FETCH', $stateParams.userName);
160161
if (cb) {
161162
cb();
162163
}
@@ -175,21 +176,13 @@ function HelperInstanceActionsModal(
175176
var deletedInstanceName = data.instance.attrs.name;
176177
data.instance.destroy(function (err) {
177178
$rootScope.safeApply();
178-
if (err) { throw err; }
179-
// redirect to next instance or new
180-
if (data.instances.models.length) {
181-
data.instances.models = $filter('orderBy')(data.instances.models, 'attrs.name');
182-
// Only change the location if we're still on the page
183-
// If the user switched to a different instance in between, we shouldn't move
184-
if ($stateParams.instanceName === deletedInstanceName) {
185-
$state.go('instance.instance', {
186-
userName: $stateParams.userName,
187-
instanceName: data.instances.models[0].attrs.name
188-
});
189-
}
190-
} else {
191-
$state.go('instance.new', {
192-
userName: $stateParams.userName
179+
if (err) {
180+
throw err;
181+
}
182+
if ($stateParams.instanceName === deletedInstanceName) {
183+
$state.go('instance.instance', {
184+
userName: $stateParams.userName,
185+
instanceName: ''
193186
});
194187
}
195188
});
Lines changed: 147 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,170 @@
11
var $controller,
22
$rootScope,
3+
$timeout,
34
$scope;
5+
var keypather;
6+
var apiMocks = require('../apiMocks/index');
47

58
describe('controllerApp'.bold.underline.blue, function () {
6-
beforeEach(angular.mock.module('app'));
7-
8-
beforeEach(function () {
9+
var ctx = {};
10+
function setup(stateParams) {
11+
angular.mock.module('app');
12+
ctx.fakeuser = {
13+
attrs: angular.copy(apiMocks.user),
14+
oauthName: function () {
15+
return 'user';
16+
}
17+
};
18+
ctx.fakeOrg1 = {
19+
attrs: angular.copy(apiMocks.user),
20+
oauthName: function () {
21+
return 'org1';
22+
}
23+
};
24+
ctx.fakeOrg2 = {
25+
attrs: angular.copy(apiMocks.user),
26+
oauthName: function () {
27+
return 'org2';
28+
}
29+
};
30+
var fetchUserMock = function (cb) {
31+
cb(null, ctx.fakeuser);
32+
};
33+
var fetchOrgsMock = function(cb) {
34+
cb(null, {models: [ctx.fakeOrg1, ctx.fakeOrg2]});
35+
};
36+
ctx.stateParams = stateParams || {
37+
userName: 'username',
38+
instanceName: 'instancename'
39+
};
40+
angular.mock.module('app', function ($provide) {
41+
$provide.value('fetchUser', fetchUserMock);
42+
$provide.value('fetchOrgs', fetchOrgsMock);
43+
$provide.value('$stateParams', ctx.stateParams);
44+
});
945
angular.mock.inject(function (
1046
_$controller_,
11-
_$rootScope_
47+
_$rootScope_,
48+
_$timeout_,
49+
_keypather_
1250
) {
1351
$controller = _$controller_;
1452
$rootScope = _$rootScope_;
1553
$scope = $rootScope.$new();
16-
});
17-
});
54+
$timeout = _$timeout_;
55+
keypather = _keypather_;
1856

19-
it('initalizes $scope.dataApp properly', function() {
57+
$rootScope.safeApply = function(cb) {
58+
$timeout(function() {
59+
$scope.$digest();
60+
});
61+
};
62+
});
2063
var ca = $controller('ControllerApp', {
2164
'$scope': $scope
2265
});
66+
}
67+
describe('basics'.blue, function () {
68+
beforeEach(function () {
69+
setup();
70+
});
71+
it('initalizes $scope.dataApp properly', function () {
2372

24-
expect($scope.dataApp).to.be.an.Object;
25-
$rootScope.$digest();
26-
});
27-
28-
it('creates a click handler that broadcasts', function() {
29-
var clicked;
30-
var ca = $controller('ControllerApp', {
31-
'$scope': $scope
73+
expect($scope.dataApp).to.be.an.Object;
74+
$rootScope.$digest();
3275
});
3376

34-
$rootScope.$digest();
77+
it('creates a click handler that broadcasts', function () {
78+
var clicked;
79+
$rootScope.$digest();
3580

36-
$scope.$on('app-document-click', function() {
37-
clicked = true;
38-
});
81+
$scope.$on('app-document-click', function () {
82+
clicked = true;
83+
});
3984

40-
$scope.dataApp.documentClickEventHandler();
85+
$scope.dataApp.documentClickEventHandler();
86+
87+
expect(clicked).to.be.true;
88+
});
89+
});
4190

42-
expect(clicked).to.be.true;
91+
describe('account stuff'.blue, function () {
92+
describe('No account already chosen'.blue, function () {
93+
it('should select user if nothing matches name in url ', function (done) {
94+
setup({});
95+
var listFetchSpy = sinon.spy(function(event, name) {
96+
expect(name).to.equal(ctx.fakeuser.oauthName());
97+
expect($scope.dataApp.data.activeAccount).to.be.an.Object;
98+
expect($scope.dataApp.data.activeAccount).to.equal(ctx.fakeuser);
99+
done();
100+
});
101+
$scope.$on('INSTANCE_LIST_FETCH', listFetchSpy);
102+
$rootScope.$digest();
103+
$rootScope.$broadcast('$stateChangeStart', null, {
104+
userName: 'username'
105+
});
106+
$rootScope.$digest();
107+
});
108+
it('should select user, matching it from the stateParams ', function (done) {
109+
setup({});
110+
var listFetchSpy = sinon.spy(function(event, name) {
111+
expect(name).to.equal(ctx.fakeuser.oauthName());
112+
expect($scope.dataApp.data.activeAccount).to.be.an.Object;
113+
expect($scope.dataApp.data.activeAccount).to.equal(ctx.fakeuser);
114+
done();
115+
});
116+
$scope.$on('INSTANCE_LIST_FETCH', listFetchSpy);
117+
$rootScope.$digest();
118+
$rootScope.$broadcast('$stateChangeStart', null, {
119+
userName: ctx.fakeuser.oauthName()
120+
});
121+
$rootScope.$digest();
122+
});
123+
it('should select org1, matching it from the stateParams ', function (done) {
124+
setup({});
125+
var listFetchSpy = sinon.spy(function(event, name) {
126+
expect(name).to.equal(ctx.fakeOrg1.oauthName());
127+
expect($scope.dataApp.data.activeAccount).to.be.an.Object;
128+
expect($scope.dataApp.data.activeAccount).to.equal(ctx.fakeOrg1);
129+
done();
130+
});
131+
$scope.$on('INSTANCE_LIST_FETCH', listFetchSpy);
132+
$rootScope.$digest();
133+
$rootScope.$broadcast('$stateChangeStart', null, {
134+
userName: ctx.fakeOrg1.oauthName()
135+
});
136+
$rootScope.$digest();
137+
});
138+
});
139+
it('should not switch accounts if active account matches', function () {
140+
setup({});
141+
var listFetchSpy = sinon.spy();
142+
keypather.set($scope, 'dataApp.data.activeAccount', ctx.fakeOrg1);
143+
$scope.$on('INSTANCE_LIST_FETCH', listFetchSpy);
144+
$rootScope.$digest();
145+
$rootScope.$broadcast('$stateChangeStart', null, {
146+
userName: ctx.fakeOrg1.oauthName()
147+
});
148+
$rootScope.$digest();
149+
expect($scope.dataApp.data.activeAccount).to.be.an.Object;
150+
expect($scope.dataApp.data.activeAccount).to.equal(ctx.fakeOrg1);
151+
sinon.assert.notCalled(listFetchSpy);
152+
});
153+
it('should switch accounts if active account does not match url', function (done) {
154+
setup({});
155+
var listFetchSpy = sinon.spy(function(event, name) {
156+
expect(name).to.equal(ctx.fakeOrg2.oauthName());
157+
expect($scope.dataApp.data.activeAccount).to.be.an.Object;
158+
expect($scope.dataApp.data.activeAccount).to.equal(ctx.fakeOrg2);
159+
done();
160+
});
161+
keypather.set($scope, 'dataApp.data.activeAccount', ctx.fakeOrg1);
162+
$scope.$on('INSTANCE_LIST_FETCH', listFetchSpy);
163+
$rootScope.$digest();
164+
$rootScope.$broadcast('$stateChangeStart', null, {
165+
userName: ctx.fakeOrg2.oauthName()
166+
});
167+
$rootScope.$digest();
168+
});
43169
});
44170
});

test/unit/directives/setup/directiveSetupPrimaryActions.unit.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var $rootScope,
55
$state,
66
$stateParams;
77
var $elScope;
8-
8+
var keypather;
99
var apiMocks = require('../../apiMocks/index');
1010

1111
function makeDefaultScope() {
@@ -58,13 +58,15 @@ describe('directiveSetupPrimaryActions'.bold.underline.blue, function () {
5858
_$stateParams_,
5959
_$rootScope_,
6060
_$timeout_,
61-
_$compile_
61+
_$compile_,
62+
_keypather_
6263
) {
6364
$state = _$state_;
6465
$stateParams = _$stateParams_;
6566
$rootScope = _$rootScope_;
6667
$compile = _$compile_;
6768
$scope = _$rootScope_.$new();
69+
keypather = _keypather_;
6870
$rootScope.safeApply = function (cb) {
6971
_$timeout_(function () {
7072
$scope.$digest();
@@ -76,6 +78,9 @@ describe('directiveSetupPrimaryActions'.bold.underline.blue, function () {
7678
$scope[key] = scope[key];
7779
});
7880
}
81+
keypather.set($rootScope, 'dataApp.data.instances.create', sinon.spy(function (opts, cb) {
82+
cb();
83+
}));
7984

8085
ctx = {};
8186
ctx.template = directiveTemplate('setup-primary-actions', {
@@ -154,7 +159,7 @@ describe('directiveSetupPrimaryActions'.bold.underline.blue, function () {
154159
});
155160

156161
it('should build if openItems is clean', function (done) {
157-
$scope.data.user.createInstance = function (opts, cb) {
162+
keypather.set($rootScope, 'dataApp.data.instances.create', sinon.spy(function (opts, cb) {
158163
console.log(opts);
159164
expect(opts.owner).to.deep.equal({
160165
github: apiMocks.user.accounts.github.accessToken
@@ -165,7 +170,7 @@ describe('directiveSetupPrimaryActions'.bold.underline.blue, function () {
165170
var copy = angular.copy(apiMocks.instances.building);
166171
copy.name = $scope.name;
167172
return { attrs: copy };
168-
};
173+
}));
169174

170175
ctx.stateMock.go = function (newState, params) {
171176
expect(newState).to.equal('instance.instance');

0 commit comments

Comments
 (0)