aboutsummaryrefslogtreecommitdiffstats
path: root/tests/msw/handlers/forms/create-comment.handler.ts
blob: d4c60030f270cfa66d207199465cdf568291ce30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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<CreateCommentResponse>;

  return HttpResponse.json({ data, errors });
});