From 29a1dec4de0aa7ba64ef068a83b1b8589fbc3ad0 Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Tue, 28 Nov 2023 17:49:26 +0100 Subject: 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 --- tests/msw/handlers/topics/topics-list.handler.ts | 42 ++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 tests/msw/handlers/topics/topics-list.handler.ts (limited to 'tests/msw/handlers/topics/topics-list.handler.ts') diff --git a/tests/msw/handlers/topics/topics-list.handler.ts b/tests/msw/handlers/topics/topics-list.handler.ts new file mode 100644 index 0000000..5e3e31a --- /dev/null +++ b/tests/msw/handlers/topics/topics-list.handler.ts @@ -0,0 +1,42 @@ +import { type ExecutionResult, graphql as executeGraphql } from 'graphql'; +import { HttpResponse, graphql } from 'msw'; +import type { + FetchTopicsListInput, + TopicsListResponse, +} from '../../../../src/services/graphql'; +import { wpTopicsFixture } from '../../../fixtures'; +import { getConnection } from '../../../utils/graphql'; +import { schema } from '../../schema'; + +export const topicsListHandler = graphql.query< + TopicsListResponse, + FetchTopicsListInput +>('TopicsList', 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({ after, first, where }: typeof variables) { + const { search, title } = where ?? {}; + 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, data: filteredTopics, first }); + }, + }, + })) as ExecutionResult; + + return HttpResponse.json({ data, errors }); +}); -- cgit v1.2.3