Repository: gregnb/mui-datatables
Pull request #1014 (2019-10-18 23:38:45)

Bugfix/issue 994 properly calculate page
Closes https://github.com/gregnb/mui-datatables/issues/994#issuecomment-543956214.

coveralls: 
[![Coverage Status](https://coveralls.io/builds/26410746/badge)](https://coveralls.io/builds/26410746)

Coverage remained the same at 77.358% when pulling **d25b72bfecd29118abee49c5175fe89ef0143a9c on bugfix/issue-994-properly-calculate-page** into **86deadf48722453db45f68116638dee1c627f9b5 on master**.


Patch:
diff --git a/src/utils.js b/src/utils.js
index 0e8fcb6..530ef0d 100644
--- a/src/utils.js
+++ b/src/utils.js
@@ -6,9 +6,10 @@ function buildMap(rows) {
 }
 
 function getPageValue(count, rowsPerPage, page) {
-  const totalPages = count === rowsPerPage ? 0 : Math.floor(count / rowsPerPage);
+  const totalPages = count <= rowsPerPage ? 1 : Math.ceil(count / rowsPerPage);
 
-  return page > totalPages ? totalPages : page;
+  // `page` is 0-indexed
+  return page >= totalPages ? totalPages - 1 : page;
 }
 
 function getCollatorComparator() {


Test patch:
diff --git a/test/utils.test.js b/test/utils.test.js
new file mode 100644
index 0000000..0d2311a
--- /dev/null
+++ b/test/utils.test.js
@@ -0,0 +1,60 @@
+import { getPageValue } from '../src/utils';
+import { assert } from 'chai';
+
+describe('utils.js', () => {
+  describe('getPageValue', () => {
+    it('returns the highest in bounds page value when page is out of bounds and count is greater than rowsPerPage', () => {
+      const count = 30;
+      const rowsPerPage = 10;
+      const page = 5;
+
+      const actualResult = getPageValue(count, rowsPerPage, page);
+      assert.strictEqual(actualResult, 2);
+    });
+
+    it('returns the highest in bounds page value when page is in bounds and count is greater than rowsPerPage', () => {
+      const count = 30;
+      const rowsPerPage = 10;
+      const page = 1;
+
+      const actualResult = getPageValue(count, rowsPerPage, page);
+      assert.strictEqual(actualResult, 1);
+    });
+
+    it('returns the highest in bounds page value when page is out of bounds and count is less than rowsPerPage', () => {
+      const count = 3;
+      const rowsPerPage = 10;
+      const page = 1;
+
+      const actualResult = getPageValue(count, rowsPerPage, page);
+      assert.strictEqual(actualResult, 0);
+    });
+
+    it('returns the highest in bounds page value when page is in bounds and count is less than rowsPerPage', () => {
+      const count = 3;
+      const rowsPerPage = 10;
+      const page = 0;
+
+      const actualResult = getPageValue(count, rowsPerPage, page);
+      assert.strictEqual(actualResult, 0);
+    });
+
+    it('returns the highest in bounds page value when page is out of bounds and count is equal to rowsPerPage', () => {
+      const count = 10;
+      const rowsPerPage = 10;
+      const page = 1;
+
+      const actualResult = getPageValue(count, rowsPerPage, page);
+      assert.strictEqual(actualResult, 0);
+    });
+
+    it('returns the highest in bounds page value when page is in bounds and count is equal to rowsPerPage', () => {
+      const count = 10;
+      const rowsPerPage = 10;
+      const page = 0;
+
+      const actualResult = getPageValue(count, rowsPerPage, page);
+      assert.strictEqual(actualResult, 0);
+    });
+  });
+});