diff options
| author | Armand Philippot <git@armandphilippot.com> | 2023-11-27 19:40:40 +0100 |
|---|---|---|
| committer | Armand Philippot <git@armandphilippot.com> | 2023-11-28 18:04:24 +0100 |
| commit | fb749e8befb2dcdc266c2e8b7ef7c9001947143a (patch) | |
| tree | 80ae62cd0a699d4feb14fe2a239b4fe317a400a0 /src/services | |
| parent | ab81df7f3d317281a05caec18e2cfd89dc26bc7a (diff) | |
test(services): add tests for posts fetchers
Diffstat (limited to 'src/services')
12 files changed, 159 insertions, 12 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>>; }; |
