diff options
| author | Armand Philippot <git@armandphilippot.com> | 2023-11-27 19:40:40 +0100 |
|---|---|---|
| committer | Armand Philippot <git@armandphilippot.com> | 2023-11-28 18:04:24 +0100 |
| commit | fb749e8befb2dcdc266c2e8b7ef7c9001947143a (patch) | |
| tree | 80ae62cd0a699d4feb14fe2a239b4fe317a400a0 /tests/msw/handlers/posts/posts-list.handler.ts | |
| parent | ab81df7f3d317281a05caec18e2cfd89dc26bc7a (diff) | |
test(services): add tests for posts fetchers
Diffstat (limited to 'tests/msw/handlers/posts/posts-list.handler.ts')
| -rw-r--r-- | tests/msw/handlers/posts/posts-list.handler.ts | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/tests/msw/handlers/posts/posts-list.handler.ts b/tests/msw/handlers/posts/posts-list.handler.ts new file mode 100644 index 0000000..7f8daf6 --- /dev/null +++ b/tests/msw/handlers/posts/posts-list.handler.ts @@ -0,0 +1,50 @@ +import { type ExecutionResult, graphql as executeGraphql } from 'graphql'; +import { HttpResponse, graphql } from 'msw'; +import type { + FetchPostsListInput, + PostsListResponse, +} from '../../../../src/services/graphql'; +import { wpPostsFixture } from '../../../fixtures'; +import { getConnection } from '../../../utils/graphql'; +import { schema } from '../../schema'; + +export const postsListHandler = graphql.query< + PostsListResponse, + FetchPostsListInput +>('PostsList', 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({ after, first, where }: typeof variables) { + const { authorName, search, title } = where ?? {}; + const filteredPostsByAuthor = authorName + ? wpPostsFixture.filter((post) => + post.author.node.name.includes(authorName) + ) + : wpPostsFixture; + const filteredPostsByTitle = title + ? filteredPostsByAuthor.filter((post) => post.title.includes(title)) + : filteredPostsByAuthor; + const filteredPosts = search + ? filteredPostsByTitle.filter( + (post) => + post.title.includes(search) || + post.contentParts.afterMore.includes(search) || + post.contentParts.beforeMore.includes(search) + ) + : filteredPostsByTitle; + + return getConnection({ after, data: filteredPosts, first }); + }, + }, + })) as ExecutionResult<PostsListResponse>; + + return HttpResponse.json({ data, errors }); +}); |
