Skip to content

Commit

Permalink
fix(pagination): add missing setCursorBased() method for dynamic chan…
Browse files Browse the repository at this point in the history
…ge (#1171)
  • Loading branch information
ghiscoding committed Nov 2, 2023
1 parent caca228 commit 886170e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
15 changes: 13 additions & 2 deletions packages/common/src/services/__tests__/pagination.service.spec.ts
Expand Up @@ -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);
Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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', () => {
Expand Down
8 changes: 7 additions & 1 deletion packages/common/src/services/pagination.service.ts
Expand Up @@ -35,6 +35,7 @@ export class PaginationService {
protected _previousPagination?: Pagination;
protected _subscriptions: EventSubscription[] = [];
protected _cursorPageInfo?: CursorPageInfo;
protected _isCursorBased = false;

/** SlickGrid Grid object */
grid!: SlickGrid;
Expand Down Expand Up @@ -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) {
Expand All @@ -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.`);
Expand Down Expand Up @@ -493,6 +495,10 @@ export class PaginationService {
}
}

setCursorBased(isWithCursor: boolean) {
this._isCursorBased = isWithCursor;
}

setCursorPageInfo(pageInfo: CursorPageInfo) {
this._cursorPageInfo = pageInfo;
}
Expand Down

0 comments on commit 886170e

Please sign in to comment.