Added tests for pagination component
diff --git a/views/ngXosLib/xosHelpers/spec/csrftoken.test.js b/views/ngXosLib/xosHelpers/spec/csrftoken.test.js
index 200014d..8901bde 100644
--- a/views/ngXosLib/xosHelpers/spec/csrftoken.test.js
+++ b/views/ngXosLib/xosHelpers/spec/csrftoken.test.js
@@ -34,6 +34,7 @@
httpBackend.when('POST', 'http://example.com', null, function(headers) {
expect(headers['X-CSRFToken']).toBe(fakeToken);
done();
+ return headers;
}).respond(200, {name: 'example' });
http.post('http://example.com');
diff --git a/views/ngXosLib/xosHelpers/spec/noHyperlinks.test.js b/views/ngXosLib/xosHelpers/spec/noHyperlinks.test.js
index a3646e3..7b6e9d0 100644
--- a/views/ngXosLib/xosHelpers/spec/noHyperlinks.test.js
+++ b/views/ngXosLib/xosHelpers/spec/noHyperlinks.test.js
@@ -12,7 +12,7 @@
describe('The xos.helper module', function(){
describe('The NoHyperlinks factory', () => {
- let httpProviderObj, httpBackend, http, noHyperlinks;
+ let httpProviderObj, noHyperlinks;
beforeEach(() => {
module(
@@ -23,9 +23,7 @@
}
);
- inject(function (_$httpBackend_, _$http_, _NoHyperlinks_) {
- httpBackend = _$httpBackend_;
- http = _$http_;
+ inject(function (_NoHyperlinks_) {
noHyperlinks = _NoHyperlinks_
});
diff --git a/views/ngXosLib/xosHelpers/spec/ui/pagination.test.js b/views/ngXosLib/xosHelpers/spec/ui/pagination.test.js
new file mode 100644
index 0000000..a8a7482
--- /dev/null
+++ b/views/ngXosLib/xosHelpers/spec/ui/pagination.test.js
@@ -0,0 +1,53 @@
+(function () {
+ 'use strict';
+
+ describe('The xos.helper module', function(){
+ describe('The xos-pagination component', () => {
+
+ let scope, element, isolatedScope;
+ let cb = jasmine.createSpy('callback')
+
+ beforeEach(module('xos.helpers'));
+
+ beforeEach(inject(function ($compile, $rootScope) {
+ scope = $rootScope.$new();
+
+ scope.pageSize = 2;
+
+ scope.totalElements = 5;
+
+ scope.change = cb;
+
+ element = angular.element('<xos-pagination page-size="pageSize" total-elements="totalElements" change="change"></xos-table>');
+ $compile(element)(scope);
+ scope.$digest();
+ isolatedScope = element.isolateScope().vm;
+ }));
+
+ it('should contain 3 pages', function() {
+ var li = element[0].getElementsByTagName('li');
+ expect(li.length).toEqual(5);
+ });
+
+ it('should call the change function', () => {
+ var li = element[0].getElementsByTagName('li')[3];
+ let link = li.getElementsByTagName('a')[0];
+ link.click();
+ expect(cb).toHaveBeenCalledWith(2);
+ });
+
+ describe('when elements number is less than page size', () => {
+ beforeEach(() => {
+ isolatedScope.pageSize = 10;
+ isolatedScope.totalElements = 9;
+ scope.$digest();
+ });
+
+ it('should not be rendered', () => {
+ var pagination = element[0].getElementsByClassName('pagination');
+ expect(pagination.length).toEqual(0);
+ });
+ });
+ });
+ });
+})();
\ No newline at end of file
diff --git a/views/ngXosLib/xosHelpers/src/ui_components/table/pagination.component.js b/views/ngXosLib/xosHelpers/src/ui_components/table/pagination.component.js
index 0f7099b..bcf56ea 100644
--- a/views/ngXosLib/xosHelpers/src/ui_components/table/pagination.component.js
+++ b/views/ngXosLib/xosHelpers/src/ui_components/table/pagination.component.js
@@ -19,7 +19,7 @@
change: '='
},
template: `
- <div class="row">
+ <div class="row" ng-if="vm.pageList.length > 1">
<div class="col-xs-12 text-center">
<ul class="pagination">
<li
@@ -68,7 +68,7 @@
// watch for data changes
$scope.$watch(() => this.totalElements, () => {
if(this.totalElements){
- this.pages = Math.round(this.totalElements / this.pageSize);
+ this.pages = Math.ceil(this.totalElements / this.pageSize);
this.pageList = this.createPages(this.pages);
}
// scope.getPages();
diff --git a/views/ngXosViews/sampleView/src/js/main.js b/views/ngXosViews/sampleView/src/js/main.js
index e2e367d..222882b 100644
--- a/views/ngXosViews/sampleView/src/js/main.js
+++ b/views/ngXosViews/sampleView/src/js/main.js
@@ -55,7 +55,7 @@
filter: 'field',
order: true,
pagination: {
- pageSize: 3
+ pageSize: 6
}
};
diff --git a/xos/core/xoslib/static/js/vendor/ngXosHelpers.js b/xos/core/xoslib/static/js/vendor/ngXosHelpers.js
index 45fab65..ad4d783 100644
--- a/xos/core/xoslib/static/js/vendor/ngXosHelpers.js
+++ b/xos/core/xoslib/static/js/vendor/ngXosHelpers.js
@@ -219,7 +219,7 @@
totalElements: '=',
change: '='
},
- template: '\n <div class="row">\n <div class="col-xs-12 text-center">\n <ul class="pagination">\n <li\n ng-click="vm.goToPage(vm.currentPage - 1)"\n ng-class="{disabled: vm.currentPage == 0}">\n <a href="" aria-label="Previous">\n <span aria-hidden="true">«</span>\n </a>\n </li>\n <li ng-repeat="i in vm.pageList" ng-class="{active: i === vm.currentPage}">\n <a href="" ng-click="vm.goToPage(i)">{{i + 1}}</a>\n </li>\n <li\n ng-click="vm.goToPage(vm.currentPage + 1)"\n ng-class="{disabled: vm.currentPage == vm.pages - 1}">\n <a href="" aria-label="Next">\n <span aria-hidden="true">»</span>\n </a>\n </li>\n </ul>\n </div>\n </div>\n ',
+ template: '\n <div class="row" ng-if="vm.pageList.length > 1">\n <div class="col-xs-12 text-center">\n <ul class="pagination">\n <li\n ng-click="vm.goToPage(vm.currentPage - 1)"\n ng-class="{disabled: vm.currentPage == 0}">\n <a href="" aria-label="Previous">\n <span aria-hidden="true">«</span>\n </a>\n </li>\n <li ng-repeat="i in vm.pageList" ng-class="{active: i === vm.currentPage}">\n <a href="" ng-click="vm.goToPage(i)">{{i + 1}}</a>\n </li>\n <li\n ng-click="vm.goToPage(vm.currentPage + 1)"\n ng-class="{disabled: vm.currentPage == vm.pages - 1}">\n <a href="" aria-label="Next">\n <span aria-hidden="true">»</span>\n </a>\n </li>\n </ul>\n </div>\n </div>\n ',
bindToController: true,
controllerAs: 'vm',
controller: ["$scope", function controller($scope) {
@@ -248,7 +248,7 @@
return _this.totalElements;
}, function () {
if (_this.totalElements) {
- _this.pages = Math.round(_this.totalElements / _this.pageSize);
+ _this.pages = Math.ceil(_this.totalElements / _this.pageSize);
_this.pageList = _this.createPages(_this.pages);
}
// scope.getPages();