diff --git a/superset-frontend/src/hooks/useUnsavedChangesPrompt/useUnsavedChangesPrompt.test.tsx b/superset-frontend/src/hooks/useUnsavedChangesPrompt/useUnsavedChangesPrompt.test.tsx index fd6b2a7c3976..37cde732cddf 100644 --- a/superset-frontend/src/hooks/useUnsavedChangesPrompt/useUnsavedChangesPrompt.test.tsx +++ b/superset-frontend/src/hooks/useUnsavedChangesPrompt/useUnsavedChangesPrompt.test.tsx @@ -22,115 +22,116 @@ import { Router } from 'react-router-dom'; import { createMemoryHistory } from 'history'; import { act } from 'spec/helpers/testing-library'; -const history = createMemoryHistory({ +let history = createMemoryHistory({ initialEntries: ['/dashboard'], }); +beforeEach(() => { + history = createMemoryHistory({ initialEntries: ['/dashboard'] }); +}); + const wrapper = ({ children }: { children: React.ReactNode }) => ( {children} ); -// eslint-disable-next-line no-restricted-globals -- TODO: Migrate from describe blocks -describe('useUnsavedChangesPrompt', () => { - test('should not show modal initially', () => { - const { result } = renderHook( - () => - useUnsavedChangesPrompt({ - hasUnsavedChanges: true, - onSave: jest.fn(), - }), - { wrapper }, - ); - - expect(result.current.showModal).toBe(false); - }); +test('should not show modal initially', () => { + const { result } = renderHook( + () => + useUnsavedChangesPrompt({ + hasUnsavedChanges: true, + onSave: jest.fn(), + }), + { wrapper }, + ); + + expect(result.current.showModal).toBe(false); +}); - test('should block navigation and show modal if there are unsaved changes', () => { - const { result } = renderHook( - () => - useUnsavedChangesPrompt({ - hasUnsavedChanges: true, - onSave: jest.fn(), - }), - { wrapper }, - ); - - // Simulate blocked navigation - act(() => { - const unblock = history.block((tx: any) => tx); - unblock(); - history.push('/another-page'); - }); - - expect(result.current.showModal).toBe(true); +test('should block navigation and show modal if there are unsaved changes', () => { + const { result } = renderHook( + () => + useUnsavedChangesPrompt({ + hasUnsavedChanges: true, + onSave: jest.fn(), + }), + { wrapper }, + ); + + // Simulate blocked navigation + act(() => { + const unblock = history.block((tx: any) => tx); + unblock(); + history.push('/another-page'); }); - test('should trigger onSave and hide modal on handleSaveAndCloseModal', async () => { - const onSave = jest.fn().mockResolvedValue(undefined); + expect(result.current.showModal).toBe(true); +}); - const { result } = renderHook( - () => - useUnsavedChangesPrompt({ - hasUnsavedChanges: true, - onSave, - }), - { wrapper }, - ); +test('should trigger onSave and hide modal on handleSaveAndCloseModal', async () => { + const onSave = jest.fn().mockResolvedValue(undefined); - await act(async () => { - await result.current.handleSaveAndCloseModal(); - }); + const { result } = renderHook( + () => + useUnsavedChangesPrompt({ + hasUnsavedChanges: true, + onSave, + }), + { wrapper }, + ); - expect(onSave).toHaveBeenCalled(); - expect(result.current.showModal).toBe(false); + await act(async () => { + await result.current.handleSaveAndCloseModal(); }); - test('should trigger manual save and not show modal again', async () => { - const onSave = jest.fn().mockResolvedValue(undefined); + expect(onSave).toHaveBeenCalled(); + expect(result.current.showModal).toBe(false); +}); + +test('should trigger manual save and not show modal again', async () => { + const onSave = jest.fn().mockResolvedValue(undefined); + + const { result } = renderHook( + () => + useUnsavedChangesPrompt({ + hasUnsavedChanges: true, + onSave, + }), + { wrapper }, + ); - const { result } = renderHook( - () => - useUnsavedChangesPrompt({ - hasUnsavedChanges: true, - onSave, - }), - { wrapper }, - ); + act(() => { + result.current.triggerManualSave(); + }); - act(() => { - result.current.triggerManualSave(); - }); + expect(onSave).toHaveBeenCalled(); + expect(result.current.showModal).toBe(false); +}); - expect(onSave).toHaveBeenCalled(); - expect(result.current.showModal).toBe(false); +test('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'); }); - 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); + expect(result.current.showModal).toBe(true); + + // Then call handleConfirmNavigation to discard changes + act(() => { + result.current.handleConfirmNavigation(); }); + + expect(result.current.showModal).toBe(false); });