Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(pagination): add missing setCursorBased() method for dynamic change #1171

Merged
merged 1 commit into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions packages/common/src/services/__tests__/pagination.service.spec.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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