From 93db24b7f7650abac1bb7095026e3a1f367b0c0a Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Mon, 11 Dec 2023 17:52:38 +0100 Subject: refactor(pages): refine Contact page * remove next/router dependency * remove pageTitle since it is defined in MDX * reduce statements by grouping messages * mock response with MSW and add test for sendEmail --- tests/msw/handlers/forms/send-email.handler.ts | 53 ++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 tests/msw/handlers/forms/send-email.handler.ts (limited to 'tests/msw/handlers/forms/send-email.handler.ts') diff --git a/tests/msw/handlers/forms/send-email.handler.ts b/tests/msw/handlers/forms/send-email.handler.ts new file mode 100644 index 0000000..8917330 --- /dev/null +++ b/tests/msw/handlers/forms/send-email.handler.ts @@ -0,0 +1,53 @@ +import { type ExecutionResult, graphql } from 'graphql'; +import { HttpResponse } from 'msw'; +import type { + SendEmail, + SendEmailInput, + SendEmailResponse, +} from '../../../../src/services/graphql'; +import { CONFIG } from '../../../../src/utils/config'; +import { wordpressAPI } from '../../instances'; +import { schema } from '../../schema'; + +export const sendEmailHandler = wordpressAPI.mutation< + SendEmailResponse, + Record<'input', SendEmailInput> +>('SendEmail', async ({ query, variables }) => { + const pageParams = new URLSearchParams(window.location.search); + const isError = pageParams.get('error') === 'true'; + + if (isError) + return HttpResponse.json({ + data: { + sendEmail: { + clientMutationId: null, + message: 'Not allowed.', + origin: CONFIG.url, + replyTo: '', + sent: false, + }, + }, + }); + + const { data, errors } = (await graphql({ + schema, + source: query, + variableValues: variables, + rootValue: { + sendEmail({ input }: typeof variables): SendEmail { + const { body, clientMutationId, replyTo, subject } = input; + const message = `Object: ${subject}\n\n${body}`; + + return { + clientMutationId, + message, + origin: CONFIG.url, + replyTo, + sent: replyTo.includes('@'), + }; + }, + }, + })) as ExecutionResult; + + return HttpResponse.json({ data, errors }); +}); -- cgit v1.2.3