Skip to content

Commit

Permalink
fix: prevent pagination from exceeding max offset (#667)
Browse files Browse the repository at this point in the history
  • Loading branch information
pozil authored Nov 14, 2024
1 parent 627907c commit cdd66d8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
29 changes: 25 additions & 4 deletions force-app/main/default/lwc/paginator/__tests__/paginator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,10 @@ describe('c-paginator', () => {
const element = createElement('c-paginator', {
is: Paginator
});
//Set the public property values
// Set the public property values
element.pageNumber = 0;
element.pageSize = 9;
element.totalItemCount = 0;

document.body.appendChild(element);

// Query div for validating the display message on component init
Expand All @@ -108,12 +107,12 @@ describe('c-paginator', () => {
const element = createElement('c-paginator', {
is: Paginator
});
document.body.appendChild(element);

//Set the public properties for item count greater than zero
// Set the public properties for item count greater than zero
element.pageNumber = 1;
element.pageSize = 9;
element.totalItemCount = 12;
document.body.appendChild(element);

// Query div for validating the display message on component init
const lightningLayoutItemEl =
Expand All @@ -129,6 +128,28 @@ describe('c-paginator', () => {
);
});

it('does not display next page button when reaching max page offset', async () => {
// Create initial element
const element = createElement('c-paginator', {
is: Paginator
});

// Set the public properties to be just below SOQL max offset (2000)
element.pageNumber = 200;
element.pageSize = 10;
element.totalItemCount = 12;
document.body.appendChild(element);

// Wait for any asynchronous DOM updates
await flushPromises();

// Check for next page button
const btnNextEl = element.shadowRoot.querySelector(
'.nav-next lightning-button-icon'
);
expect(btnNextEl).toBeNull();
});

it('is accessible', async () => {
const element = createElement('c-paginator', {
is: Paginator
Expand Down
7 changes: 6 additions & 1 deletion force-app/main/default/lwc/paginator/paginator.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { LightningElement, api } from 'lwc';

const MAX_ITEM_OFFSET = 2000;

export default class Paginator extends LightningElement {
/** The current page number. */
@api pageNumber;
Expand Down Expand Up @@ -27,7 +29,10 @@ export default class Paginator extends LightningElement {
}

get isNotLastPage() {
return this.pageNumber < this.totalPages;
return (
this.pageNumber < this.totalPages &&
this.pageNumber * this.pageSize < MAX_ITEM_OFFSET
);
}

get totalPages() {
Expand Down

0 comments on commit cdd66d8

Please sign in to comment.