aboutsummaryrefslogtreecommitdiffstats
path: root/src/services/graphql/helpers/build-comments-tree.test.ts
diff options
context:
space:
mode:
authorArmand Philippot <git@armandphilippot.com>2023-11-24 20:00:08 +0100
committerArmand Philippot <git@armandphilippot.com>2023-11-27 14:47:51 +0100
commitf111685c5886f3e77edfd3621c98d8ac1b9bcce4 (patch)
tree62a541fe3afeb64bf745443706fbfb02e96c5230 /src/services/graphql/helpers/build-comments-tree.test.ts
parentbee515641cb144be9a855ff2cac258d2fedab21d (diff)
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
Diffstat (limited to 'src/services/graphql/helpers/build-comments-tree.test.ts')
-rw-r--r--src/services/graphql/helpers/build-comments-tree.test.ts67
1 files changed, 67 insertions, 0 deletions
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,
+ });
+ });
+});