diff options
Diffstat (limited to 'tests/msw/handlers/thematics/thematics-count.handler.ts')
| -rw-r--r-- | tests/msw/handlers/thematics/thematics-count.handler.ts | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/tests/msw/handlers/thematics/thematics-count.handler.ts b/tests/msw/handlers/thematics/thematics-count.handler.ts new file mode 100644 index 0000000..4bcdf2d --- /dev/null +++ b/tests/msw/handlers/thematics/thematics-count.handler.ts @@ -0,0 +1,45 @@ +import { type ExecutionResult, graphql as executeGraphql } from 'graphql'; +import { HttpResponse, graphql } from 'msw'; +import type { ThematicsCountResponse } from '../../../../src/services/graphql'; +import type { GraphQLPostWhere } from '../../../../src/types'; +import { wpThematicsFixture } from '../../../fixtures'; +import { getConnection } from '../../../utils/graphql'; +import { schema } from '../../schema'; + +export const thematicsCountHandler = graphql.query< + ThematicsCountResponse, + GraphQLPostWhere +>('ThematicsCount', 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({ search, title }: typeof variables) { + const filteredThematicsByTitle = title + ? wpThematicsFixture.filter((thematic) => + thematic.title.includes(title) + ) + : wpThematicsFixture; + const filteredThematics = search + ? filteredThematicsByTitle.filter((thematic) => + thematic.title.includes(search) + ) + : filteredThematicsByTitle; + + return getConnection({ + after: null, + data: filteredThematics, + first: undefined, + }); + }, + }, + })) as ExecutionResult<ThematicsCountResponse>; + + return HttpResponse.json({ data, errors }); +}); |
