From c49f69d95f5d675f93d3d52491244a8de397aa70 Mon Sep 17 00:00:00 2001 From: ghiscoding Date: Thu, 2 Nov 2023 02:29:23 -0400 Subject: [PATCH] fix(pagination): add missing setCursorBased() method for dynamic change --- .../services/__tests__/pagination.service.spec.ts | 15 +++++++++++++-- .../common/src/services/pagination.service.ts | 8 +++++++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/packages/common/src/services/__tests__/pagination.service.spec.ts b/packages/common/src/services/__tests__/pagination.service.spec.ts index 45ca7352b..3c76c714f 100644 --- a/packages/common/src/services/__tests__/pagination.service.spec.ts +++ b/packages/common/src/services/__tests__/pagination.service.spec.ts @@ -213,6 +213,17 @@ describe('PaginationService', () => { }); describe('goToFirstPage method', () => { + it('should be able to change isCursorBased flag by calling setCursorBased()', () => { + const spy = jest.spyOn(service, 'processOnPageChanged'); + service.init(gridStub, mockGridOption.pagination as Pagination, mockGridOption.backendServiceApi); + + service.setCursorBased(true); + expect(service.isCursorBased).toBeTruthy(); + + service.setCursorBased(false); + expect(service.isCursorBased).toBeFalsy(); + }); + it('should expect current page to be 1 and "processOnPageChanged" method to be called', () => { const spy = jest.spyOn(service, 'processOnPageChanged'); service.init(gridStub, mockGridOption.pagination as Pagination, mockGridOption.backendServiceApi); @@ -306,7 +317,7 @@ describe('PaginationService', () => { expect(service.dataFrom).toBe(51); expect(service.dataTo).toBe(75); expect(service.getCurrentPageNumber()).toBe(3); - expect(spy).toHaveBeenCalledWith(3, undefined, {first: 25, after: "c", newPage: 3, pageSize: 25 }); + expect(spy).toHaveBeenCalledWith(3, undefined, { first: 25, after: "c", newPage: 3, pageSize: 25 }); }); it('should expect page to increment by 1 and "processOnPageChanged" method NOT to be called', () => { @@ -356,7 +367,7 @@ describe('PaginationService', () => { expect(service.dataFrom).toBe(1); expect(service.dataTo).toBe(25); expect(service.getCurrentPageNumber()).toBe(1); - expect(spy).toHaveBeenCalledWith(1, undefined, {last: 25, before: "b", newPage: 1, pageSize: 25 }); + expect(spy).toHaveBeenCalledWith(1, undefined, { last: 25, before: "b", newPage: 1, pageSize: 25 }); }); it('should expect page to decrement by 1 and "processOnPageChanged" method NOT to be called', () => { diff --git a/packages/common/src/services/pagination.service.ts b/packages/common/src/services/pagination.service.ts index 29b66d7f2..a16d110f0 100644 --- a/packages/common/src/services/pagination.service.ts +++ b/packages/common/src/services/pagination.service.ts @@ -35,6 +35,7 @@ export class PaginationService { protected _previousPagination?: Pagination; protected _subscriptions: EventSubscription[] = []; protected _cursorPageInfo?: CursorPageInfo; + protected _isCursorBased = false; /** SlickGrid Grid object */ grid!: SlickGrid; @@ -107,7 +108,7 @@ export class PaginationService { * page3: {startCursor: C, endCursor: D } */ get isCursorBased(): boolean { - return !!this._backendServiceApi?.options.isWithCursor; + return this._isCursorBased; } addRxJsResource(rxjs: RxJsFacade) { @@ -121,6 +122,7 @@ export class PaginationService { this._paginationOptions = paginationOptions; this._isLocalGrid = !backendServiceApi; this._pageNumber = paginationOptions.pageNumber || 1; + this._isCursorBased = this._backendServiceApi?.options?.isWithCursor ?? false; if (backendServiceApi && (!backendServiceApi.service || !backendServiceApi.process)) { throw new Error(`BackendServiceApi requires the following 2 properties "process" and "service" to be defined.`); @@ -493,6 +495,10 @@ export class PaginationService { } } + setCursorBased(isWithCursor: boolean) { + this._isCursorBased = isWithCursor; + } + setCursorPageInfo(pageInfo: CursorPageInfo) { this._cursorPageInfo = pageInfo; }