aboutsummaryrefslogtreecommitdiffstats
path: root/tests/msw/handlers/topics/topics-slugs.handler.ts
blob: 779a384e568a8eed0c3e5ec0f5f9f3a02c74bf85 (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
import { type ExecutionResult, graphql } from 'graphql';
import { HttpResponse } from 'msw';
import type { TopicsSlugsResponse } from '../../../../src/services/graphql';
import { wpTopicsFixture } from '../../../fixtures';
import { wordpressAPI } from '../../instances';
import { schema } from '../../schema';

export const topicsSlugsHandler = wordpressAPI.query<
  TopicsSlugsResponse,
  Record<'first', number>
>('TopicsSlugs', async ({ query, variables }) => {
  const pageParams = new URLSearchParams(window.location.search);
  const isError = pageParams.get('error') === 'true';

  if (isError) return HttpResponse.json({ data: { topics: null } });

  const { data, errors } = (await graphql({
    schema,
    source: query,
    variableValues: variables,
    rootValue: {
      topics: { nodes: wpTopicsFixture },
    },
  })) as ExecutionResult<TopicsSlugsResponse>;

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