From f111685c5886f3e77edfd3621c98d8ac1b9bcce4 Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Fri, 24 Nov 2023 20:00:08 +0100 Subject: refactor(services, types): reorganize GraphQL fetchers and data types The Typescript mapped types was useful for autocompletion in fetchers but their are harder to maintain. I think it's better to keep each query close to its fetcher to have a better understanding of the fetched data. So I: * colocate queries with their own fetcher * colocate mutations with their own mutator * remove Typescript mapped types for queries and mutations * move data convertors inside graphql services * rename most of data types and fetchers --- ...convert-post-preview-to-article-preview.test.ts | 130 +++++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 src/services/graphql/helpers/convert-post-preview-to-article-preview.test.ts (limited to 'src/services/graphql/helpers/convert-post-preview-to-article-preview.test.ts') diff --git a/src/services/graphql/helpers/convert-post-preview-to-article-preview.test.ts b/src/services/graphql/helpers/convert-post-preview-to-article-preview.test.ts new file mode 100644 index 0000000..c13684f --- /dev/null +++ b/src/services/graphql/helpers/convert-post-preview-to-article-preview.test.ts @@ -0,0 +1,130 @@ +import { describe, expect, it } from '@jest/globals'; +import type { WPPostPreview, WPThematicPreview } from '../../../types'; +import { convertPostPreviewToArticlePreview } from './convert-post-preview-to-article-preview'; +import { convertTaxonomyToPageLink } from './convert-taxonomy-to-page-link'; +import { convertWPImgToImg } from './convert-wp-image-to-img'; + +describe('convert-post-preview-to-article-preview', () => { + /* eslint-disable max-statements */ + it('converts a RecentWPPost object to a RecentArticle object', () => { + const post: WPPostPreview = { + acfPosts: null, + commentCount: 6, + contentParts: { + beforeMore: + 'Et quos fuga molestias. Voluptatum nobis mollitia eaque dolorem sunt. Dolores dignissimos consequuntur mollitia. Enim molestias quibusdam sequi. Dolore ut quo. Libero iure non vel reprehenderit.', + }, + databaseId: 5, + date: '2021-04-28', + featuredImage: null, + info: { + wordsCount: 450, + }, + modified: '2021-04-29', + slug: '/the-post-slug', + title: 'et tempore sint', + }; + const result = convertPostPreviewToArticlePreview(post); + + expect(result.id).toBe(post.databaseId); + expect(result.intro).toBe(post.contentParts.beforeMore); + expect(result.meta.commentsCount).toBe(post.commentCount); + expect(result.meta.cover).toBeUndefined(); + expect(result.meta.dates.publication).toBe(post.date); + expect(result.meta.dates.update).toBe(post.modified); + expect(result.meta.thematics).toBeUndefined(); + expect(result.meta.wordsCount).toBe(post.info.wordsCount); + expect(result.slug).toBe(post.slug); + expect(result.title).toBe(post.title); + }); + /* eslint-enable max-statements */ + + it('can return 0 as comment count if not defined', () => { + const post: WPPostPreview = { + acfPosts: null, + commentCount: null, + contentParts: { + beforeMore: + 'Et quos fuga molestias. Voluptatum nobis mollitia eaque dolorem sunt. Dolores dignissimos consequuntur mollitia. Enim molestias quibusdam sequi. Dolore ut quo. Libero iure non vel reprehenderit.', + }, + databaseId: 5, + date: '2021-04-28', + featuredImage: null, + info: { + wordsCount: 450, + }, + modified: '2021-04-29', + slug: '/the-post-slug', + title: 'et tempore sint', + }; + const result = convertPostPreviewToArticlePreview(post); + + expect(result.meta.commentsCount).toBe(0); + }); + + it('can convert the cover', () => { + const post = { + acfPosts: null, + commentCount: null, + contentParts: { + beforeMore: + 'Et quos fuga molestias. Voluptatum nobis mollitia eaque dolorem sunt. Dolores dignissimos consequuntur mollitia. Enim molestias quibusdam sequi. Dolore ut quo. Libero iure non vel reprehenderit.', + }, + databaseId: 5, + date: '2021-04-28', + featuredImage: { + node: { + altText: 'molestiae praesentium animi', + mediaDetails: { + height: 480, + width: 640, + }, + sourceUrl: 'https://picsum.photos/640/480', + title: 'ullam deserunt perspiciatis', + }, + }, + info: { + wordsCount: 450, + }, + modified: '2021-04-29', + slug: '/the-post-slug', + title: 'et tempore sint', + } satisfies WPPostPreview; + const result = convertPostPreviewToArticlePreview(post); + + expect(result.meta.cover).toStrictEqual( + convertWPImgToImg(post.featuredImage.node) + ); + }); + + it('can convert the thematics', () => { + const thematics: WPThematicPreview[] = [ + { databaseId: 2, slug: '/thematic1', title: 'aut quis vel' }, + { databaseId: 8, slug: '/thematic2', title: 'vel sint autem' }, + ]; + const post: WPPostPreview = { + acfPosts: { + postsInThematic: thematics, + }, + commentCount: 6, + contentParts: { + beforeMore: + 'Et quos fuga molestias. Voluptatum nobis mollitia eaque dolorem sunt. Dolores dignissimos consequuntur mollitia. Enim molestias quibusdam sequi. Dolore ut quo. Libero iure non vel reprehenderit.', + }, + databaseId: 5, + date: '2021-04-28', + featuredImage: null, + info: { + wordsCount: 450, + }, + modified: '2021-04-29', + slug: '/the-post-slug', + title: 'et tempore sint', + }; + const result = convertPostPreviewToArticlePreview(post); + + expect(result.meta.thematics).toStrictEqual( + thematics.map(convertTaxonomyToPageLink) + ); + }); +}); -- cgit v1.2.3