From 180134883b281883246fc9909b1d494363861144 Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Tue, 28 Nov 2023 15:48:25 +0100 Subject: test(services): add tests for comments list fetcher --- .../msw/handlers/comments/comments-list.handler.ts | 39 ++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 tests/msw/handlers/comments/comments-list.handler.ts (limited to 'tests/msw/handlers/comments/comments-list.handler.ts') 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; + + return HttpResponse.json({ data, errors }); +}); -- cgit v1.2.3