diff options
| author | Armand Philippot <git@armandphilippot.com> | 2023-12-06 18:20:54 +0100 |
|---|---|---|
| committer | Armand Philippot <git@armandphilippot.com> | 2023-12-07 19:12:11 +0100 |
| commit | b8eb008dd5927fb736e56699637f5f8549965eae (patch) | |
| tree | 648274babea3d3d09ed3e0f5f1fef013f94158fb /tests/msw/handlers/repositories | |
| parent | 802285872a2c57e7a5e130f32a2b45497d7687f1 (diff) | |
refactor(hooks): replace useGithubApi with useGithubRepoMeta
* use GraphQL API instead of REST (the inconvenient however is that we
now need an authorization token...)
* move fetcher in services
* add tests
* mock response using MSW
Diffstat (limited to 'tests/msw/handlers/repositories')
| -rw-r--r-- | tests/msw/handlers/repositories/index.ts | 3 | ||||
| -rw-r--r-- | tests/msw/handlers/repositories/repository.handler.ts | 32 |
2 files changed, 35 insertions, 0 deletions
diff --git a/tests/msw/handlers/repositories/index.ts b/tests/msw/handlers/repositories/index.ts new file mode 100644 index 0000000..b29a41f --- /dev/null +++ b/tests/msw/handlers/repositories/index.ts @@ -0,0 +1,3 @@ +import { repositoryHandler } from './repository.handler'; + +export const repositoriesHandlers = [repositoryHandler]; diff --git a/tests/msw/handlers/repositories/repository.handler.ts b/tests/msw/handlers/repositories/repository.handler.ts new file mode 100644 index 0000000..2c459c1 --- /dev/null +++ b/tests/msw/handlers/repositories/repository.handler.ts @@ -0,0 +1,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 }); +}); |
