aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorArmand Philippot <git@armandphilippot.com>2023-11-28 15:48:25 +0100
committerArmand Philippot <git@armandphilippot.com>2023-11-28 18:04:24 +0100
commit180134883b281883246fc9909b1d494363861144 (patch)
treebf108c3c2961616c9af808b9810345fc6cd23ffe
parentfb749e8befb2dcdc266c2e8b7ef7c9001947143a (diff)
test(services): add tests for comments list fetcher
-rw-r--r--src/services/graphql/fetchers/comments/fetch-comments.test.ts26
-rw-r--r--src/services/graphql/fetchers/comments/fetch-comments.ts2
-rw-r--r--tests/fixtures/index.ts1
-rw-r--r--tests/fixtures/wp-comments.fixture.ts104
-rw-r--r--tests/msw/handlers/comments/comments-list.handler.ts39
-rw-r--r--tests/msw/handlers/comments/index.ts3
-rw-r--r--tests/msw/handlers/index.ts3
-rw-r--r--tests/msw/schema/types/author.types.ts15
-rw-r--r--tests/msw/schema/types/comment.types.ts74
-rw-r--r--tests/msw/schema/types/common.types.ts13
-rw-r--r--tests/msw/schema/types/index.ts9
11 files changed, 273 insertions, 16 deletions
diff --git a/src/services/graphql/fetchers/comments/fetch-comments.test.ts b/src/services/graphql/fetchers/comments/fetch-comments.test.ts
new file mode 100644
index 0000000..15cfb6d
--- /dev/null
+++ b/src/services/graphql/fetchers/comments/fetch-comments.test.ts
@@ -0,0 +1,26 @@
+import { afterEach, describe, expect, it } from '@jest/globals';
+import { wpCommentsFixture } from '../../../../../tests/fixtures';
+import { fetchCommentsList } from './fetch-comments';
+
+describe('fetch-comments', () => {
+ afterEach(() => {
+ window.history.replaceState({}, '', '/');
+ });
+
+ it('returns the WordPress comments using GraphQL', async () => {
+ const result = await fetchCommentsList({});
+
+ expect.assertions(1);
+
+ expect(result.length).toBe(wpCommentsFixture.length);
+ });
+
+ it('rejects with an error when no comments are found', async () => {
+ window.history.replaceState({}, '', '/?error=true');
+ expect.assertions(1);
+
+ await expect(async () =>
+ fetchCommentsList({ where: { contentName: '/inexistent-slug' } })
+ ).rejects.toEqual(new Error('No comments found.'));
+ });
+});
diff --git a/src/services/graphql/fetchers/comments/fetch-comments.ts b/src/services/graphql/fetchers/comments/fetch-comments.ts
index 85ae6c1..3b52863 100644
--- a/src/services/graphql/fetchers/comments/fetch-comments.ts
+++ b/src/services/graphql/fetchers/comments/fetch-comments.ts
@@ -7,7 +7,7 @@ import type {
} from '../../../../types';
import { fetchGraphQL, getGraphQLUrl } from '../../../../utils/helpers';
-type CommentsListResponse = {
+export type CommentsListResponse = {
comments: Nullable<GraphQLNodes<WPComment>>;
};
diff --git a/tests/fixtures/index.ts b/tests/fixtures/index.ts
index c2e1c04..0c624c2 100644
--- a/tests/fixtures/index.ts
+++ b/tests/fixtures/index.ts
@@ -1 +1,2 @@
+export * from './wp-comments.fixture';
export * from './wp-posts.fixture';
diff --git a/tests/fixtures/wp-comments.fixture.ts b/tests/fixtures/wp-comments.fixture.ts
new file mode 100644
index 0000000..3e0a1bb
--- /dev/null
+++ b/tests/fixtures/wp-comments.fixture.ts
@@ -0,0 +1,104 @@
+import type { WPComment } from '../../src/types';
+
+export const wpCommentsFixture: WPComment[] = [
+ {
+ approved: true,
+ author: {
+ node: {
+ avatar: {
+ height: 96,
+ url: 'https://cloudflare-ipfs.com/ipfs/Qmd3W5DuhgHirLHGVixi6V76LhCkZUz6pnFt5AJBiyvHye/avatar/1225.jpg',
+ width: 96,
+ },
+ name: 'London70',
+ url: null,
+ },
+ },
+ content:
+ 'Aliquid dolore molestiae eaque. Sint velit consectetur nesciunt est maxime. Ut qui harum ut quae iure. Voluptatem eius aut tempore repudiandae corrupti dignissimos.',
+ databaseId: 1,
+ date: '2022-04-21',
+ parentDatabaseId: 0,
+ status: 'APPROVE',
+ },
+ {
+ approved: true,
+ author: {
+ node: {
+ avatar: {
+ height: 96,
+ url: 'https://cloudflare-ipfs.com/ipfs/Qmd3W5DuhgHirLHGVixi6V76LhCkZUz6pnFt5AJBiyvHye/avatar/168.jpg',
+ width: 96,
+ },
+ name: 'Wilburn.Tillman5',
+ url: null,
+ },
+ },
+ content:
+ 'Sit labore quia excepturi repellat minus exercitationem. Nihil veniam sed voluptas a doloremque voluptatum. Quia accusantium totam accusamus quia qui dolorem autem ut. Et reprehenderit voluptates.',
+ databaseId: 2,
+ date: '2022-05-11',
+ parentDatabaseId: 0,
+ status: 'APPROVE',
+ },
+ {
+ approved: true,
+ author: {
+ node: {
+ avatar: {
+ height: 96,
+ url: 'https://cloudflare-ipfs.com/ipfs/Qmd3W5DuhgHirLHGVixi6V76LhCkZUz6pnFt5AJBiyvHye/avatar/419.jpg',
+ width: 96,
+ },
+ name: 'Addison27',
+ url: 'https://example.test',
+ },
+ },
+ content:
+ 'Cupiditate beatae ea vel dolore porro magnam in. Nostrum eum corrupti. Exercitationem cupiditate deserunt sunt quia quia ipsum placeat sit. Est maiores dolor qui. A tempora repellendus cumque voluptatem omnis beatae dolor nobis. Quis aut eos.',
+ databaseId: 3,
+ date: '2022-05-16',
+ parentDatabaseId: 1,
+ status: 'APPROVE',
+ },
+ {
+ approved: true,
+ author: {
+ node: {
+ avatar: {
+ height: 96,
+ url: 'https://cloudflare-ipfs.com/ipfs/Qmd3W5DuhgHirLHGVixi6V76LhCkZUz6pnFt5AJBiyvHye/avatar/810.jpg',
+ width: 96,
+ },
+ name: 'Mariana20',
+ url: null,
+ },
+ },
+ content:
+ 'Placeat iste unde. Et ad rem aut dolor placeat sunt aliquid aliquam. Dolor repellendus quis alias consequatur nihil.',
+ databaseId: 4,
+ date: '2022-05-17',
+ parentDatabaseId: 0,
+ status: 'APPROVE',
+ },
+ {
+ approved: false,
+ author: {
+ node: {
+ avatar: {
+ height: 96,
+ url: 'https://cloudflare-ipfs.com/ipfs/Qmd3W5DuhgHirLHGVixi6V76LhCkZUz6pnFt5AJBiyvHye/avatar/1202.jpg',
+ width: 96,
+ },
+ name: 'Willow8',
+ url: null,
+ },
+ },
+ content:
+ 'Debitis quia fuga itaque voluptatem consequatur dolores. Praesentium quibusdam non sequi. Inventore voluptatem qui ad. Aperiam labore maxime qui necessitatibus. Sed molestias veritatis et.',
+ databaseId: 4,
+ date: '2022-06-02',
+ parentDatabaseId: 2,
+ status: 'HOLD',
+ },
+];
diff --git a/tests/msw/handlers/comments/comments-list.handler.ts b/tests/msw/handlers/comments/comments-list.handler.ts
new file mode 100644
index 0000000..e815a0e
--- /dev/null
+++ b/tests/msw/handlers/comments/comments-list.handler.ts
@@ -0,0 +1,39 @@
+import { type ExecutionResult, graphql as executeGraphql } from 'graphql';
+import { HttpResponse, graphql } from 'msw';
+import type {
+ FetchCommentsListInput,
+ CommentsListResponse,
+} from '../../../../src/services/graphql';
+import { wpCommentsFixture } from '../../../fixtures';
+import { schema } from '../../schema';
+
+export const commentsListHandler = graphql.query<
+ CommentsListResponse,
+ FetchCommentsListInput
+>('CommentsList', async ({ query, variables }) => {
+ const pageParams = new URLSearchParams(window.location.search);
+ const isError = pageParams.get('error') === 'true';
+
+ if (isError) return HttpResponse.json({ data: { comments: null } });
+
+ const { data, errors } = (await executeGraphql({
+ schema,
+ source: query,
+ variableValues: variables,
+ rootValue: {
+ comments({ first, where }: typeof variables) {
+ const { status: commentStatus } = where ?? {};
+ const filteredComments = commentStatus
+ ? wpCommentsFixture.filter(
+ (comment) => comment.status === commentStatus
+ )
+ : wpCommentsFixture;
+ const comments = filteredComments.slice(0, first);
+
+ return { nodes: comments };
+ },
+ },
+ })) as ExecutionResult<CommentsListResponse>;
+
+ return HttpResponse.json({ data, errors });
+});
diff --git a/tests/msw/handlers/comments/index.ts b/tests/msw/handlers/comments/index.ts
new file mode 100644
index 0000000..349355b
--- /dev/null
+++ b/tests/msw/handlers/comments/index.ts
@@ -0,0 +1,3 @@
+import { commentsListHandler } from './comments-list.handler';
+
+export const commentsHandlers = [commentsListHandler];
diff --git a/tests/msw/handlers/index.ts b/tests/msw/handlers/index.ts
index decbc9f..5f331e7 100644
--- a/tests/msw/handlers/index.ts
+++ b/tests/msw/handlers/index.ts
@@ -1,3 +1,4 @@
+import { commentsHandlers } from './comments';
import { postsHandlers } from './posts';
-export const handlers = [...postsHandlers];
+export const handlers = [...commentsHandlers, ...postsHandlers];
diff --git a/tests/msw/schema/types/author.types.ts b/tests/msw/schema/types/author.types.ts
index bbb50a4..53c0bb7 100644
--- a/tests/msw/schema/types/author.types.ts
+++ b/tests/msw/schema/types/author.types.ts
@@ -1,17 +1,4 @@
-export const authorTypes = `enum AvatarRatingEnum {
- G
- PG
- R
- X
-}
-
-type Avatar {
- height: Int
- url: String
- width: Int
-}
-
-type User {
+export const authorTypes = `type User {
avatar(
forceDefault: Boolean
rating: AvatarRatingEnum
diff --git a/tests/msw/schema/types/comment.types.ts b/tests/msw/schema/types/comment.types.ts
new file mode 100644
index 0000000..00cff77
--- /dev/null
+++ b/tests/msw/schema/types/comment.types.ts
@@ -0,0 +1,74 @@
+export const commentTypes = `enum CommentsConnectionOrderbyEnum {
+ COMMENT_AGENT
+ COMMENT_APPROVED
+ COMMENT_AUTHOR
+ COMMENT_AUTHOR_EMAIL
+ COMMENT_AUTHOR_IP
+ COMMENT_AUTHOR_URL
+ COMMENT_CONTENT
+ COMMENT_DATE
+ COMMENT_DATE_GMT
+ COMMENT_ID
+ COMMENT_IN
+ COMMENT_KARMA
+ COMMENT_PARENT
+ COMMENT_POST_ID
+ COMMENT_TYPE
+ USER_ID
+}
+
+type Commenter {
+ avatar: Avatar
+ name: String
+ url: String
+}
+
+type CommentToCommenterConnectionEdge {
+ cursor: String
+ node: Commenter!
+}
+
+enum CommentStatusEnum {
+ APPROVE
+ HOLD
+ SPAM
+ TRASH
+}
+
+type Comment {
+ approved: Boolean
+ author: CommentToCommenterConnectionEdge
+ content(format: PostObjectFieldFormatEnum): String
+ databaseId: Int!
+ date: String
+ parentDatabaseId: Int
+ status: CommentStatusEnum
+}
+
+input RootQueryToCommentConnectionWhereArgs {
+ contentId: ID
+ contentName: String
+ order: OrderEnum
+ orderby: CommentsConnectionOrderbyEnum
+ parent: Int
+ status: String
+}
+
+type RootQueryToCommentConnectionPageInfo {
+ endCursor: String
+ hasNextPage: Boolean!
+ hasPreviousPage: Boolean!
+ startCursor: String
+ total: Int
+}
+
+type RootQueryToCommentConnectionEdge {
+ cursor: String
+ node: Comment!
+}
+
+type RootQueryToCommentConnection {
+ edges: [RootQueryToCommentConnectionEdge!]!
+ nodes: [Comment!]!
+ pageInfo: RootQueryToCommentConnectionPageInfo!
+}`;
diff --git a/tests/msw/schema/types/common.types.ts b/tests/msw/schema/types/common.types.ts
index 23baeb3..03e066c 100644
--- a/tests/msw/schema/types/common.types.ts
+++ b/tests/msw/schema/types/common.types.ts
@@ -21,6 +21,19 @@ enum PostObjectsConnectionOrderbyEnum {
TITLE
}
+enum AvatarRatingEnum {
+ G
+ PG
+ R
+ X
+}
+
+type Avatar {
+ height: Int
+ url: String
+ width: Int
+}
+
type ContentPartsType {
afterMore(format: PostObjectFieldFormatEnum): String
beforeMore(format: PostObjectFieldFormatEnum): String
diff --git a/tests/msw/schema/types/index.ts b/tests/msw/schema/types/index.ts
index 163955b..c34cacd 100644
--- a/tests/msw/schema/types/index.ts
+++ b/tests/msw/schema/types/index.ts
@@ -1,4 +1,5 @@
import { authorTypes } from './author.types';
+import { commentTypes } from './comment.types';
import { commonTypes } from './common.types';
import { featuredImageTypes } from './featured-image.types';
import { postTypes } from './post.types';
@@ -6,6 +7,13 @@ import { thematicTypes } from './thematic.types';
import { topicTypes } from './topic.types';
const rootQueryType = `type Query {
+ comments(
+ after: String
+ before: String
+ first: Int
+ last: Int
+ where: RootQueryToCommentConnectionWhereArgs
+ ): RootQueryToCommentConnection
post(
asPreview: Boolean
id: ID!
@@ -22,6 +30,7 @@ const rootQueryType = `type Query {
export const types = [
authorTypes,
+ commentTypes,
commonTypes,
featuredImageTypes,
postTypes,