From 637e71613cdb6449c9f9da8ea775f522b7804ed5 Mon Sep 17 00:00:00 2001 From: Brad van der Laan Date: Sun, 10 Jun 2018 21:41:20 -0400 Subject: [PATCH] :green_heart: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_ --- app/tag/tag-controller.js | 23 +++++----- app/tag/tag-controller.spec.js | 80 ++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 13 deletions(-) create mode 100644 app/tag/tag-controller.spec.js diff --git a/app/tag/tag-controller.js b/app/tag/tag-controller.js index 3163e75..26bcc73 100644 --- a/app/tag/tag-controller.js +++ b/app/tag/tag-controller.js @@ -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; @@ -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 @@ -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 ); @@ -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 = []; diff --git a/app/tag/tag-controller.spec.js b/app/tag/tag-controller.spec.js new file mode 100644 index 0000000..9f0ec9d --- /dev/null +++ b/app/tag/tag-controller.spec.js @@ -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(); + }); + }); +}); +