aboutsummaryrefslogtreecommitdiffstats
path: root/tests/msw/handlers/thematics/thematics-slugs.handler.ts
blob: 77bbeda5358fc4a3f104f0a0b358c5086c5725ba (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 as executeGraphql } from 'graphql';
import { HttpResponse } from 'msw';
import type { ThematicsSlugsResponse } from '../../../../src/services/graphql';
import { wpThematicsFixture } from '../../../fixtures';
import { wordpressAPI } from '../../instances';
import { schema } from '../../schema';

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

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

  const { data, errors } = (await executeGraphql({
    schema,
    source: query,
    variableValues: variables,
    rootValue: {
      thematics: { nodes: wpThematicsFixture },
    },
  })) as ExecutionResult<ThematicsSlugsResponse>;

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