Skip to content

Commit

Permalink
💚Add tests for tag sorting
Browse files Browse the repository at this point in the history
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_
  • Loading branch information
Brad van der Laan committed Jun 11, 2018
1 parent a3dc62b commit 637e716
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 13 deletions.
23 changes: 10 additions & 13 deletions app/tag/tag-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@
* # TagController
* Controller of the docker-registry-frontend
*/
angular.module('tag-controller', ['registry-services'])
.controller('TagController', ['$scope', '$route', '$routeParams', '$location', '$filter', 'Manifest', 'Tag', 'AppMode', 'filterFilter', '$modal',
function($scope, $route, $routeParams, $location, $filter, Manifest, Tag, AppMode, filterFilter, $modal){
angular.module('tag-controller', ['ui.bootstrap', 'registry-services', 'app-mode-services'])
.controller('TagController', ['$scope', '$route', '$location', '$filter', 'Manifest', 'Tag', 'AppMode', 'filterFilter', '$modal',
function($scope, $route, $location, $filter, Manifest, Tag, AppMode, filterFilter, $modal){

$scope.$route = $route;
$scope.$location = $location;
$scope.$routeParams = $routeParams;

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

// Fetch tags
$scope.tags = Tag.query({
Tag.query({
repoUser: $scope.repositoryUser,
repoName: $scope.repositoryName
}, function(result){
}).$promise.then(function(result){
$scope.tags = result;

// Determine the number of pages
$scope.maxTagsPage = parseInt(Math.ceil(parseFloat(result.length)/parseFloat($scope.tagsPerPage)));
// Compute the right current page number
Expand All @@ -52,7 +53,9 @@ angular.module('tag-controller', ['registry-services'])
}
// Select wanted tags
var idxShift = 0;
$scope.displayedTags = $scope.tags;
// Copy collection for rendering in a smart-table
$scope.displayedTags = [].concat($scope.tags);

if($scope.tagsPerPage){
idxShift = ($scope.tagsCurrentPage - 1) * $scope.tagsPerPage;
$scope.displayedTags = $scope.displayedTags.slice(idxShift, ($scope.tagsCurrentPage ) * $scope.tagsPerPage );
Expand All @@ -69,12 +72,6 @@ angular.module('tag-controller', ['registry-services'])
}
});



// Copy collection for rendering in a smart-table
$scope.displayedTags = [].concat($scope.tags);


// selected tags
$scope.selection = [];

Expand Down
80 changes: 80 additions & 0 deletions app/tag/tag-controller.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
'use strict';

function buildRoute() {
return {
current: {
params: {
repositoryName: 'nginx',
tagName: 'latest',
tagsPerPage: 10,
},
},
};
}

function mockTagService($q) {
var mockTagReturnValue = [];
var mockTag = { query: null };
spyOn(mockTag, 'query').and.returnValue({$promise: $q.when(mockTagReturnValue)});

return mockTag;
}

function mockAppMode($httpBackend) {
var expectedAppMode = {"browseOnly": true, "defaultRepositoriesPerPage": 20, "defaultTagsPerPage": 10};
$httpBackend.expectGET('app-mode.json').respond(expectedAppMode);
}

describe('TagController', function() {

// load the controller's module
beforeEach(module('tag-controller'));

var $controller, $httpBackend, $q, $rootScope;

beforeEach(inject(function(_$controller_, _$httpBackend_, _$q_, _$rootScope_) {
$controller = _$controller_;
$httpBackend = _$httpBackend_;
$q = _$q_;
$rootScope = _$rootScope_;
}));

describe('Sorting', function() {
it('should sort tags Ascending', function() {
var $scope = $rootScope.$new();
var route = buildRoute();
var mockTag = mockTagService($q);
mockAppMode($httpBackend);

var ctrl = $controller('TagController', {$scope: $scope, $route: route, Tag: mockTag});
// $httpBackend.flush();
$scope.displayedTags = [{
name: 'aaa',
details: {
created: '2015-03-25',
},
}, {
name: 'bbb',
details: {
created: '2015-03-20',
},
}];
expect($scope.orderByCreated).toBeTruthy();

$scope.sortTags();
expect($scope.displayedTags).toEqual([{
name: 'bbb',
details: {
created: '2015-03-20',
},
}, {
name: 'aaa',
details: {
created: '2015-03-25',
},
}]);
expect($scope.orderByCreated).toBeFalsy();
});
});
});

0 comments on commit 637e716

Please sign in to comment.