aboutsummaryrefslogtreecommitdiffstats
path: root/tests/msw
diff options
context:
space:
mode:
authorArmand Philippot <git@armandphilippot.com>2023-11-28 17:49:26 +0100
committerArmand Philippot <git@armandphilippot.com>2023-11-28 18:04:24 +0100
commit29a1dec4de0aa7ba64ef068a83b1b8589fbc3ad0 (patch)
tree8db871542e878e9fdf589bccd1be7b5ed1378f72 /tests/msw
parentf564d181bc428e25a02bf1d98c4449a6b3eb8e9e (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')
-rw-r--r--tests/msw/handlers/index.ts2
-rw-r--r--tests/msw/handlers/topics/index.ts11
-rw-r--r--tests/msw/handlers/topics/topic.handler.ts21
-rw-r--r--tests/msw/handlers/topics/topics-count.handler.ts43
-rw-r--r--tests/msw/handlers/topics/topics-list.handler.ts42
-rw-r--r--tests/msw/handlers/topics/topics-slugs.handler.ts26
-rw-r--r--tests/msw/schema/types/index.ts12
-rw-r--r--tests/msw/schema/types/topic.types.ts35
8 files changed, 191 insertions, 1 deletions
diff --git a/tests/msw/handlers/index.ts b/tests/msw/handlers/index.ts
index e42a0eb..85a2300 100644
--- a/tests/msw/handlers/index.ts
+++ b/tests/msw/handlers/index.ts
@@ -1,9 +1,11 @@
import { commentsHandlers } from './comments';
import { postsHandlers } from './posts';
import { thematicsHandlers } from './thematics';
+import { topicsHandlers } from './topics';
export const handlers = [
...commentsHandlers,
...postsHandlers,
...thematicsHandlers,
+ ...topicsHandlers,
];
diff --git a/tests/msw/handlers/topics/index.ts b/tests/msw/handlers/topics/index.ts
new file mode 100644
index 0000000..e29694b
--- /dev/null
+++ b/tests/msw/handlers/topics/index.ts
@@ -0,0 +1,11 @@
+import { topicHandler } from './topic.handler';
+import { topicsCountHandler } from './topics-count.handler';
+import { topicsListHandler } from './topics-list.handler';
+import { topicsSlugsHandler } from './topics-slugs.handler';
+
+export const topicsHandlers = [
+ topicHandler,
+ topicsCountHandler,
+ topicsListHandler,
+ topicsSlugsHandler,
+];
diff --git a/tests/msw/handlers/topics/topic.handler.ts b/tests/msw/handlers/topics/topic.handler.ts
new file mode 100644
index 0000000..5df00ea
--- /dev/null
+++ b/tests/msw/handlers/topics/topic.handler.ts
@@ -0,0 +1,21 @@
+import { type ExecutionResult, graphql as executeGraphql } from 'graphql';
+import { HttpResponse, graphql } from 'msw';
+import type { TopicResponse } from '../../../../src/services/graphql';
+import { wpTopicsFixture } from '../../../fixtures';
+import { schema } from '../../schema';
+
+export const topicHandler = graphql.query<
+ TopicResponse,
+ Record<'slug', string>
+>('Topic', async ({ query, variables }) => {
+ const { data, errors } = (await executeGraphql({
+ schema,
+ source: query,
+ variableValues: variables,
+ rootValue: {
+ topic: wpTopicsFixture.find((wpTopic) => wpTopic.slug === variables.slug),
+ },
+ })) as ExecutionResult<TopicResponse>;
+
+ return HttpResponse.json({ data, errors });
+});
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 });
+});
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<TopicsListResponse>;
+
+ return HttpResponse.json({ data, errors });
+});
diff --git a/tests/msw/handlers/topics/topics-slugs.handler.ts b/tests/msw/handlers/topics/topics-slugs.handler.ts
new file mode 100644
index 0000000..960e411
--- /dev/null
+++ b/tests/msw/handlers/topics/topics-slugs.handler.ts
@@ -0,0 +1,26 @@
+import { type ExecutionResult, graphql as executeGraphql } from 'graphql';
+import { HttpResponse, graphql } from 'msw';
+import type { TopicsSlugsResponse } from '../../../../src/services/graphql';
+import { wpTopicsFixture } from '../../../fixtures';
+import { schema } from '../../schema';
+
+export const topicsSlugsHandler = graphql.query<
+ TopicsSlugsResponse,
+ Record<'first', number>
+>('TopicsSlugs', 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: { nodes: wpTopicsFixture },
+ },
+ })) as ExecutionResult<TopicsSlugsResponse>;
+
+ return HttpResponse.json({ data, errors });
+});
diff --git a/tests/msw/schema/types/index.ts b/tests/msw/schema/types/index.ts
index c570033..ada7f2c 100644
--- a/tests/msw/schema/types/index.ts
+++ b/tests/msw/schema/types/index.ts
@@ -38,6 +38,18 @@ const rootQueryType = `type Query {
last: Int
where: RootQueryToThematicConnectionWhereArgs
): RootQueryToThematicConnection
+ topic(
+ asPreview: Boolean
+ id: ID!
+ idType: TopicIdType
+ ): Topic
+ topics(
+ after: String
+ before: String
+ first: Int
+ last: Int
+ where: RootQueryToTopicConnectionWhereArgs
+ ): RootQueryToTopicConnection
}`;
export const types = [
diff --git a/tests/msw/schema/types/topic.types.ts b/tests/msw/schema/types/topic.types.ts
index ba9abb7..2d54653 100644
--- a/tests/msw/schema/types/topic.types.ts
+++ b/tests/msw/schema/types/topic.types.ts
@@ -1,4 +1,11 @@
-export const topicTypes = `union Topic_Acftopics_PostsInTopic = Post
+export const topicTypes = `enum TopicIdType {
+ DATABASE_ID
+ ID
+ SLUG
+ URI
+}
+
+union Topic_Acftopics_PostsInTopic = Post
type Topic_Acftopics {
officialWebsite: String
@@ -16,6 +23,32 @@ type Topic {
seo: PostTypeSEO
slug: String
title(format: PostObjectFieldFormatEnum): String
+}
+
+input RootQueryToTopicConnectionWhereArgs {
+ authorName: String
+ orderby: [PostObjectsConnectionOrderbyInput]
+ search: String
+ title: String
+}
+
+type RootQueryToTopicConnectionPageInfo {
+ endCursor: String
+ hasNextPage: Boolean!
+ hasPreviousPage: Boolean!
+ startCursor: String
+ total: Int
+}
+
+type RootQueryToTopicConnectionEdge {
+ cursor: String
+ node: Topic!
+}
+
+type RootQueryToTopicConnection {
+ edges: [RootQueryToTopicConnectionEdge!]!
+ nodes: [Topic!]!
+ pageInfo: RootQueryToTopicConnectionPageInfo!
}`;
// cSpell:ignore Acftopics