From 7b4008b10256ced0797468dac17b9877b1f39f7c Mon Sep 17 00:00:00 2001 From: arnaud Date: Thu, 9 Nov 2023 23:02:48 +0100 Subject: [PATCH] feat(useMikadoGraph): the prerequisite form can only be opened once --- app/mikado-graph/component/mikado-graph.tsx | 4 +-- .../mikado-graph.usecase.spec.tsx | 25 ++++++++++++++++--- app/mikado-graph/mikado-graph.usecase.tsx | 4 ++- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/app/mikado-graph/component/mikado-graph.tsx b/app/mikado-graph/component/mikado-graph.tsx index 689ca6d..6b4e36a 100644 --- a/app/mikado-graph/component/mikado-graph.tsx +++ b/app/mikado-graph/component/mikado-graph.tsx @@ -1,11 +1,11 @@ 'use client'; -import React, { useEffect } from 'react'; +import React from 'react'; import { StatusView } from '@/api/mikado-graph/mikado-graph'; import AddPrerequisiteForm from '@/mikado-graph/component/add-prerequisite-form'; import { Translation } from '@/tools/i18n/intl-provider'; import { - Controls, Handle, MiniMap, Position, ReactFlow, useEdgesState, useNodesState, + Controls, Handle, MiniMap, Position, ReactFlow, } from 'reactflow'; import { GaolData, MikadoGraph, NewPrerequisiteData, PrerequisiteData, diff --git a/app/mikado-graph/mikado-graph.usecase.spec.tsx b/app/mikado-graph/mikado-graph.usecase.spec.tsx index a4e15b5..d94fdbc 100644 --- a/app/mikado-graph/mikado-graph.usecase.spec.tsx +++ b/app/mikado-graph/mikado-graph.usecase.spec.tsx @@ -198,10 +198,7 @@ describe('useMikadoGraph', () => { const mikadoGraphId = uuidv4(); const mikadoGraphView = aMikadoGraphView({ mikadoGraphId, prerequisites: [] }); const { result } = renderHook(() => useMikadoGraph(mikadoGraphView), { - wrapper: createWrapper( - {}, - { 'prerequisite.notification.add-prerequisite.success': 'The prerequisite has been added' }, - ), + wrapper: createWrapper(), }); await act(() => result.current.openPrerequisiteForm(mikadoGraphId)); @@ -217,6 +214,26 @@ describe('useMikadoGraph', () => { target: 'new-prerequisite', }); }); + + test('The prerequisite form can only be opened once', async () => { + const parentId = uuidv4(); + const mikadoGraphView = aMikadoGraphView({ + mikadoGraphId: parentId, + prerequisites: [ + { prerequisiteId: 'new-prerequisite', parentId: uuidv4() }, + ], + }); + const { result } = renderHook(() => useMikadoGraph(mikadoGraphView), { + wrapper: createWrapper(), + }); + + await act(() => result.current.openPrerequisiteForm(parentId)); + + expect(result.current.mikadoGraph.nodes[1]).toMatchObject({ + id: 'new-prerequisite', + parentId, + }); // 0: Goal + 1: prerequisite + }); }); describe('add prerequisite', () => { diff --git a/app/mikado-graph/mikado-graph.usecase.tsx b/app/mikado-graph/mikado-graph.usecase.tsx index b484a54..29d2bbf 100644 --- a/app/mikado-graph/mikado-graph.usecase.tsx +++ b/app/mikado-graph/mikado-graph.usecase.tsx @@ -80,9 +80,11 @@ export default function useMikadoGraph(defaultMikadoGraphView: MikadoGraphView) const nodeHeight = 350; const openPrerequisiteForm = (parentId: string) => { + const prerequisiteWithoutNodeForm = mikadoGraphView.prerequisites.filter((p) => p.prerequisiteId !== 'new-prerequisite'); + setMikadoGraphView({ ...mikadoGraphView, - prerequisites: [...mikadoGraphView.prerequisites, { + prerequisites: [...prerequisiteWithoutNodeForm, { prerequisiteId: 'new-prerequisite', parentId, label: '',