Skip to content

Commit 637e716

Browse files
author
Brad van der Laan
committed
💚Add tests for tag sorting
This change set adds tests for the new tag sorting by date feature. It changed the syntax for the query of tags a bit to use the .then() api vs. passing the handler function as a second parameter to the query() api as that just made writing the test easier and more _promisy_
1 parent a3dc62b commit 637e716

File tree

2 files changed

+90
-13
lines changed

2 files changed

+90
-13
lines changed

app/tag/tag-controller.js

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@
77
* # TagController
88
* Controller of the docker-registry-frontend
99
*/
10-
angular.module('tag-controller', ['registry-services'])
11-
.controller('TagController', ['$scope', '$route', '$routeParams', '$location', '$filter', 'Manifest', 'Tag', 'AppMode', 'filterFilter', '$modal',
12-
function($scope, $route, $routeParams, $location, $filter, Manifest, Tag, AppMode, filterFilter, $modal){
10+
angular.module('tag-controller', ['ui.bootstrap', 'registry-services', 'app-mode-services'])
11+
.controller('TagController', ['$scope', '$route', '$location', '$filter', 'Manifest', 'Tag', 'AppMode', 'filterFilter', '$modal',
12+
function($scope, $route, $location, $filter, Manifest, Tag, AppMode, filterFilter, $modal){
1313

1414
$scope.$route = $route;
1515
$scope.$location = $location;
16-
$scope.$routeParams = $routeParams;
1716

1817
$scope.searchName = $route.current.params.searchName;
1918
$scope.repositoryUser = $route.current.params.repositoryUser;
@@ -34,10 +33,12 @@ angular.module('tag-controller', ['registry-services'])
3433
});
3534

3635
// Fetch tags
37-
$scope.tags = Tag.query({
36+
Tag.query({
3837
repoUser: $scope.repositoryUser,
3938
repoName: $scope.repositoryName
40-
}, function(result){
39+
}).$promise.then(function(result){
40+
$scope.tags = result;
41+
4142
// Determine the number of pages
4243
$scope.maxTagsPage = parseInt(Math.ceil(parseFloat(result.length)/parseFloat($scope.tagsPerPage)));
4344
// Compute the right current page number
@@ -52,7 +53,9 @@ angular.module('tag-controller', ['registry-services'])
5253
}
5354
// Select wanted tags
5455
var idxShift = 0;
55-
$scope.displayedTags = $scope.tags;
56+
// Copy collection for rendering in a smart-table
57+
$scope.displayedTags = [].concat($scope.tags);
58+
5659
if($scope.tagsPerPage){
5760
idxShift = ($scope.tagsCurrentPage - 1) * $scope.tagsPerPage;
5861
$scope.displayedTags = $scope.displayedTags.slice(idxShift, ($scope.tagsCurrentPage ) * $scope.tagsPerPage );
@@ -69,12 +72,6 @@ angular.module('tag-controller', ['registry-services'])
6972
}
7073
});
7174

72-
73-
74-
// Copy collection for rendering in a smart-table
75-
$scope.displayedTags = [].concat($scope.tags);
76-
77-
7875
// selected tags
7976
$scope.selection = [];
8077

app/tag/tag-controller.spec.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
'use strict';
2+
3+
function buildRoute() {
4+
return {
5+
current: {
6+
params: {
7+
repositoryName: 'nginx',
8+
tagName: 'latest',
9+
tagsPerPage: 10,
10+
},
11+
},
12+
};
13+
}
14+
15+
function mockTagService($q) {
16+
var mockTagReturnValue = [];
17+
var mockTag = { query: null };
18+
spyOn(mockTag, 'query').and.returnValue({$promise: $q.when(mockTagReturnValue)});
19+
20+
return mockTag;
21+
}
22+
23+
function mockAppMode($httpBackend) {
24+
var expectedAppMode = {"browseOnly": true, "defaultRepositoriesPerPage": 20, "defaultTagsPerPage": 10};
25+
$httpBackend.expectGET('app-mode.json').respond(expectedAppMode);
26+
}
27+
28+
describe('TagController', function() {
29+
30+
// load the controller's module
31+
beforeEach(module('tag-controller'));
32+
33+
var $controller, $httpBackend, $q, $rootScope;
34+
35+
beforeEach(inject(function(_$controller_, _$httpBackend_, _$q_, _$rootScope_) {
36+
$controller = _$controller_;
37+
$httpBackend = _$httpBackend_;
38+
$q = _$q_;
39+
$rootScope = _$rootScope_;
40+
}));
41+
42+
describe('Sorting', function() {
43+
it('should sort tags Ascending', function() {
44+
var $scope = $rootScope.$new();
45+
var route = buildRoute();
46+
var mockTag = mockTagService($q);
47+
mockAppMode($httpBackend);
48+
49+
var ctrl = $controller('TagController', {$scope: $scope, $route: route, Tag: mockTag});
50+
// $httpBackend.flush();
51+
$scope.displayedTags = [{
52+
name: 'aaa',
53+
details: {
54+
created: '2015-03-25',
55+
},
56+
}, {
57+
name: 'bbb',
58+
details: {
59+
created: '2015-03-20',
60+
},
61+
}];
62+
expect($scope.orderByCreated).toBeTruthy();
63+
64+
$scope.sortTags();
65+
expect($scope.displayedTags).toEqual([{
66+
name: 'bbb',
67+
details: {
68+
created: '2015-03-20',
69+
},
70+
}, {
71+
name: 'aaa',
72+
details: {
73+
created: '2015-03-25',
74+
},
75+
}]);
76+
expect($scope.orderByCreated).toBeFalsy();
77+
});
78+
});
79+
});
80+

0 commit comments

Comments
 (0)