diff options
| author | Armand Philippot <git@armandphilippot.com> | 2023-12-11 17:52:38 +0100 |
|---|---|---|
| committer | Armand Philippot <git@armandphilippot.com> | 2023-12-11 17:52:38 +0100 |
| commit | 93db24b7f7650abac1bb7095026e3a1f367b0c0a (patch) | |
| tree | c6efd8669d333941494e573d2468a4fb6603b134 /tests/msw/handlers | |
| parent | cd2cb5748be9e9c479d9802dd3897de1cd1cbd9f (diff) | |
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
Diffstat (limited to 'tests/msw/handlers')
| -rw-r--r-- | tests/msw/handlers/forms/index.ts | 3 | ||||
| -rw-r--r-- | tests/msw/handlers/forms/send-email.handler.ts | 53 | ||||
| -rw-r--r-- | tests/msw/handlers/index.ts | 2 |
3 files changed, 58 insertions, 0 deletions
diff --git a/tests/msw/handlers/forms/index.ts b/tests/msw/handlers/forms/index.ts new file mode 100644 index 0000000..ce1e5b3 --- /dev/null +++ b/tests/msw/handlers/forms/index.ts @@ -0,0 +1,3 @@ +import { sendEmailHandler } from './send-email.handler'; + +export const formsHandlers = [sendEmailHandler]; 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<SendEmailResponse>; + + return HttpResponse.json({ data, errors }); +}); diff --git a/tests/msw/handlers/index.ts b/tests/msw/handlers/index.ts index bfdeb95..a41733e 100644 --- a/tests/msw/handlers/index.ts +++ b/tests/msw/handlers/index.ts @@ -1,4 +1,5 @@ import { commentsHandlers } from './comments'; +import { formsHandlers } from './forms'; import { postsHandlers } from './posts'; import { repositoriesHandlers } from './repositories'; import { thematicsHandlers } from './thematics'; @@ -6,6 +7,7 @@ import { topicsHandlers } from './topics'; export const handlers = [ ...commentsHandlers, + ...formsHandlers, ...postsHandlers, ...repositoriesHandlers, ...thematicsHandlers, |
