aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils
diff options
context:
space:
mode:
authorArmand Philippot <git@armandphilippot.com>2023-11-27 19:40:40 +0100
committerArmand Philippot <git@armandphilippot.com>2023-11-28 18:04:24 +0100
commitfb749e8befb2dcdc266c2e8b7ef7c9001947143a (patch)
tree80ae62cd0a699d4feb14fe2a239b4fe317a400a0 /src/utils
parentab81df7f3d317281a05caec18e2cfd89dc26bc7a (diff)
test(services): add tests for posts fetchers
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/hooks/use-posts-list/use-posts-list.test.ts24
-rw-r--r--src/utils/hooks/use-posts-list/use-posts-list.test.tsx74
2 files changed, 74 insertions, 24 deletions
diff --git a/src/utils/hooks/use-posts-list/use-posts-list.test.ts b/src/utils/hooks/use-posts-list/use-posts-list.test.ts
deleted file mode 100644
index ff69de2..0000000
--- a/src/utils/hooks/use-posts-list/use-posts-list.test.ts
+++ /dev/null
@@ -1,24 +0,0 @@
-import { describe, expect, it } from '@jest/globals';
-import { act, renderHook } from '@testing-library/react';
-import { fetchPostsList } from '../../../services/graphql';
-import { usePostsList } from './use-posts-list';
-
-describe('usePostsList', () => {
- it('can return the first new result index when loading more posts', async () => {
- const perPage = 5;
- const { result } = renderHook(() =>
- usePostsList({ fetcher: fetchPostsList, perPage })
- );
-
- 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.test.tsx b/src/utils/hooks/use-posts-list/use-posts-list.test.tsx
new file mode 100644
index 0000000..f23ddde
--- /dev/null
+++ b/src/utils/hooks/use-posts-list/use-posts-list.test.tsx
@@ -0,0 +1,74 @@
+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);
+ });
+});