Skip to content

Commit

Permalink
fix: #443 Fix pagination issues (#577)
Browse files Browse the repository at this point in the history
  • Loading branch information
sdrejkarz authored May 30, 2024
1 parent 80110fe commit e1963fd
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { MockedResponse } from '@apollo/client/testing';
import { act } from '@testing-library/react-hooks';
import { waitFor } from '@testing-library/react';
import { act } from 'react';

import { fillPaginationItemListQuery, paginationTestItemFactory } from '../../../tests/factories';
import { renderHook } from '../../../tests/utils/rendering';
Expand All @@ -25,10 +26,9 @@ describe('usePaginationQuery: Hook', () => {
},
{ first: 8 }
);
const useEffectCleanMock = initMockedResponse;

const mocks = [initMockedResponse, mock].filter(
(x): x is MockedResponse<Record<string, any>, Record<string, any>> => x !== undefined
);
const mocks = mock ? [initMockedResponse, mock, useEffectCleanMock] : [initMockedResponse, useEffectCleanMock];

const { result, waitForApolloMocks } = renderHook(
() =>
Expand Down Expand Up @@ -61,6 +61,7 @@ describe('usePaginationQuery: Hook', () => {
expect(firstItem?.id).toBe('item-1');

expect(data?.allNotifications?.edges.length).toBe(initDataLength);
await waitForApolloMocks();
});

it('should fetch next page', async () => {
Expand All @@ -87,9 +88,9 @@ describe('usePaginationQuery: Hook', () => {
result.current.loadNext();
});

await waitForApolloMocks();
await waitForApolloMocks(1);

expect(result.current.data?.allNotifications?.edges.length).toBe(nextDataLength);
await waitFor(() => expect(result.current.data?.allNotifications?.edges.length).toBe(nextDataLength));
});

it('should fetch previous page', async () => {
Expand All @@ -110,7 +111,7 @@ describe('usePaginationQuery: Hook', () => {
result.current.loadPrevious();
});

await waitForApolloMocks();
await waitForApolloMocks(1);

expect(result.current.data?.allNotifications?.edges.length).toBe(initDataLength);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,33 +62,44 @@ export const usePaginatedQuery = <
const { data, loading, fetchMore } = useQuery<A, CursorsInput & B>(query, options.hookOptions);

useEffect(() => {
if (cachedCursors.includes(data?.[options.dataKey]?.pageInfo.endCursor)) {
setCachedCursors([]);
}
const currentEndCursor = data?.[options.dataKey]?.pageInfo.endCursor;
const isFirstEndCursor = cachedCursors.indexOf(currentEndCursor) === 0;
if (isFirstEndCursor) setCachedCursors([]);
}, [data, cachedCursors, options.dataKey]);

useEffect(() => {
setHasPrevious(cachedCursors.length > 0);
setHasNext(data?.[options.dataKey]?.pageInfo.hasNextPage ?? false);
}, [data, cachedCursors.length, options.dataKey]);

useEffect(() => {
const setCacheToFirstPage = () => {
fetchMore({
updateQuery: (_, { fetchMoreResult }) => fetchMoreResult,
});
};

return setCacheToFirstPage;
}, [fetchMore]);

const loadNext = useCallback(() => {
const queryData = data?.[options.dataKey];
const endCursor = queryData?.pageInfo.endCursor;
setCachedCursors((prev) => [...prev, endCursor]);

fetchMore({
variables: {
after: endCursor,
},
updateQuery: (_, { fetchMoreResult }) => {
return fetchMoreResult;
},
}).then(() => {
setCachedCursors((prev) => [...prev, endCursor]);
});
}, [data, setCachedCursors, fetchMore, options.dataKey]);

const loadPrevious = useCallback(() => {
const newCachedCursors = cachedCursors.slice(0, -1);
setCachedCursors(newCachedCursors);
const lastEndCursor = newCachedCursors.length > 0 ? newCachedCursors[newCachedCursors.length - 1] : undefined;

fetchMore({
Expand All @@ -98,6 +109,8 @@ export const usePaginatedQuery = <
updateQuery: (_, { fetchMoreResult }) => {
return fetchMoreResult;
},
}).then(() => {
setCachedCursors(newCachedCursors);
});
}, [cachedCursors, setCachedCursors, fetchMore]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ describe('CrudDemoItemList: Component', () => {
})
),
fillCrudDemoItemPaginationListQuery(allItems, {}, { tenantId, first: 8 }),
fillCrudDemoItemPaginationListQuery(allItems, {}, { tenantId, first: 8 }),
];
render(<Component />, { routerProps, apolloMocks });

Expand All @@ -50,7 +51,19 @@ describe('CrudDemoItemList: Component', () => {

it('should render link to add new item form', async () => {
const routerProps = createMockRouterProps(RoutesConfig.crudDemoItem.list);
const apolloMocks = [fillCommonQueryWithUser(), fillCrudDemoItemPaginationListQuery(allItems, {}, { first: 8 })];
const apolloMocks = [
fillCommonQueryWithUser(
currentUserFactory({
tenants: [
tenantFactory({
id: tenantId,
}),
],
})
),
fillCrudDemoItemPaginationListQuery(allItems, {}, { tenantId, first: 8 }),
fillCrudDemoItemPaginationListQuery(allItems, {}, { tenantId, first: 8 }),
];

render(<Component />, { routerProps, apolloMocks });

Expand Down

0 comments on commit e1963fd

Please sign in to comment.