aboutsummaryrefslogtreecommitdiffstats
path: root/tests/msw/handlers
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 /tests/msw/handlers
parentfb749e8befb2dcdc266c2e8b7ef7c9001947143a (diff)
test(services): add tests for comments list fetcher
Diffstat (limited to 'tests/msw/handlers')
-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
3 files changed, 44 insertions, 1 deletions
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];