aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/hooks/use-posts-list
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils/hooks/use-posts-list')
-rw-r--r--src/utils/hooks/use-posts-list/index.ts1
-rw-r--r--src/utils/hooks/use-posts-list/use-posts-list.test.tsx74
-rw-r--r--src/utils/hooks/use-posts-list/use-posts-list.ts83
3 files changed, 0 insertions, 158 deletions
diff --git a/src/utils/hooks/use-posts-list/index.ts b/src/utils/hooks/use-posts-list/index.ts
deleted file mode 100644
index 664c142..0000000
--- a/src/utils/hooks/use-posts-list/index.ts
+++ /dev/null
@@ -1 +0,0 @@
-export * from './use-posts-list';
diff --git a/src/utils/hooks/use-posts-list/use-posts-list.test.tsx b/src/utils/hooks/use-posts-list/use-posts-list.test.tsx
deleted file mode 100644
index f23ddde..0000000
--- a/src/utils/hooks/use-posts-list/use-posts-list.test.tsx
+++ /dev/null
@@ -1,74 +0,0 @@
-import {
- afterEach,
- beforeEach,
- describe,
- expect,
- it,
- jest,
-} from '@jest/globals';
-import { act, renderHook } from '@testing-library/react';
-import type { ReactNode } from 'react';
-import { SWRConfig } from 'swr';
-import { fetchPostsList } from '../../../services/graphql';
-import { usePostsList } from './use-posts-list';
-
-const wrapper = ({ children }: { children?: ReactNode }) => {
- const map = new Map();
-
- return (
- <SWRConfig
- value={{
- provider: () => map,
- isOnline() {
- return true;
- },
- isVisible() {
- return true;
- },
- initFocus() {
- /* nothing */
- },
- initReconnect() {
- /* nothing */
- },
- }}
- >
- {children}
- </SWRConfig>
- );
-};
-
-describe('usePostsList', () => {
- beforeEach(() => {
- /* Not sure why it is needed, but without it Jest was complaining with `You
- * are trying to import a file after the Jest environment has been torn
- * down`... Maybe because of useSWR? */
- jest.useFakeTimers({
- doNotFake: ['queueMicrotask'],
- });
- });
-
- afterEach(() => {
- jest.runOnlyPendingTimers();
- jest.useRealTimers();
- });
-
- it('can return the first new result index when loading more posts', async () => {
- const perPage = 5;
- const { result } = renderHook(
- () => usePostsList({ fetcher: fetchPostsList, perPage }),
- { wrapper }
- );
-
- expect.assertions(2);
-
- expect(result.current.firstNewResultIndex).toBeUndefined();
-
- await act(async () => {
- await result.current.loadMore();
- });
-
- // Assuming there is more than one page.
- expect(result.current.firstNewResultIndex).toBe(perPage + 1);
- });
-});
diff --git a/src/utils/hooks/use-posts-list/use-posts-list.ts b/src/utils/hooks/use-posts-list/use-posts-list.ts
deleted file mode 100644
index bb77f31..0000000
--- a/src/utils/hooks/use-posts-list/use-posts-list.ts
+++ /dev/null
@@ -1,83 +0,0 @@
-import { useCallback, useState } from 'react';
-import type {
- ArticlePreview,
- GraphQLConnection,
- GraphQLEdge,
- Maybe,
- WPPostPreview,
-} from '../../../types';
-import {
- type UsePaginationConfig,
- usePagination,
- type UsePaginationReturn,
-} from '../use-pagination';
-import { convertPostPreviewToArticlePreview } from 'src/services/graphql';
-
-export type usePostsListReturn = Omit<
- UsePaginationReturn<WPPostPreview>,
- 'data'
-> & {
- /**
- * The articles list.
- */
- articles: Maybe<GraphQLConnection<ArticlePreview>[]>;
- /**
- * The index of the first new result when loading more posts.
- */
- firstNewResultIndex: Maybe<number>;
-};
-
-export const usePostsList = (
- config: UsePaginationConfig<WPPostPreview>
-): usePostsListReturn => {
- const {
- data,
- error,
- hasNextPage,
- isEmpty,
- isError,
- isLoading,
- isLoadingMore,
- isRefreshing,
- isValidating,
- loadMore,
- size,
- } = usePagination(config);
- const [firstNewResultIndex, setFirstNewResultIndex] =
- useState<Maybe<number>>(undefined);
-
- const handleLoadMore = useCallback(async () => {
- setFirstNewResultIndex(size * config.perPage + 1);
-
- await loadMore();
- }, [config.perPage, loadMore, size]);
-
- const articles: Maybe<GraphQLConnection<ArticlePreview>[]> = data?.map(
- (page): GraphQLConnection<ArticlePreview> => {
- return {
- edges: page.edges.map((edge): GraphQLEdge<ArticlePreview> => {
- return {
- cursor: edge.cursor,
- node: convertPostPreviewToArticlePreview(edge.node),
- };
- }),
- pageInfo: page.pageInfo,
- };
- }
- );
-
- return {
- articles,
- error,
- firstNewResultIndex,
- hasNextPage,
- isEmpty,
- isError,
- isLoading,
- isLoadingMore,
- isRefreshing,
- isValidating,
- loadMore: handleLoadMore,
- size,
- };
-};