diff options
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/fixtures/index.ts | 1 | ||||
| -rw-r--r-- | tests/fixtures/wp-posts.fixture.ts | 177 | ||||
| -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 | ||||
| -rw-r--r-- | tests/msw/index.ts | 3 | ||||
| -rw-r--r-- | tests/msw/schema/types/index.ts | 5 | ||||
| -rw-r--r-- | tests/msw/schema/types/post.types.ts | 8 |
13 files changed, 442 insertions, 1 deletions
diff --git a/tests/fixtures/index.ts b/tests/fixtures/index.ts new file mode 100644 index 0000000..c2e1c04 --- /dev/null +++ b/tests/fixtures/index.ts @@ -0,0 +1 @@ +export * from './wp-posts.fixture'; diff --git a/tests/fixtures/wp-posts.fixture.ts b/tests/fixtures/wp-posts.fixture.ts new file mode 100644 index 0000000..a1b1e4a --- /dev/null +++ b/tests/fixtures/wp-posts.fixture.ts @@ -0,0 +1,177 @@ +import type { WPPost } from '../../src/types'; + +export const wpPostsFixture: WPPost[] = [ + { + acfPosts: null, + author: { + node: { + name: 'Alisha.Dare45', + }, + }, + commentCount: 2, + contentParts: { + afterMore: + 'Velit rerum ea ex at esse sunt eum corporis. Autem sunt et. Earum consectetur alias enim officiis nesciunt.', + beforeMore: + 'Architecto quasi consequuntur. Ut sint perspiciatis dolor non iure et ut. Reiciendis id minus perferendis vero.', + }, + databaseId: 1, + date: '2022-11-02', + featuredImage: null, + info: { + wordsCount: 400, + }, + modified: '2022-11-02', + seo: { + metaDesc: 'Repudiandae autem numquam.', + title: 'similique atque corporis', + }, + slug: '/post-1', + title: 'Post 1', + }, + { + acfPosts: null, + author: { + node: { + name: 'Alisha.Dare45', + }, + }, + commentCount: null, + contentParts: { + afterMore: + 'Accusantium voluptatem ullam. Culpa et qui voluptatibus earum commodi expedita dolor. Aut suscipit corporis exercitationem tempora sapiente. Deleniti velit assumenda voluptatum et rem inventore soluta hic distinctio. Excepturi dolorum maxime voluptas. Ducimus et aut quia ex.', + beforeMore: + 'Et illo voluptates. Nihil sed error. Quas et similique incidunt quae in rem omnis. Odit distinctio magni et qui tempora pariatur eius perferendis.', + }, + databaseId: 2, + date: '2022-11-03', + featuredImage: { + node: { + altText: null, + mediaDetails: { + height: 480, + width: 640, + }, + sourceUrl: 'https://picsum.photos/640/480', + title: null, + }, + }, + info: { + wordsCount: 520, + }, + modified: '2022-11-04', + seo: { + metaDesc: 'Veritatis deleniti et cum modi id necessitatibus dolores.', + title: 'numquam nihil voluptas', + }, + slug: '/post-2', + title: 'Post 2', + }, + { + acfPosts: { + postsInThematic: [ + { + databaseId: 2, + slug: '/thematic-1', + title: 'Thematic 1', + }, + ], + postsInTopic: null, + }, + author: { + node: { + name: 'Alisha.Dare45', + }, + }, + commentCount: 1, + contentParts: { + afterMore: + 'Animi facere velit in voluptate facere non repellat magni. Aut est dicta et mollitia blanditiis dolores. Est minima voluptatibus eveniet.', + beforeMore: + 'Consequatur odit voluptatum est est accusantium. Nobis aspernatur qui. Magni ut facere laudantium voluptatem autem quia voluptas.', + }, + databaseId: 3, + date: '2022-11-06', + featuredImage: { + node: { + altText: 'qui officia est', + mediaDetails: { + height: 480, + width: 640, + }, + sourceUrl: 'https://picsum.photos/640/480', + title: null, + }, + }, + info: { + wordsCount: 320, + }, + modified: '2022-11-06', + seo: { + metaDesc: + 'Ut deserunt qui reprehenderit ut veritatis consequatur quia corporis.', + title: 'laboriosam incidunt enim', + }, + slug: '/post-3', + title: 'Post 3', + }, + { + acfPosts: { + postsInThematic: [ + { + databaseId: 3, + slug: '/thematic-2', + title: 'Thematic 2', + }, + { + databaseId: 4, + slug: '/thematic-3', + title: 'Thematic 3', + }, + ], + postsInTopic: [ + { + databaseId: 2, + featuredImage: null, + slug: '/topic-1', + title: 'Topic 1', + }, + ], + }, + author: { + node: { + name: 'Alisha.Dare45', + }, + }, + commentCount: 5, + contentParts: { + afterMore: + 'Consequuntur inventore labore enim rerum ut. Ut et sit tempora provident. Aliquid nihil dolorem dolorum assumenda doloremque perspiciatis similique debitis voluptas.', + beforeMore: + 'Adipisci velit voluptatum unde consequatur amet. In aliquam animi qui non ut.', + }, + databaseId: 4, + date: '2022-11-08', + featuredImage: { + node: { + altText: 'est dolorum maxime', + mediaDetails: { + height: 480, + width: 640, + }, + sourceUrl: 'https://picsum.photos/640/480', + title: 'nostrum quisquam officiis', + }, + }, + info: { + wordsCount: 650, + }, + modified: '2022-11-11', + seo: { + metaDesc: 'In ipsam pariatur laboriosam aut alias molestiae.', + title: 'fugit et sunt', + }, + slug: '/post-4', + title: 'Post 4', + }, +]; 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 }); +}); diff --git a/tests/msw/index.ts b/tests/msw/index.ts index 997ae40..29504a4 100644 --- a/tests/msw/index.ts +++ b/tests/msw/index.ts @@ -1,3 +1,4 @@ import { setupServer } from 'msw/node'; +import { handlers } from './handlers'; -export const mswServer = setupServer(...[]); +export const mswServer = setupServer(...handlers); diff --git a/tests/msw/schema/types/index.ts b/tests/msw/schema/types/index.ts index ea749be..163955b 100644 --- a/tests/msw/schema/types/index.ts +++ b/tests/msw/schema/types/index.ts @@ -6,6 +6,11 @@ import { thematicTypes } from './thematic.types'; import { topicTypes } from './topic.types'; const rootQueryType = `type Query { + post( + asPreview: Boolean + id: ID! + idType: PostIdType + ): Post posts( after: String before: String diff --git a/tests/msw/schema/types/post.types.ts b/tests/msw/schema/types/post.types.ts index 04c4f37..11c514c 100644 --- a/tests/msw/schema/types/post.types.ts +++ b/tests/msw/schema/types/post.types.ts @@ -22,6 +22,13 @@ type Post { title(format: PostObjectFieldFormatEnum): String } +enum PostIdType { + DATABASE_ID + ID + SLUG + URI +} + enum PostStatusEnum { ACF_DISABLED AUTO_DRAFT @@ -66,6 +73,7 @@ type RootQueryToPostConnectionPageInfo { type RootQueryToPostConnection { edges: [RootQueryToPostConnectionEdge!]! + nodes: [Post!]! pageInfo: RootQueryToPostConnectionPageInfo! }`; |
