diff options
Diffstat (limited to 'tests/msw/handlers/posts/posts-slugs.handler.ts')
| -rw-r--r-- | tests/msw/handlers/posts/posts-slugs.handler.ts | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/tests/msw/handlers/posts/posts-slugs.handler.ts b/tests/msw/handlers/posts/posts-slugs.handler.ts new file mode 100644 index 0000000..9aadddb --- /dev/null +++ b/tests/msw/handlers/posts/posts-slugs.handler.ts @@ -0,0 +1,26 @@ +import { type ExecutionResult, graphql as executeGraphql } from 'graphql'; +import { HttpResponse, graphql } from 'msw'; +import type { PostsSlugsResponse } from '../../../../src/services/graphql'; +import { wpPostsFixture } from '../../../fixtures'; +import { schema } from '../../schema'; + +export const postsSlugsHandler = graphql.query< + PostsSlugsResponse, + Record<'first', number> +>('PostsSlugs', async ({ query, variables }) => { + const pageParams = new URLSearchParams(window.location.search); + const isError = pageParams.get('error') === 'true'; + + if (isError) return HttpResponse.json({ data: { posts: null } }); + + const { data, errors } = (await executeGraphql({ + schema, + source: query, + variableValues: variables, + rootValue: { + posts: { nodes: wpPostsFixture }, + }, + })) as ExecutionResult<PostsSlugsResponse>; + + return HttpResponse.json({ data, errors }); +}); |
