diff options
| author | Armand Philippot <git@armandphilippot.com> | 2023-11-28 17:49:26 +0100 |
|---|---|---|
| committer | Armand Philippot <git@armandphilippot.com> | 2023-11-28 18:04:24 +0100 |
| commit | 29a1dec4de0aa7ba64ef068a83b1b8589fbc3ad0 (patch) | |
| tree | 8db871542e878e9fdf589bccd1be7b5ed1378f72 /src/services | |
| parent | f564d181bc428e25a02bf1d98c4449a6b3eb8e9e (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 'src/services')
8 files changed, 109 insertions, 5 deletions
diff --git a/src/services/graphql/fetchers/topics/fetch-all-topics-slugs.test.ts b/src/services/graphql/fetchers/topics/fetch-all-topics-slugs.test.ts new file mode 100644 index 0000000..2e3bbb2 --- /dev/null +++ b/src/services/graphql/fetchers/topics/fetch-all-topics-slugs.test.ts @@ -0,0 +1,26 @@ +import { afterEach, describe, expect, it } from '@jest/globals'; +import { wpTopicsFixture } from '../../../../../tests/fixtures'; +import { fetchAllTopicsSlugs } from './fetch-all-topics-slugs'; + +describe('fetch-all-topics-slugs', () => { + afterEach(() => { + window.history.replaceState({}, '', '/'); + }); + + it('returns the WordPress topics using GraphQL', async () => { + const result = await fetchAllTopicsSlugs(wpTopicsFixture.length); + + expect.assertions(1); + + expect(result).toStrictEqual(wpTopicsFixture.map((post) => post.slug)); + }); + + it('rejects with an error when no topics are found', async () => { + window.history.replaceState({}, '', '/?error=true'); + expect.assertions(1); + + await expect(async () => + fetchAllTopicsSlugs(wpTopicsFixture.length) + ).rejects.toEqual(new Error('Unable to find the topics slugs.')); + }); +}); diff --git a/src/services/graphql/fetchers/topics/fetch-all-topics-slugs.ts b/src/services/graphql/fetchers/topics/fetch-all-topics-slugs.ts index 1df0039..8af2757 100644 --- a/src/services/graphql/fetchers/topics/fetch-all-topics-slugs.ts +++ b/src/services/graphql/fetchers/topics/fetch-all-topics-slugs.ts @@ -1,7 +1,7 @@ import type { GraphQLNodes, Nullable, SlugNode } from '../../../../types'; import { fetchGraphQL, getGraphQLUrl } from '../../../../utils/helpers'; -type TopicsSlugsResponse = { +export type TopicsSlugsResponse = { topics: Nullable<GraphQLNodes<SlugNode>>; }; diff --git a/src/services/graphql/fetchers/topics/fetch-topic.test.ts b/src/services/graphql/fetchers/topics/fetch-topic.test.ts new file mode 100644 index 0000000..5fae313 --- /dev/null +++ b/src/services/graphql/fetchers/topics/fetch-topic.test.ts @@ -0,0 +1,23 @@ +import { describe, expect, it } from '@jest/globals'; +import { wpTopicsFixture } from '../../../../../tests/fixtures'; +import { fetchTopic } from './fetch-topic'; + +describe('fetch-topic', () => { + it('returns a topic by slug', async () => { + const result = await fetchTopic(wpTopicsFixture[2].slug); + + expect.assertions(1); + + expect(result).toStrictEqual(wpTopicsFixture[2]); + }); + + it('rejects with an error when the slug does not exist', async () => { + const slug = '/inexistent-slug'; + + expect.assertions(1); + + await expect(async () => fetchTopic(slug)).rejects.toEqual( + new Error(`No topic found for the following slug ${slug}.`) + ); + }); +}); diff --git a/src/services/graphql/fetchers/topics/fetch-topic.ts b/src/services/graphql/fetchers/topics/fetch-topic.ts index efc1d9e..9c01096 100644 --- a/src/services/graphql/fetchers/topics/fetch-topic.ts +++ b/src/services/graphql/fetchers/topics/fetch-topic.ts @@ -1,7 +1,7 @@ import type { Nullable, WPTopic } from '../../../../types'; import { fetchGraphQL, getGraphQLUrl } from '../../../../utils/helpers'; -type TopicResponse = { +export type TopicResponse = { topic: Nullable<WPTopic>; }; @@ -55,6 +55,8 @@ const topicQuery = `query Topic($slug: ID!) { afterMore beforeMore } + databaseId + date featuredImage { node { altText @@ -66,6 +68,7 @@ const topicQuery = `query Topic($slug: ID!) { title } } + modified seo { metaDesc title diff --git a/src/services/graphql/fetchers/topics/fetch-topics-count.test.ts b/src/services/graphql/fetchers/topics/fetch-topics-count.test.ts new file mode 100644 index 0000000..0e3bb90 --- /dev/null +++ b/src/services/graphql/fetchers/topics/fetch-topics-count.test.ts @@ -0,0 +1,26 @@ +import { afterEach, describe, expect, it } from '@jest/globals'; +import { wpTopicsFixture } from '../../../../../tests/fixtures'; +import { fetchTopicsCount } from './fetch-topics-count'; + +describe('fetch-topics-count', () => { + afterEach(() => { + window.history.replaceState({}, '', '/'); + }); + + it('returns the WordPress topics count using GraphQL', async () => { + const result = await fetchTopicsCount(); + + expect.assertions(1); + + expect(result).toBe(wpTopicsFixture.length); + }); + + it('rejects with an error when no topics are found', async () => { + window.history.replaceState({}, '', '/?error=true'); + expect.assertions(1); + + await expect(async () => fetchTopicsCount()).rejects.toEqual( + new Error('Unable to find the total number of topics.') + ); + }); +}); diff --git a/src/services/graphql/fetchers/topics/fetch-topics-count.ts b/src/services/graphql/fetchers/topics/fetch-topics-count.ts index 868b01e..0f8bdfc 100644 --- a/src/services/graphql/fetchers/topics/fetch-topics-count.ts +++ b/src/services/graphql/fetchers/topics/fetch-topics-count.ts @@ -5,7 +5,7 @@ import type { } from '../../../../types'; import { fetchGraphQL, getGraphQLUrl } from '../../../../utils/helpers'; -type TopicsCountResponse = { +export type TopicsCountResponse = { topics: Nullable<{ pageInfo: Pick<GraphQLPageInfo, 'total'>; }>; diff --git a/src/services/graphql/fetchers/topics/fetch-topics-list.test.ts b/src/services/graphql/fetchers/topics/fetch-topics-list.test.ts new file mode 100644 index 0000000..b841974 --- /dev/null +++ b/src/services/graphql/fetchers/topics/fetch-topics-list.test.ts @@ -0,0 +1,26 @@ +import { afterEach, describe, expect, it } from '@jest/globals'; +import { wpTopicsFixture } from '../../../../../tests/fixtures'; +import { fetchTopicsList } from './fetch-topics-list'; + +describe('fetch-topics-list', () => { + afterEach(() => { + window.history.replaceState({}, '', '/'); + }); + + it('returns the WordPress topics using GraphQL', async () => { + const result = await fetchTopicsList({}); + + expect.assertions(1); + + expect(result.pageInfo.total).toBe(wpTopicsFixture.length); + }); + + it('rejects with an error when no topics are found', async () => { + window.history.replaceState({}, '', '/?error=true'); + expect.assertions(1); + + await expect(async () => + fetchTopicsList({ where: { title: 'inexistent-title' } }) + ).rejects.toEqual(new Error('No topics found.')); + }); +}); diff --git a/src/services/graphql/fetchers/topics/fetch-topics-list.ts b/src/services/graphql/fetchers/topics/fetch-topics-list.ts index 1bc2e38..2ede721 100644 --- a/src/services/graphql/fetchers/topics/fetch-topics-list.ts +++ b/src/services/graphql/fetchers/topics/fetch-topics-list.ts @@ -8,7 +8,7 @@ import type { } from '../../../../types'; import { fetchGraphQL, getGraphQLUrl } from '../../../../utils/helpers'; -type TopicsListResponse = { +export type TopicsListResponse = { topics: Nullable<GraphQLConnection<WPTopicPreview>>; }; @@ -34,7 +34,7 @@ const topicsListQuery = `query TopicsList($after: String, $before: String, $firs height width } - slug + sourceUrl title } } |
