From 1c20e06da5a9817c15c80ca5a25cfacf8eeb0485 Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Mon, 11 Dec 2023 18:47:59 +0100 Subject: test(services): add tests for createComment mutation * add Jest test * add a Cypress test in article pages spec --- tests/msw/handlers/forms/create-comment.handler.ts | 49 ++++++++++++++++++++++ tests/msw/handlers/forms/index.ts | 3 +- 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 tests/msw/handlers/forms/create-comment.handler.ts (limited to 'tests/msw/handlers/forms') diff --git a/tests/msw/handlers/forms/create-comment.handler.ts b/tests/msw/handlers/forms/create-comment.handler.ts new file mode 100644 index 0000000..d4c6003 --- /dev/null +++ b/tests/msw/handlers/forms/create-comment.handler.ts @@ -0,0 +1,49 @@ +import { type ExecutionResult, graphql } from 'graphql'; +import { HttpResponse } from 'msw'; +import type { + CreateCommentInput, + CreateCommentPayload, + CreateCommentResponse, +} from '../../../../src/services/graphql'; +import { wordpressAPI } from '../../instances'; +import { schema } from '../../schema'; + +export const createCommentHandler = wordpressAPI.mutation< + CreateCommentResponse, + Record<'input', CreateCommentInput> +>('CreateComment', async ({ query, variables }) => { + const pageParams = new URLSearchParams(window.location.search); + const isError = pageParams.get('error') === 'true'; + + if (isError) + return HttpResponse.json({ + data: { + createComment: { + clientMutationId: null, + comment: null, + success: false, + }, + }, + }); + + const { data, errors } = (await graphql({ + schema, + source: query, + variableValues: variables, + rootValue: { + createComment({ input }: typeof variables): CreateCommentPayload { + const { clientMutationId } = input; + + return { + clientMutationId, + comment: { + approved: true, + }, + success: true, + }; + }, + }, + })) as ExecutionResult; + + return HttpResponse.json({ data, errors }); +}); diff --git a/tests/msw/handlers/forms/index.ts b/tests/msw/handlers/forms/index.ts index ce1e5b3..64be29b 100644 --- a/tests/msw/handlers/forms/index.ts +++ b/tests/msw/handlers/forms/index.ts @@ -1,3 +1,4 @@ +import { createCommentHandler } from './create-comment.handler'; import { sendEmailHandler } from './send-email.handler'; -export const formsHandlers = [sendEmailHandler]; +export const formsHandlers = [createCommentHandler, sendEmailHandler]; -- cgit v1.2.3