aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/services/graphql/fetchers/posts/fetch-all-posts-slugs.test.ts26
-rw-r--r--src/services/graphql/fetchers/posts/fetch-all-posts-slugs.ts2
-rw-r--r--src/services/graphql/fetchers/posts/fetch-last-post-cursor.test.ts25
-rw-r--r--src/services/graphql/fetchers/posts/fetch-last-post-cursor.ts6
-rw-r--r--src/services/graphql/fetchers/posts/fetch-post.test.ts23
-rw-r--r--src/services/graphql/fetchers/posts/fetch-post.ts2
-rw-r--r--src/services/graphql/fetchers/posts/fetch-posts-count.test.ts26
-rw-r--r--src/services/graphql/fetchers/posts/fetch-posts-count.ts6
-rw-r--r--src/services/graphql/fetchers/posts/fetch-posts-list.test.ts26
-rw-r--r--src/services/graphql/fetchers/posts/fetch-posts-list.ts2
-rw-r--r--src/services/graphql/fetchers/posts/fetch-recent-posts.test.ts25
-rw-r--r--src/services/graphql/fetchers/posts/fetch-recent-posts.ts2
-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
14 files changed, 233 insertions, 36 deletions
diff --git a/src/services/graphql/fetchers/posts/fetch-all-posts-slugs.test.ts b/src/services/graphql/fetchers/posts/fetch-all-posts-slugs.test.ts
new file mode 100644
index 0000000..a90df02
--- /dev/null
+++ b/src/services/graphql/fetchers/posts/fetch-all-posts-slugs.test.ts
@@ -0,0 +1,26 @@
+import { afterEach, describe, expect, it } from '@jest/globals';
+import { wpPostsFixture } from '../../../../../tests/fixtures';
+import { fetchAllPostsSlugs } from './fetch-all-posts-slugs';
+
+describe('fetch-all-posts-slugs', () => {
+ afterEach(() => {
+ window.history.replaceState({}, '', '/');
+ });
+
+ it('returns the WordPress posts using GraphQL', async () => {
+ const result = await fetchAllPostsSlugs(wpPostsFixture.length);
+
+ expect.assertions(1);
+
+ expect(result).toStrictEqual(wpPostsFixture.map((post) => post.slug));
+ });
+
+ it('rejects with an error when no posts are found', async () => {
+ window.history.replaceState({}, '', '/?error=true');
+ expect.assertions(1);
+
+ await expect(async () =>
+ fetchAllPostsSlugs(wpPostsFixture.length)
+ ).rejects.toEqual(new Error('Unable to find the posts slugs.'));
+ });
+});
diff --git a/src/services/graphql/fetchers/posts/fetch-all-posts-slugs.ts b/src/services/graphql/fetchers/posts/fetch-all-posts-slugs.ts
index 25cc782..cca83ff 100644
--- a/src/services/graphql/fetchers/posts/fetch-all-posts-slugs.ts
+++ b/src/services/graphql/fetchers/posts/fetch-all-posts-slugs.ts
@@ -1,7 +1,7 @@
import type { GraphQLNodes, Nullable, SlugNode } from '../../../../types';
import { fetchGraphQL, getGraphQLUrl } from '../../../../utils/helpers';
-type PostsSlugsResponse = {
+export type PostsSlugsResponse = {
posts: Nullable<GraphQLNodes<SlugNode>>;
};
diff --git a/src/services/graphql/fetchers/posts/fetch-last-post-cursor.test.ts b/src/services/graphql/fetchers/posts/fetch-last-post-cursor.test.ts
new file mode 100644
index 0000000..4fe2f50
--- /dev/null
+++ b/src/services/graphql/fetchers/posts/fetch-last-post-cursor.test.ts
@@ -0,0 +1,25 @@
+import { afterEach, describe, expect, it } from '@jest/globals';
+import { fetchLastPostCursor } from './fetch-last-post-cursor';
+
+describe('fetch-last-post-cursor', () => {
+ afterEach(() => {
+ window.history.replaceState({}, '', '/');
+ });
+
+ it('returns the cursor of the last WordPress post using GraphQL', async () => {
+ const result = await fetchLastPostCursor(2);
+
+ expect.assertions(1);
+
+ expect(result).toBe('cursor2');
+ });
+
+ it('rejects with an error when no posts are found', async () => {
+ window.history.replaceState({}, '', '/?error=true');
+ expect.assertions(1);
+
+ await expect(async () => fetchLastPostCursor(1)).rejects.toEqual(
+ new Error('Unable to find the cursor of the last post.')
+ );
+ });
+});
diff --git a/src/services/graphql/fetchers/posts/fetch-last-post-cursor.ts b/src/services/graphql/fetchers/posts/fetch-last-post-cursor.ts
index d5ed174..161a50e 100644
--- a/src/services/graphql/fetchers/posts/fetch-last-post-cursor.ts
+++ b/src/services/graphql/fetchers/posts/fetch-last-post-cursor.ts
@@ -1,10 +1,8 @@
import type { GraphQLPageInfo, Nullable } from '../../../../types';
import { fetchGraphQL, getGraphQLUrl } from '../../../../utils/helpers';
-type LastPostCursorResponse = {
- posts: Nullable<{
- pageInfo: Pick<GraphQLPageInfo, 'endCursor'>;
- }>;
+export type LastPostCursorResponse = {
+ posts: Nullable<Record<'pageInfo', Pick<GraphQLPageInfo, 'endCursor'>>>;
};
const lastPostCursorQuery = `query LastPostCursor($first: Int) {
diff --git a/src/services/graphql/fetchers/posts/fetch-post.test.ts b/src/services/graphql/fetchers/posts/fetch-post.test.ts
new file mode 100644
index 0000000..e19e4de
--- /dev/null
+++ b/src/services/graphql/fetchers/posts/fetch-post.test.ts
@@ -0,0 +1,23 @@
+import { describe, expect, it } from '@jest/globals';
+import { wpPostsFixture } from '../../../../../tests/fixtures';
+import { fetchPost } from './fetch-post';
+
+describe('fetch-post', () => {
+ it('returns a post by slug', async () => {
+ const result = await fetchPost(wpPostsFixture[2].slug);
+
+ expect.assertions(1);
+
+ expect(result).toStrictEqual(wpPostsFixture[2]);
+ });
+
+ it('rejects with an error when the slug does not exist', async () => {
+ const slug = '/inexistent-slug';
+
+ expect.assertions(1);
+
+ await expect(async () => fetchPost(slug)).rejects.toEqual(
+ new Error(`No post found for the following slug ${slug}.`)
+ );
+ });
+});
diff --git a/src/services/graphql/fetchers/posts/fetch-post.ts b/src/services/graphql/fetchers/posts/fetch-post.ts
index 53c6bc3..bbd3912 100644
--- a/src/services/graphql/fetchers/posts/fetch-post.ts
+++ b/src/services/graphql/fetchers/posts/fetch-post.ts
@@ -1,7 +1,7 @@
import type { Nullable, WPPost } from '../../../../types';
import { fetchGraphQL, getGraphQLUrl } from '../../../../utils/helpers';
-type PostResponse = {
+export type PostResponse = {
post: Nullable<WPPost>;
};
diff --git a/src/services/graphql/fetchers/posts/fetch-posts-count.test.ts b/src/services/graphql/fetchers/posts/fetch-posts-count.test.ts
new file mode 100644
index 0000000..a0fd594
--- /dev/null
+++ b/src/services/graphql/fetchers/posts/fetch-posts-count.test.ts
@@ -0,0 +1,26 @@
+import { afterEach, describe, expect, it } from '@jest/globals';
+import { wpPostsFixture } from '../../../../../tests/fixtures';
+import { fetchPostsCount } from './fetch-posts-count';
+
+describe('fetch-posts-count', () => {
+ afterEach(() => {
+ window.history.replaceState({}, '', '/');
+ });
+
+ it('returns the WordPress posts count using GraphQL', async () => {
+ const result = await fetchPostsCount();
+
+ expect.assertions(1);
+
+ expect(result).toBe(wpPostsFixture.length);
+ });
+
+ it('rejects with an error when no posts are found', async () => {
+ window.history.replaceState({}, '', '/?error=true');
+ expect.assertions(1);
+
+ await expect(async () => fetchPostsCount()).rejects.toEqual(
+ new Error('Unable to find the total number of posts.')
+ );
+ });
+});
diff --git a/src/services/graphql/fetchers/posts/fetch-posts-count.ts b/src/services/graphql/fetchers/posts/fetch-posts-count.ts
index a72af52..316cd47 100644
--- a/src/services/graphql/fetchers/posts/fetch-posts-count.ts
+++ b/src/services/graphql/fetchers/posts/fetch-posts-count.ts
@@ -5,10 +5,8 @@ import type {
} from '../../../../types';
import { fetchGraphQL, getGraphQLUrl } from '../../../../utils/helpers';
-type PostsCountResponse = {
- posts: Nullable<{
- pageInfo: Pick<GraphQLPageInfo, 'total'>;
- }>;
+export type PostsCountResponse = {
+ posts: Nullable<Record<'pageInfo', Pick<GraphQLPageInfo, 'total'>>>;
};
const postsCountQuery = `query PostsCount($authorName: String, $search: String, $title: String) {
diff --git a/src/services/graphql/fetchers/posts/fetch-posts-list.test.ts b/src/services/graphql/fetchers/posts/fetch-posts-list.test.ts
new file mode 100644
index 0000000..1e21363
--- /dev/null
+++ b/src/services/graphql/fetchers/posts/fetch-posts-list.test.ts
@@ -0,0 +1,26 @@
+import { afterEach, describe, expect, it } from '@jest/globals';
+import { wpPostsFixture } from '../../../../../tests/fixtures';
+import { fetchPostsList } from './fetch-posts-list';
+
+describe('fetch-posts-list', () => {
+ afterEach(() => {
+ window.history.replaceState({}, '', '/');
+ });
+
+ it('returns the WordPress posts using GraphQL', async () => {
+ const result = await fetchPostsList({});
+
+ expect.assertions(1);
+
+ expect(result.pageInfo.total).toBe(wpPostsFixture.length);
+ });
+
+ it('rejects with an error when no posts are found', async () => {
+ window.history.replaceState({}, '', '/?error=true');
+ expect.assertions(1);
+
+ await expect(async () =>
+ fetchPostsList({ where: { authorName: 'inexistent-author' } })
+ ).rejects.toEqual(new Error('No posts found.'));
+ });
+});
diff --git a/src/services/graphql/fetchers/posts/fetch-posts-list.ts b/src/services/graphql/fetchers/posts/fetch-posts-list.ts
index 452892b..0bf6dc1 100644
--- a/src/services/graphql/fetchers/posts/fetch-posts-list.ts
+++ b/src/services/graphql/fetchers/posts/fetch-posts-list.ts
@@ -8,7 +8,7 @@ import type {
} from '../../../../types';
import { fetchGraphQL, getGraphQLUrl } from '../../../../utils/helpers';
-type PostsListResponse = {
+export type PostsListResponse = {
posts: Nullable<GraphQLConnection<WPPostPreview>>;
};
diff --git a/src/services/graphql/fetchers/posts/fetch-recent-posts.test.ts b/src/services/graphql/fetchers/posts/fetch-recent-posts.test.ts
new file mode 100644
index 0000000..2d2fcc8
--- /dev/null
+++ b/src/services/graphql/fetchers/posts/fetch-recent-posts.test.ts
@@ -0,0 +1,25 @@
+import { afterEach, describe, expect, it } from '@jest/globals';
+import { fetchRecentPosts } from './fetch-recent-posts';
+
+describe('fetch-recent-posts', () => {
+ afterEach(() => {
+ window.history.replaceState({}, '', '/');
+ });
+
+ it('returns the WordPress most recent posts using GraphQL', async () => {
+ const result = await fetchRecentPosts({ first: 2 });
+
+ expect.assertions(1);
+
+ expect(result.edges.length).toBe(2);
+ });
+
+ it('rejects with an error when no posts are found', async () => {
+ window.history.replaceState({}, '', '/?error=true');
+ expect.assertions(1);
+
+ await expect(async () =>
+ fetchRecentPosts({ where: { authorName: 'inexistent-author' } })
+ ).rejects.toEqual(new Error('No recent posts found.'));
+ });
+});
diff --git a/src/services/graphql/fetchers/posts/fetch-recent-posts.ts b/src/services/graphql/fetchers/posts/fetch-recent-posts.ts
index 12785d6..ddbea36 100644
--- a/src/services/graphql/fetchers/posts/fetch-recent-posts.ts
+++ b/src/services/graphql/fetchers/posts/fetch-recent-posts.ts
@@ -7,7 +7,7 @@ import type {
} from '../../../../types';
import { fetchGraphQL, getGraphQLUrl } from '../../../../utils/helpers';
-type RecentPostsResponse = {
+export type RecentPostsResponse = {
posts: Nullable<GraphQLConnection<RecentWPPost>>;
};
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);
+ });
+});