aboutsummaryrefslogtreecommitdiffstats
path: root/tests/msw/handlers/repositories/repository.handler.ts
blob: 2c459c13edf57a1a5bda805f9ef9cd2cab2980e4 (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
import { type ExecutionResult, graphql } from 'graphql';
import { HttpResponse } from 'msw';
import type {
  FetchGithubRepoMetaInput,
  GithubRepositoryResponse,
} from '../../../../src/services/github';
import { githubRepos } from '../../../fixtures';
import { githubAPI } from '../../instances';
import { githubSchema } from '../../schema';

export const repositoryHandler = githubAPI.query<
  GithubRepositoryResponse,
  FetchGithubRepoMetaInput
>('GithubRepository', async ({ query, variables }) => {
  const pageParams = new URLSearchParams(window.location.search);
  const isError = pageParams.get('error') === 'true';

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

  const { data, errors } = (await graphql({
    schema: githubSchema,
    source: query,
    variableValues: variables,
    rootValue: {
      repository: githubRepos.find(
        (repo) => repo.owner === variables.owner && repo.name === variables.name
      ),
    },
  })) as ExecutionResult<GithubRepositoryResponse>;

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