diff options
| author | Armand Philippot <git@armandphilippot.com> | 2023-11-28 17:49:26 +0100 |
|---|---|---|
| committer | Armand Philippot <git@armandphilippot.com> | 2023-11-28 18:04:24 +0100 |
| commit | 29a1dec4de0aa7ba64ef068a83b1b8589fbc3ad0 (patch) | |
| tree | 8db871542e878e9fdf589bccd1be7b5ed1378f72 /tests/msw/handlers/topics/topics-count.handler.ts | |
| parent | f564d181bc428e25a02bf1d98c4449a6b3eb8e9e (diff) | |
fix(services,types): make queries and types coherent for Topic
* some nodes was missing in topicQuery
* a node was mispelled in topicsListQuery
* add tests for all topics fetchers
Diffstat (limited to 'tests/msw/handlers/topics/topics-count.handler.ts')
| -rw-r--r-- | tests/msw/handlers/topics/topics-count.handler.ts | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/tests/msw/handlers/topics/topics-count.handler.ts b/tests/msw/handlers/topics/topics-count.handler.ts new file mode 100644 index 0000000..7e3dab9 --- /dev/null +++ b/tests/msw/handlers/topics/topics-count.handler.ts @@ -0,0 +1,43 @@ +import { type ExecutionResult, graphql as executeGraphql } from 'graphql'; +import { HttpResponse, graphql } from 'msw'; +import type { TopicsCountResponse } from '../../../../src/services/graphql'; +import type { GraphQLPostWhere } from '../../../../src/types'; +import { wpTopicsFixture } from '../../../fixtures'; +import { getConnection } from '../../../utils/graphql'; +import { schema } from '../../schema'; + +export const topicsCountHandler = graphql.query< + TopicsCountResponse, + GraphQLPostWhere +>('TopicsCount', async ({ query, variables }) => { + const pageParams = new URLSearchParams(window.location.search); + const isError = pageParams.get('error') === 'true'; + + if (isError) return HttpResponse.json({ data: { topics: null } }); + + const { data, errors } = (await executeGraphql({ + schema, + source: query, + variableValues: variables, + rootValue: { + topics({ search, title }: typeof variables) { + const filteredTopicsByTitle = title + ? wpTopicsFixture.filter((topic) => topic.title.includes(title)) + : wpTopicsFixture; + const filteredTopics = search + ? filteredTopicsByTitle.filter((topic) => + topic.title.includes(search) + ) + : filteredTopicsByTitle; + + return getConnection({ + after: null, + data: filteredTopics, + first: undefined, + }); + }, + }, + })) as ExecutionResult<TopicsCountResponse>; + + return HttpResponse.json({ data, errors }); +}); |
