Skip to content

Commit

Permalink
Improved tests and fixed destroy call before initialization #54
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelthq committed Nov 4, 2015
1 parent 51c019e commit 2b8491c
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 12 deletions.
4 changes: 2 additions & 2 deletions jquery.twbsPagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@

var $this = $(this);
var data = $this.data('twbs-pagination');
var options = typeof option === 'object' && option;
var options = typeof option === 'object' ? option : {};

if (!data) $this.data('twbs-pagination', (data = new TwbsPagination(this, options) ));
if (typeof option === 'string') methodReturn = data[ option ].apply(data, args);
Expand All @@ -247,7 +247,7 @@
};

$.fn.twbsPagination.defaults = {
totalPages: 0,
totalPages: 1,
startPage: 1,
visiblePages: 5,
initiateStartPageClick: true,
Expand Down
2 changes: 1 addition & 1 deletion jquery.twbsPagination.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions tests/run-test-jquery-1.8.1.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Tests</title>

<link rel="stylesheet" href="http://code.jquery.com/qunit/qunit-1.13.0.css">

<script src="http://code.jquery.com/jquery-1.8.1.min.js" type="text/javascript"></script>
<script src="../jquery.twbsPagination.js" type="text/javascript"></script>

</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<ul id="pagination" style="display: none;"></ul>

<script src="http://code.jquery.com/qunit/qunit-1.13.0.js"></script>
<script src="./test.js"></script>
</body>
</html>
2 changes: 1 addition & 1 deletion tests/run-test.html → tests/run-test-jquery-latest.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<link rel="stylesheet" href="http://code.jquery.com/qunit/qunit-1.13.0.css">

<script src="http://code.jquery.com/jquery-2.0.3.min.js" type="text/javascript"></script>
<script src="../jquery.twbsPagination.min.js" type="text/javascript"></script>
<script src="../jquery.twbsPagination.js" type="text/javascript"></script>

</head>
<body>
Expand Down
94 changes: 86 additions & 8 deletions tests/test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
(function () {
(function ($) {

var pag1 = $('#pagination');
pag1.twbsPagination({
totalPages: 30

QUnit.test("Test destroy called before initialization", function () {
ok(pag1.twbsPagination('destroy'));
});

test("Test 'getPages' method (EVEN visible pages number)", function () {
QUnit.test("Test 'getPages' method (EVEN visible pages number)", function () {
pag1.twbsPagination('destroy');
pag1.twbsPagination({
totalPages: 30
});

var expected1 = {currentPage: 1, numeric: [1, 2, 3, 4, 5]};
deepEqual(pag1.twbsPagination('getPages', 1), expected1);
var expected2 = {currentPage: 2, numeric: [1, 2, 3, 4, 5]};
Expand All @@ -30,9 +36,13 @@
deepEqual(pag1.twbsPagination('getPages', 30), expected30);
});

test("Test 'getPages' method (ODD visible pages number)", function () {
QUnit.test("Test 'getPages' method (ODD visible pages number)", function () {
pag1.twbsPagination('destroy');
pag1.twbsPagination({totalPages: 30, visiblePages: 6});
pag1.twbsPagination({
totalPages: 30,
visiblePages: 6
});

var expected1 = {currentPage: 1, numeric: [1, 2, 3, 4, 5, 6]};
deepEqual(pag1.twbsPagination('getPages', 1), expected1);
var expected2 = {currentPage: 2, numeric: [1, 2, 3, 4, 5, 6]};
Expand All @@ -57,7 +67,7 @@
deepEqual(pag1.twbsPagination('getPages', 30), expected30);
});

test("Test 'getPages' method (total < visible)", function () {
QUnit.test("Test 'getPages' method (total < visible)", function () {
pag1.twbsPagination('destroy');
pag1.twbsPagination({
totalPages: 3,
Expand All @@ -71,4 +81,72 @@
deepEqual(pag1.twbsPagination('getPages', 3), exp3);
});

})();
QUnit.test("Test classes appended for pagination", function () {
pag1.twbsPagination('destroy');
pag1.twbsPagination({
totalPages: 3,
visiblePages: 5
});
equal(pag1.find('.page').length, 3);
equal(pag1.find('.next').length, 1);
equal(pag1.find('.prev').length, 1);
equal(pag1.find('.last').length, 1);
equal(pag1.find('.first').length, 1);
equal(pag1.find('.page.active').length, 1);
equal(pag1.find('.prev.disabled').length, 1);
equal(pag1.find('.first.disabled').length, 1);
});

QUnit.test("Test custom classes appended for pagination", function () {
pag1.twbsPagination('destroy');
pag1.twbsPagination({
totalPages: 10,
visiblePages: 5,
pageClass: 'my-page',
nextClass: 'my-next-page',
prevClass: 'my-prev-page',
lastClass: 'my-last-page',
firstClass: 'my-first-page',
activeClass: 'my-active-class',
disabledClass: 'my-disabled-class'
});
equal(pag1.find('.my-page').length, 5);
equal(pag1.find('.my-next-page').length, 1);
equal(pag1.find('.my-prev-page').length, 1);
equal(pag1.find('.my-last-page').length, 1);
equal(pag1.find('.my-first-page').length, 1);
equal(pag1.find('.my-page.my-active-class').length, 1);
equal(pag1.find('.my-prev-page.my-disabled-class').length, 1);
equal(pag1.find('.my-first-page.my-disabled-class').length, 1);
});

QUnit.test("Test page numbers text", function () {
pag1.twbsPagination('destroy');
pag1.twbsPagination({
totalPages: 2
});
equal(pag1.find('.page:eq(0)').text(), '1');
equal(pag1.find('.next').text(), 'Next');
equal(pag1.find('.prev').text(), 'Previous');
equal(pag1.find('.first').text(), 'First');
equal(pag1.find('.last').text(), 'Last');
});

QUnit.test("Test custom texts", function () {
pag1.twbsPagination('destroy');
pag1.twbsPagination({
totalPages: 2,
page: '[{{page}}]',
first: '(first)',
prev: '<<',
next: '>>',
last: '(last)[{{total_pages}}]'
});
equal(pag1.find('.page:eq(0)').text(), '[1]');
equal(pag1.find('.next').text(), '>>');
equal(pag1.find('.prev').text(), '<<');
equal(pag1.find('.first').text(), '(first)');
equal(pag1.find('.last').text(), '(last)[2]');
});

})(window.jQuery);

0 comments on commit 2b8491c

Please sign in to comment.