From 880acefa81f2d79d89a55a4b2a3ef5f878994538 Mon Sep 17 00:00:00 2001 From: Gabriel Guerrero Date: Wed, 28 Aug 2024 22:46:47 +0100 Subject: [PATCH] fix(signals): fix clearEntitiesCache for remote scroll pagination clearEntitiesCache was not reseting the state correctly Fix #128 --- ...-entities-remote-scroll-pagination.spec.ts | 75 +++++++++++++++++++ .../with-entities-remote-scroll-pagination.ts | 2 + 2 files changed, 77 insertions(+) diff --git a/libs/ngrx-traits/signals/src/lib/with-entities-pagination/with-entities-remote-scroll-pagination.spec.ts b/libs/ngrx-traits/signals/src/lib/with-entities-pagination/with-entities-remote-scroll-pagination.spec.ts index a9b9ee4..218b666 100644 --- a/libs/ngrx-traits/signals/src/lib/with-entities-pagination/with-entities-remote-scroll-pagination.spec.ts +++ b/libs/ngrx-traits/signals/src/lib/with-entities-pagination/with-entities-remote-scroll-pagination.spec.ts @@ -594,6 +594,8 @@ describe('withEntitiesRemoteScrollPagination', () => { }); tick(400); expect(store.entities().length).toEqual(2); + expect(store.pagination().currentPage).toEqual(0); + expect(store.pagination().requestPage).toEqual(0); }); })); @@ -656,6 +658,79 @@ describe('withEntitiesRemoteScrollPagination', () => { }); tick(400); expect(store.entities().length).toEqual(30); + expect(store.pagination().currentPage).toEqual(0); + expect(store.pagination().requestPage).toEqual(0); + }); + })); + + it('should reset cache when filter is executed and nextPage was called', fakeAsync(() => { + TestBed.runInInjectionContext(() => { + const Store = signalStore( + withEntities({ + entity, + }), + withCallStatus({ initialValue: 'loading' }), + withEntitiesRemoteScrollPagination({ + entity, + pageSize: 10, + pagesToCache: 3, + }), + withEntitiesRemoteFilter({ + entity, + defaultFilter: { search: '', foo: 'bar' }, + }), + withEntitiesLoadingCall({ + fetchEntities: ({ entitiesFilter, entitiesPagedRequest }) => { + let result = [...mockProducts]; + const total = result.length; + if (entitiesFilter()?.search) + result = mockProducts.filter((entity) => + entitiesFilter()?.search + ? entity.name + .toLowerCase() + .includes(entitiesFilter()?.search.toLowerCase()) + : false, + ); + const options = { + skip: entitiesPagedRequest()?.startIndex, + take: entitiesPagedRequest()?.size, + }; + if (options?.skip || options?.take) { + const skip = +(options?.skip ?? 0); + const take = +(options?.take ?? 0); + result = result.slice(skip, skip + take); + } + return of({ entities: result, total }); + return Promise.resolve({ entities: result, total: result.length }); + }, + }), + ); + const store = new Store(); + TestBed.flushEffects(); + // first fill cache + tick(); + expect(store.entities().length).toEqual(30); + store.loadEntitiesNextPage(); + tick(); + tick(); + store.loadEntitiesNextPage(); + tick(); + store.loadEntitiesNextPage(); + tick(); + expect(store.entities().length).toEqual(60); + expect(store.entitiesCurrentPage().pageIndex).toEqual(3); + expect(store.entitiesCurrentPage().entities).toEqual( + mockProducts.slice(30, 40), + ); + + store.filterEntities({ + filter: { search: 'zero' }, + patch: true, + }); + tick(400); + expect(store.entities().length).toEqual(2); + expect(store.pagination().currentPage).toEqual(0); + expect(store.pagination().requestPage).toEqual(0); }); })); diff --git a/libs/ngrx-traits/signals/src/lib/with-entities-pagination/with-entities-remote-scroll-pagination.ts b/libs/ngrx-traits/signals/src/lib/with-entities-pagination/with-entities-remote-scroll-pagination.ts index eb7f7f1..0545f86 100644 --- a/libs/ngrx-traits/signals/src/lib/with-entities-pagination/with-entities-remote-scroll-pagination.ts +++ b/libs/ngrx-traits/signals/src/lib/with-entities-pagination/with-entities-remote-scroll-pagination.ts @@ -456,6 +456,8 @@ function clearEntitiesCache( ...pagination(), total: 0, hasMore: true, + currentPage: 0, + requestPage: 0, }, }, );