diff --git a/superset-frontend/src/hooks/useUnsavedChangesPrompt/index.ts b/superset-frontend/src/hooks/useUnsavedChangesPrompt/index.ts index b09adece6afa..172e389c249d 100644 --- a/superset-frontend/src/hooks/useUnsavedChangesPrompt/index.ts +++ b/superset-frontend/src/hooks/useUnsavedChangesPrompt/index.ts @@ -41,6 +41,7 @@ export const useUnsavedChangesPrompt = ({ const manualSaveRef = useRef(false); // Track if save was user-initiated (not via navigation) const handleConfirmNavigation = useCallback(() => { + setShowModal(false); confirmNavigationRef.current?.(); }, []); diff --git a/superset-frontend/src/hooks/useUnsavedChangesPrompt/useUnsavedChangesPrompt.test.tsx b/superset-frontend/src/hooks/useUnsavedChangesPrompt/useUnsavedChangesPrompt.test.tsx index bc42cdcfc91d..9e70ee2648e8 100644 --- a/superset-frontend/src/hooks/useUnsavedChangesPrompt/useUnsavedChangesPrompt.test.tsx +++ b/superset-frontend/src/hooks/useUnsavedChangesPrompt/useUnsavedChangesPrompt.test.tsx @@ -103,4 +103,33 @@ describe('useUnsavedChangesPrompt', () => { expect(onSave).toHaveBeenCalled(); expect(result.current.showModal).toBe(false); }); + + it('should close modal when handleConfirmNavigation is called', () => { + const onSave = jest.fn(); + + const { result } = renderHook( + () => + useUnsavedChangesPrompt({ + hasUnsavedChanges: true, + onSave, + }), + { wrapper }, + ); + + // First, trigger navigation to show the modal + act(() => { + const unblock = history.block((tx: any) => tx); + unblock(); + history.push('/another-page'); + }); + + expect(result.current.showModal).toBe(true); + + // Then call handleConfirmNavigation to discard changes + act(() => { + result.current.handleConfirmNavigation(); + }); + + expect(result.current.showModal).toBe(false); + }); });