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 --- .../graphql/helpers/build-comments-tree.test.ts | 67 ++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 src/services/graphql/helpers/build-comments-tree.test.ts (limited to 'src/services/graphql/helpers/build-comments-tree.test.ts') diff --git a/src/services/graphql/helpers/build-comments-tree.test.ts b/src/services/graphql/helpers/build-comments-tree.test.ts new file mode 100644 index 0000000..cd9fa40 --- /dev/null +++ b/src/services/graphql/helpers/build-comments-tree.test.ts @@ -0,0 +1,67 @@ +import { describe, expect, it } from '@jest/globals'; +import type { SingleComment } from '../../../types'; +import { buildCommentsTree } from './build-comments-tree'; + +describe('build-comments-tree', () => { + it('transforms a flat comments array to a comments tree', () => { + const firstComment = { + content: 'Non non provident mollitia a.', + id: 1, + isApproved: true, + meta: { author: { name: 'Emma_Muller' }, date: '2022-11-02' }, + replies: [], + } satisfies SingleComment; + const firstCommentReplies = [ + { + content: 'Et omnis voluptatem est atque.', + id: 3, + isApproved: true, + meta: { author: { name: 'Patrick.Goodwin44' }, date: '2022-11-05' }, + replies: [], + parentId: 1, + }, + ] satisfies SingleComment[]; + const secondComment = { + content: 'Vero iure architecto modi iusto qui.', + id: 2, + isApproved: true, + meta: { author: { name: 'Dominique13' }, date: '2022-11-04' }, + replies: [], + } satisfies SingleComment; + const secondCommentReplies = [ + { + content: 'Qui quaerat quas quia praesentium quasi.', + id: 4, + isApproved: true, + meta: { author: { name: 'Patrick.Goodwin44' }, date: '2022-11-05' }, + replies: [], + parentId: 2, + }, + { + content: 'Ut officia aliquid harum voluptas molestiae quo.', + id: 5, + isApproved: true, + meta: { author: { name: 'Ariel.Braun6' }, date: '2022-11-06' }, + replies: [], + parentId: 2, + }, + ] satisfies SingleComment[]; + const comments: SingleComment[] = [ + firstComment, + secondComment, + ...firstCommentReplies, + ...secondCommentReplies, + ]; + const result = buildCommentsTree(comments); + + expect(result).toHaveLength(2); + expect(result[0]).toStrictEqual({ + ...firstComment, + replies: firstCommentReplies, + }); + expect(result[1]).toStrictEqual({ + ...secondComment, + replies: secondCommentReplies, + }); + }); +}); -- cgit v1.2.3