Skip to content

Commit 1ee6342

Browse files
committed
Finishing up the lose end issues with this improvement
Fixing Tests, Creating service for fetching instances
1 parent 623f883 commit 1ee6342

7 files changed

Lines changed: 57 additions & 48 deletions

File tree

client/controllers/abstractLayouts/controllerInstanceLayout.js

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ require('app')
66
function ControllerInstanceLayout(
77
configLogoutURL,
88
fetchUser,
9+
fetchInstances,
910
$stateParams,
10-
QueryAssist,
1111
errs,
1212
$rootScope,
1313
keypather,
@@ -27,49 +27,40 @@ function ControllerInstanceLayout(
2727
return errs.handler(err);
2828
}
2929
thisUser = user;
30-
fetchInstances(
30+
resolveInstanceFetch(
3131
$stateParams.userName
3232
);
3333
});
3434

35-
function fetchInstances(account, cb) {
36-
if (!account) { return; }
37-
async.series([
35+
function resolveInstanceFetch(username) {
36+
if (!username) { return; }
37+
async.waterfall([
3838
function (cb) {
3939
$rootScope.dataApp.state.loadingInstances = true;
4040
$rootScope.dataApp.data.instances = null;
4141
$rootScope.safeApply(cb);
4242
},
4343
function (cb) {
44-
new QueryAssist(thisUser, cb)
45-
.wrapFunc('fetchInstances', cb)
46-
.query({
47-
githubUsername: account
48-
})
49-
.cacheFetch(function (instances, cached, cb) {
50-
if (account === keypather.get($rootScope, 'dataApp.data.activeAccount.oauthName()')) {
51-
if ($rootScope.dataApp.data.instances !== instances) {
52-
$rootScope.dataApp.data.instances = instances;
53-
}
54-
$rootScope.dataApp.state.loadingInstances = false;
55-
$rootScope.safeApply(cb);
56-
} else {
57-
cb();
58-
}
59-
})
60-
.resolve(function (err, projects, cb) {
61-
cb(err);
62-
})
63-
.go();
44+
fetchInstances(username, true, cb);
6445
},
65-
cb
46+
function (instances, queriedUsername, cb) {
47+
if (username === keypather.get($rootScope, 'dataApp.data.activeAccount.oauthName()')) {
48+
if ($rootScope.dataApp.data.instances !== instances) {
49+
$rootScope.dataApp.data.instances = instances;
50+
}
51+
$rootScope.dataApp.state.loadingInstances = false;
52+
$rootScope.safeApply(cb);
53+
} else {
54+
cb();
55+
}
56+
}
6657
], function (err) {
67-
if (err) { throw err; }
58+
if (err) { return errs.handler(err); }
6859
});
6960
}
7061

7162
var instanceListUnwatcher = $scope.$on('INSTANCE_LIST_FETCH', function(event, username) {
72-
fetchInstances(username);
63+
resolveInstanceFetch(username);
7364
});
7465

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

client/controllers/instance/controllerInstanceHome.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ function ControllerInstanceHome(
88
$stateParams,
99
$state,
1010
$scope,
11+
fetchInstances,
1112
$localStorage,
1213
$rootScope,
1314
keypather
@@ -16,15 +17,12 @@ function ControllerInstanceHome(
1617
var instanceName = keypather.get($localStorage, 'lastInstancePerUser.' + userName);
1718
if (!instanceName) {
1819
$scope.loading = true;
19-
var unwatch = $rootScope.$watch('dataApp.data.instances', function(n) {
20-
if (n) {
21-
unwatch();
22-
if (userName === $rootScope.dataApp.data.activeAccount.oauthName()) {
23-
$scope.loading = false;
24-
var models = $filter('orderBy')(n.models, 'attrs.name');
25-
var name = keypather.get(n, 'models[0].attrs.name');
26-
goToInstance(userName, name);
27-
}
20+
fetchInstances(userName, false, function(err, instances, account) {
21+
if (account === $rootScope.dataApp.data.activeAccount.oauthName()) {
22+
$scope.loading = false;
23+
var models = $filter('orderBy')(instances.models, 'attrs.name');
24+
var name = keypather.get(models, '[0].attrs.name');
25+
goToInstance(userName, name);
2826
}
2927
});
3028
} else {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
require('app')
2+
.factory('fetchInstances', fetchInstances);
3+
4+
function fetchInstances(
5+
fetchUser
6+
) {
7+
var currentInstances;
8+
var currentAccountName;
9+
return function (activeAccountName, forceQuery, cb) {
10+
if (activeAccountName === currentAccountName && !forceQuery) {
11+
return cb(null, currentInstances, activeAccountName);
12+
} else {
13+
currentAccountName = activeAccountName;
14+
fetchUser(function (err, user) {
15+
currentInstances = user.fetchInstances({
16+
githubUsername: currentAccountName
17+
}, function (err) {
18+
cb(err, currentInstances, activeAccountName);
19+
});
20+
});
21+
}
22+
};
23+
}

client/services/serviceHelperInstanceActionsModal.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ function HelperInstanceActionsModal(
186186
// Only change the location if we're still on the page
187187
// If the user switched to a different instance in between, we shouldn't move
188188
if ($stateParams.instanceName === deletedInstanceName) {
189-
$state.go('instance.new', {
189+
$state.go('instance.home', {
190190
userName: $stateParams.userName
191191
});
192192
}

test/unit/directives/directiveAccountsSelect.unit.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,7 @@ describe('directiveAccountsSelect'.bold.underline.blue, function() {
108108
initState();
109109
ctx.stateMock.go = sinon.spy(function (location, state) {
110110
expect(state).to.deep.equal({
111-
userName: ctx.fakeOrg1.oauthName(),
112-
instanceName: ''
111+
userName: ctx.fakeOrg1.oauthName()
113112
});
114113
done();
115114
});

test/unit/directives/directiveEditRepoCommit.unit.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ describe('directiveEditRepoCommit'.bold.underline.blue, function() {
211211
var $el = ctx.element[0]
212212
.querySelector('.commit.load > time.commit-time');
213213
expect($el).to.be.ok;
214-
expect($el.innerText).to.equal('2 months ago');
214+
expect($el.innerText).to.equal('3 months ago');
215215
});
216216

217217
});

test/unit/services/serviceHelperInstanceActionsModal.unit.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ describe('serviceHelperInstanceActionsModal'.bold.underline.blue, function() {
307307

308308
sinon.assert.called(fakeGo);
309309
sinon.assert.called($scope.instance.destroy);
310-
sinon.assert.calledWith(fakeGo,'instance.new', {
310+
sinon.assert.calledWith(fakeGo,'instance.home', {
311311
userName: 'username'
312312
});
313313
done();
@@ -324,9 +324,8 @@ describe('serviceHelperInstanceActionsModal'.bold.underline.blue, function() {
324324

325325
sinon.assert.called(fakeGo);
326326
sinon.assert.called($scope.instance.destroy);
327-
sinon.assert.calledWith(fakeGo,'instance.instance', {
328-
userName: 'username',
329-
instanceName: 'other'
327+
sinon.assert.calledWith(fakeGo,'instance.home', {
328+
userName: 'username'
330329
});
331330
done();
332331
});
@@ -359,9 +358,8 @@ describe('serviceHelperInstanceActionsModal'.bold.underline.blue, function() {
359358
setTimeout(function() {
360359
sinon.assert.notCalled(fakeGo);
361360
sinon.assert.called($scope.instance.destroy);
362-
sinon.assert.neverCalledWith(fakeGo,'instance.instance', {
363-
userName: 'username',
364-
instanceName: 'other'
361+
sinon.assert.neverCalledWith(fakeGo,'instance.home', {
362+
userName: 'username'
365363
});
366364
done();
367365
}, 50);

0 commit comments

Comments
 (0)