From fb749e8befb2dcdc266c2e8b7ef7c9001947143a Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Mon, 27 Nov 2023 19:40:40 +0100 Subject: test(services): add tests for posts fetchers --- .../msw/handlers/posts/last-post-cursor.handler.ts | 33 ++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 tests/msw/handlers/posts/last-post-cursor.handler.ts (limited to 'tests/msw/handlers/posts/last-post-cursor.handler.ts') diff --git a/tests/msw/handlers/posts/last-post-cursor.handler.ts b/tests/msw/handlers/posts/last-post-cursor.handler.ts new file mode 100644 index 0000000..2f4b648 --- /dev/null +++ b/tests/msw/handlers/posts/last-post-cursor.handler.ts @@ -0,0 +1,33 @@ +import { type ExecutionResult, graphql as executeGraphql } from 'graphql'; +import { HttpResponse, graphql } from 'msw'; +import type { LastPostCursorResponse } from '../../../../src/services/graphql'; +import { wpPostsFixture } from '../../../fixtures'; +import { getConnection } from '../../../utils/graphql'; +import { schema } from '../../schema'; + +export const lastPostCursorHandler = graphql.query< + LastPostCursorResponse, + Record<'first', number> +>('LastPostCursor', 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({ first }: typeof variables) { + return getConnection({ + after: null, + data: wpPostsFixture, + first, + }); + }, + }, + })) as ExecutionResult; + + return HttpResponse.json({ data, errors }); +}); -- cgit v1.2.3