diff options
Diffstat (limited to 'src/services/graphql/fetchers/thematics')
8 files changed, 113 insertions, 4 deletions
diff --git a/src/services/graphql/fetchers/thematics/fetch-all-thematics-slugs.test.ts b/src/services/graphql/fetchers/thematics/fetch-all-thematics-slugs.test.ts new file mode 100644 index 0000000..3650528 --- /dev/null +++ b/src/services/graphql/fetchers/thematics/fetch-all-thematics-slugs.test.ts @@ -0,0 +1,26 @@ +import { afterEach, describe, expect, it } from '@jest/globals'; +import { wpThematicsFixture } from '../../../../../tests/fixtures'; +import { fetchAllThematicsSlugs } from './fetch-all-thematics-slugs'; + +describe('fetch-all-thematics-slugs', () => { + afterEach(() => { + window.history.replaceState({}, '', '/'); + }); + + it('returns the WordPress thematics using GraphQL', async () => { + const result = await fetchAllThematicsSlugs(wpThematicsFixture.length); + + expect.assertions(1); + + expect(result).toStrictEqual(wpThematicsFixture.map((post) => post.slug)); + }); + + it('rejects with an error when no thematics are found', async () => { + window.history.replaceState({}, '', '/?error=true'); + expect.assertions(1); + + await expect(async () => + fetchAllThematicsSlugs(wpThematicsFixture.length) + ).rejects.toEqual(new Error('Unable to find the thematics slugs.')); + }); +}); diff --git a/src/services/graphql/fetchers/thematics/fetch-all-thematics-slugs.ts b/src/services/graphql/fetchers/thematics/fetch-all-thematics-slugs.ts index c44bb6d..80da462 100644 --- a/src/services/graphql/fetchers/thematics/fetch-all-thematics-slugs.ts +++ b/src/services/graphql/fetchers/thematics/fetch-all-thematics-slugs.ts @@ -1,7 +1,7 @@ import type { GraphQLNodes, Nullable, SlugNode } from '../../../../types'; import { fetchGraphQL, getGraphQLUrl } from '../../../../utils/helpers'; -type ThematicsSlugsResponse = { +export type ThematicsSlugsResponse = { thematics: Nullable<GraphQLNodes<SlugNode>>; }; diff --git a/src/services/graphql/fetchers/thematics/fetch-thematic.test.ts b/src/services/graphql/fetchers/thematics/fetch-thematic.test.ts new file mode 100644 index 0000000..bf6495a --- /dev/null +++ b/src/services/graphql/fetchers/thematics/fetch-thematic.test.ts @@ -0,0 +1,23 @@ +import { describe, expect, it } from '@jest/globals'; +import { wpThematicsFixture } from '../../../../../tests/fixtures'; +import { fetchThematic } from './fetch-thematic'; + +describe('fetch-thematic', () => { + it('returns a thematic by slug', async () => { + const result = await fetchThematic(wpThematicsFixture[2].slug); + + expect.assertions(1); + + expect(result).toStrictEqual(wpThematicsFixture[2]); + }); + + it('rejects with an error when the slug does not exist', async () => { + const slug = '/inexistent-slug'; + + expect.assertions(1); + + await expect(async () => fetchThematic(slug)).rejects.toEqual( + new Error(`No thematic found for the following slug ${slug}.`) + ); + }); +}); diff --git a/src/services/graphql/fetchers/thematics/fetch-thematic.ts b/src/services/graphql/fetchers/thematics/fetch-thematic.ts index a9958bc..4214785 100644 --- a/src/services/graphql/fetchers/thematics/fetch-thematic.ts +++ b/src/services/graphql/fetchers/thematics/fetch-thematic.ts @@ -1,7 +1,7 @@ import type { Nullable, WPThematic } from '../../../../types'; import { fetchGraphQL, getGraphQLUrl } from '../../../../utils/helpers'; -type ThematicResponse = { +export type ThematicResponse = { thematic: Nullable<WPThematic>; }; @@ -54,6 +54,8 @@ const thematicQuery = `query Thematic($slug: ID!) { afterMore beforeMore } + databaseId + date featuredImage { node { altText @@ -65,6 +67,7 @@ const thematicQuery = `query Thematic($slug: ID!) { title } } + modified seo { metaDesc title diff --git a/src/services/graphql/fetchers/thematics/fetch-thematics-count.test.ts b/src/services/graphql/fetchers/thematics/fetch-thematics-count.test.ts new file mode 100644 index 0000000..0d3ff2a --- /dev/null +++ b/src/services/graphql/fetchers/thematics/fetch-thematics-count.test.ts @@ -0,0 +1,26 @@ +import { afterEach, describe, expect, it } from '@jest/globals'; +import { wpThematicsFixture } from '../../../../../tests/fixtures'; +import { fetchThematicsCount } from './fetch-thematics-count'; + +describe('fetch-thematics-count', () => { + afterEach(() => { + window.history.replaceState({}, '', '/'); + }); + + it('returns the WordPress thematics count using GraphQL', async () => { + const result = await fetchThematicsCount(); + + expect.assertions(1); + + expect(result).toBe(wpThematicsFixture.length); + }); + + it('rejects with an error when no thematics are found', async () => { + window.history.replaceState({}, '', '/?error=true'); + expect.assertions(1); + + await expect(async () => fetchThematicsCount()).rejects.toEqual( + new Error('Unable to find the total number of thematics.') + ); + }); +}); diff --git a/src/services/graphql/fetchers/thematics/fetch-thematics-count.ts b/src/services/graphql/fetchers/thematics/fetch-thematics-count.ts index 29a3b17..d961b02 100644 --- a/src/services/graphql/fetchers/thematics/fetch-thematics-count.ts +++ b/src/services/graphql/fetchers/thematics/fetch-thematics-count.ts @@ -5,7 +5,7 @@ import type { } from '../../../../types'; import { fetchGraphQL, getGraphQLUrl } from '../../../../utils/helpers'; -type ThematicsCountResponse = { +export type ThematicsCountResponse = { thematics: Nullable<{ pageInfo: Pick<GraphQLPageInfo, 'total'>; }>; diff --git a/src/services/graphql/fetchers/thematics/fetch-thematics-list.test.ts b/src/services/graphql/fetchers/thematics/fetch-thematics-list.test.ts new file mode 100644 index 0000000..d52262d --- /dev/null +++ b/src/services/graphql/fetchers/thematics/fetch-thematics-list.test.ts @@ -0,0 +1,26 @@ +import { afterEach, describe, expect, it } from '@jest/globals'; +import { wpThematicsFixture } from '../../../../../tests/fixtures'; +import { fetchThematicsList } from './fetch-thematics-list'; + +describe('fetch-thematics-list', () => { + afterEach(() => { + window.history.replaceState({}, '', '/'); + }); + + it('returns the WordPress thematics using GraphQL', async () => { + const result = await fetchThematicsList({}); + + expect.assertions(1); + + expect(result.pageInfo.total).toBe(wpThematicsFixture.length); + }); + + it('rejects with an error when no thematics are found', async () => { + window.history.replaceState({}, '', '/?error=true'); + expect.assertions(1); + + await expect(async () => + fetchThematicsList({ where: { title: 'inexistent-title' } }) + ).rejects.toEqual(new Error('No thematics found.')); + }); +}); diff --git a/src/services/graphql/fetchers/thematics/fetch-thematics-list.ts b/src/services/graphql/fetchers/thematics/fetch-thematics-list.ts index f4d22c6..7e1e582 100644 --- a/src/services/graphql/fetchers/thematics/fetch-thematics-list.ts +++ b/src/services/graphql/fetchers/thematics/fetch-thematics-list.ts @@ -8,7 +8,7 @@ import type { } from '../../../../types'; import { fetchGraphQL, getGraphQLUrl } from '../../../../utils/helpers'; -type ThematicsListResponse = { +export type ThematicsListResponse = { thematics: Nullable<GraphQLConnection<WPThematicPreview>>; }; @@ -42,6 +42,11 @@ const thematicsListQuery = `query ThematicsList($after: String, $before: String, title } } + pageInfo { + endCursor + hasNextPage + total + } } }`; |
