From 39c5a34bf880700bc5616cb391f4d301e5fb999a Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Sun, 27 Oct 2019 23:21:40 -0400 Subject: [PATCH] Fix page count when filteredCount is falsy --- src/directives/pagination.ts | 8 ++++---- src/directives/table.ts | 2 +- test/directives/pagination.js | 9 ++++++++- test/directives/table.js | 20 ++++++++++++++++++++ 4 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/directives/pagination.ts b/src/directives/pagination.ts index 4e58d4d..671ac8b 100644 --- a/src/directives/pagination.ts +++ b/src/directives/pagination.ts @@ -39,8 +39,8 @@ export interface PaginationDirectiveConfiguration { export const paginationDirective = ({table}: PaginationDirectiveConfiguration): PaginationDirective => { let {slice: {page: currentPage, size: currentSize}} = table.getTableState(); - let itemListLength = table.filteredCount; - let pageCount = currentSize ? Math.ceil(itemListLength / currentSize) : 1; + let itemListLength = table.filteredCount || 0; + let pageCount = currentSize && itemListLength ? Math.ceil(itemListLength / currentSize) : 1; const proxy = sliceListener({emitter: table}); @@ -72,8 +72,8 @@ export const paginationDirective = ({table}: PaginationDirectiveConfiguration directive.onSummaryChange(({page: p, size: s, filteredCount}: Summary) => { currentPage = p; currentSize = s; - itemListLength = filteredCount; - pageCount = currentSize ? Math.ceil(itemListLength / currentSize) : 1; + itemListLength = filteredCount || 0; + pageCount = currentSize && itemListLength ? Math.ceil(itemListLength / currentSize) : 1; }); return directive; diff --git a/src/directives/table.ts b/src/directives/table.ts index 9eb87c4..be1fa03 100644 --- a/src/directives/table.ts +++ b/src/directives/table.ts @@ -84,7 +84,7 @@ export const tableDirective = ({sortFactory, tableState, data, filterFactory, // We need to register in case the summary comes from outside (like server data) table.on(SmartTableEvents.SUMMARY_CHANGED, ({filteredCount: count}) => { - filteredCount = count; + filteredCount = count || 0; }); const safeAssign = newState => Object.assign({}, newState); diff --git a/test/directives/pagination.js b/test/directives/pagination.js index fe7bf46..fe677bd 100644 --- a/test/directives/pagination.js +++ b/test/directives/pagination.js @@ -83,4 +83,11 @@ export default ({test}) => { table.dispatch(evts.SUMMARY_CHANGED, {page: 1, size: 0}); t.deepEqual(dir.state().pageCount, 1); }); -} \ No newline at end of file + test('pagination directive accepts falsy value for filteredCount', t => { + const table = fakeTable({page: 1, size: 1}); + const dir = pagination({table}); + t.deepEqual(dir.state().pageCount, 1); + table.dispatch(evts.SUMMARY_CHANGED, {page: 1, size: 1, filteredCount: 0}); + t.deepEqual(dir.state().pageCount, 1); + }); +} diff --git a/test/directives/table.js b/test/directives/table.js index 9500684..4909efe 100644 --- a/test/directives/table.js +++ b/test/directives/table.js @@ -316,6 +316,26 @@ export default ({test}) => { t.equal(table.filteredCount, 2, 'filtered count should have been updated'); }); + test('filteredCount allows falsy value', t => { + const tableState = { + sort: {pointer: 'id', direction: SortDirection.DESC}, + search: {}, + filter: {name: [{value: 'b'}]}, + slice: {page: 1, size: 1} + }; + const table = tableFactory({ + data: [ + {id: 1, name: 'foo'}, + {id: 2, name: 'blah'}, + {id: 3, name: 'bip'} + ], + tableState + }); + t.equal(table.filteredCount, 3, 'initially with the length of data array'); + table.dispatch(evts.SUMMARY_CHANGED, {}); + t.equal(table.filteredCount, 0, 'filteredCount should be a number'); + }); + test('getTableState should return a deep copy of the tableState', t => { const tableState = { sort: {pointer: 'foo'},