@@ -2,7 +2,15 @@ import { fireEvent, render, screen } from '@testing-library/react';
22import { beforeEach , expect , test , vi } from 'vitest' ;
33import FlowchartPage from '@/pages/FlowchartPage' ;
44
5- const { mockToastSuccess } = vi . hoisted ( ( ) => ( { mockToastSuccess : vi . fn ( ) } ) ) ;
5+ const { mockSavedFlowcharts, mockToastSuccess } = vi . hoisted ( ( ) => ( {
6+ mockSavedFlowcharts : [ ] as Array < {
7+ id : string ;
8+ title : string ;
9+ createdAt : number ;
10+ data : { nodes : unknown [ ] ; edges : unknown [ ] } ;
11+ } > ,
12+ mockToastSuccess : vi . fn ( ) ,
13+ } ) ) ;
614
715vi . mock ( 'sonner' , ( ) => ( {
816 toast : { success : mockToastSuccess , error : vi . fn ( ) } ,
@@ -15,7 +23,7 @@ vi.mock('@/lib/stores/session-store', () => ({
1523 pendingFlowchartData : null as null ,
1624 setPendingFlowchartData : vi . fn ( ) ,
1725 saveFlowchart : vi . fn ( ) ,
18- savedFlowcharts : [ ] as [ ] ,
26+ savedFlowcharts : mockSavedFlowcharts ,
1927 deleteSavedFlowchart : vi . fn ( ) ,
2028 } ) ,
2129} ) ) ;
@@ -48,7 +56,10 @@ vi.mock('reactflow', async () => {
4856 } ;
4957} ) ;
5058
51- beforeEach ( ( ) => mockToastSuccess . mockClear ( ) ) ;
59+ beforeEach ( ( ) => {
60+ mockSavedFlowcharts . length = 0 ;
61+ mockToastSuccess . mockClear ( ) ;
62+ } ) ;
5263
5364test ( 'keeps a selected node when deletion is cancelled' , ( ) => {
5465 render ( < FlowchartPage /> ) ;
@@ -72,3 +83,28 @@ test('deletes a selected node only after confirmation and reports success', () =
7283 expect ( screen . queryByRole ( 'button' , { name : 'Argument 1' } ) ) . not . toBeInTheDocument ( ) ;
7384 expect ( mockToastSuccess ) . toHaveBeenCalledWith ( 'Node deleted' ) ;
7485} ) ;
86+
87+ test ( 'clears a stale node selection when a saved flowchart is loaded' , ( ) => {
88+ mockSavedFlowcharts . push ( {
89+ id : 'saved-1' ,
90+ title : 'Loaded chart' ,
91+ createdAt : Date . now ( ) ,
92+ data : {
93+ nodes : [ {
94+ id : 'loaded-node' ,
95+ type : 'custom' ,
96+ position : { x : 0 , y : 0 } ,
97+ data : { label : 'Loaded argument' , type : 'contention' } ,
98+ } ] ,
99+ edges : [ ] ,
100+ } ,
101+ } ) ;
102+ render ( < FlowchartPage /> ) ;
103+
104+ fireEvent . click ( screen . getByRole ( 'button' , { name : 'Argument 1' } ) ) ;
105+ fireEvent . click ( screen . getByRole ( 'button' , { name : 'Load' } ) ) ;
106+ fireEvent . click ( screen . getByRole ( 'button' , { name : / L o a d e d c h a r t / } ) ) ;
107+
108+ expect ( screen . getByRole ( 'button' , { name : 'Loaded argument' } ) ) . toBeInTheDocument ( ) ;
109+ expect ( screen . queryByRole ( 'button' , { name : 'Delete' } ) ) . not . toBeInTheDocument ( ) ;
110+ } ) ;
0 commit comments