diff options
Diffstat (limited to 'tests/msw/handlers')
| -rw-r--r-- | tests/msw/handlers/index.ts | 3 | ||||
| -rw-r--r-- | tests/msw/handlers/posts/index.ts | 15 | ||||
| -rw-r--r-- | tests/msw/handlers/posts/last-post-cursor.handler.ts | 33 | ||||
| -rw-r--r-- | tests/msw/handlers/posts/post.handler.ts | 21 | ||||
| -rw-r--r-- | tests/msw/handlers/posts/posts-count.handler.ts | 51 | ||||
| -rw-r--r-- | tests/msw/handlers/posts/posts-list.handler.ts | 50 | ||||
| -rw-r--r-- | tests/msw/handlers/posts/posts-slugs.handler.ts | 26 | ||||
| -rw-r--r-- | tests/msw/handlers/posts/recent-posts.handler.ts | 50 |
8 files changed, 249 insertions, 0 deletions
diff --git a/tests/msw/handlers/index.ts b/tests/msw/handlers/index.ts new file mode 100644 index 0000000..decbc9f --- /dev/null +++ b/tests/msw/handlers/index.ts @@ -0,0 +1,3 @@ +import { postsHandlers } from './posts'; + +export const handlers = [...postsHandlers]; diff --git a/tests/msw/handlers/posts/index.ts b/tests/msw/handlers/posts/index.ts new file mode 100644 index 0000000..3a49e13 --- /dev/null +++ b/tests/msw/handlers/posts/index.ts @@ -0,0 +1,15 @@ +import { lastPostCursorHandler } from './last-post-cursor.handler'; +import { postHandler } from './post.handler'; +import { postsCountHandler } from './posts-count.handler'; +import { postsListHandler } from './posts-list.handler'; +import { postsSlugsHandler } from './posts-slugs.handler'; +import { recentPostsHandler } from './recent-posts.handler'; + +export const postsHandlers = [ + lastPostCursorHandler, + postHandler, + postsCountHandler, + postsListHandler, + postsSlugsHandler, + recentPostsHandler, +]; 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<LastPostCursorResponse>; + + return HttpResponse.json({ data, errors }); +}); diff --git a/tests/msw/handlers/posts/post.handler.ts b/tests/msw/handlers/posts/post.handler.ts new file mode 100644 index 0000000..4abdfed --- /dev/null +++ b/tests/msw/handlers/posts/post.handler.ts @@ -0,0 +1,21 @@ +import { type ExecutionResult, graphql as executeGraphql } from 'graphql'; +import { HttpResponse, graphql } from 'msw'; +import type { PostResponse } from '../../../../src/services/graphql'; +import { wpPostsFixture } from '../../../fixtures'; +import { schema } from '../../schema'; + +export const postHandler = graphql.query<PostResponse, Record<'slug', string>>( + 'Post', + async ({ query, variables }) => { + const { data, errors } = (await executeGraphql({ + schema, + source: query, + variableValues: variables, + rootValue: { + post: wpPostsFixture.find((wpPost) => wpPost.slug === variables.slug), + }, + })) as ExecutionResult<PostResponse>; + + return HttpResponse.json({ data, errors }); + } +); diff --git a/tests/msw/handlers/posts/posts-count.handler.ts b/tests/msw/handlers/posts/posts-count.handler.ts new file mode 100644 index 0000000..95fa105 --- /dev/null +++ b/tests/msw/handlers/posts/posts-count.handler.ts @@ -0,0 +1,51 @@ +import { type ExecutionResult, graphql as executeGraphql } from 'graphql'; +import { HttpResponse, graphql } from 'msw'; +import type { PostsCountResponse } from '../../../../src/services/graphql'; +import type { GraphQLPostWhere } from '../../../../src/types'; +import { wpPostsFixture } from '../../../fixtures'; +import { getConnection } from '../../../utils/graphql'; +import { schema } from '../../schema'; + +export const postsCountHandler = graphql.query< + PostsCountResponse, + GraphQLPostWhere +>('PostsCount', 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({ authorName, search, title }: typeof variables) { + 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: null, + data: filteredPosts, + first: undefined, + }); + }, + }, + })) as ExecutionResult<PostsCountResponse>; + + return HttpResponse.json({ data, errors }); +}); 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 }); +}); diff --git a/tests/msw/handlers/posts/posts-slugs.handler.ts b/tests/msw/handlers/posts/posts-slugs.handler.ts new file mode 100644 index 0000000..9aadddb --- /dev/null +++ b/tests/msw/handlers/posts/posts-slugs.handler.ts @@ -0,0 +1,26 @@ +import { type ExecutionResult, graphql as executeGraphql } from 'graphql'; +import { HttpResponse, graphql } from 'msw'; +import type { PostsSlugsResponse } from '../../../../src/services/graphql'; +import { wpPostsFixture } from '../../../fixtures'; +import { schema } from '../../schema'; + +export const postsSlugsHandler = graphql.query< + PostsSlugsResponse, + Record<'first', number> +>('PostsSlugs', 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: { nodes: wpPostsFixture }, + }, + })) as ExecutionResult<PostsSlugsResponse>; + + return HttpResponse.json({ data, errors }); +}); diff --git a/tests/msw/handlers/posts/recent-posts.handler.ts b/tests/msw/handlers/posts/recent-posts.handler.ts new file mode 100644 index 0000000..34e0efb --- /dev/null +++ b/tests/msw/handlers/posts/recent-posts.handler.ts @@ -0,0 +1,50 @@ +import { type ExecutionResult, graphql as executeGraphql } from 'graphql'; +import { HttpResponse, graphql } from 'msw'; +import type { + FetchPostsListInput, + RecentPostsResponse, +} from '../../../../src/services/graphql'; +import { wpPostsFixture } from '../../../fixtures'; +import { getConnection } from '../../../utils/graphql'; +import { schema } from '../../schema'; + +export const recentPostsHandler = graphql.query< + RecentPostsResponse, + FetchPostsListInput +>('RecentPosts', 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<RecentPostsResponse>; + + return HttpResponse.json({ data, errors }); +}); |
