-
Notifications
You must be signed in to change notification settings - Fork 677
CONSOLE-5012: Migrate confirmModal to overlay pattern #15948
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
c1664b0
4c7cd99
6887299
72c92d6
1ab1d3b
a139911
18f6bd3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,17 @@ | ||
| /** | ||
| * Mock implementation of error-modal-handler for Jest tests | ||
| * | ||
| * Note: This mock provides a simplified SyncModalLaunchers that doesn't call | ||
| * useSyncWarningModalLauncher. Tests that need warning modal functionality | ||
| * should explicitly mock warning-modal-handler or use the real implementation. | ||
| */ | ||
|
|
||
| export const mockLaunchErrorModal = jest.fn(); | ||
|
|
||
| export const SyncErrorModalLauncher = () => null; | ||
| export const useSyncErrorModalLauncher = jest.fn(); | ||
|
|
||
| export const useErrorModalLauncher = jest.fn(() => mockLaunchErrorModal); | ||
| // Simplified component that doesn't sync warning modals | ||
| // Tests needing both error and warning modals should not use this mock | ||
| export const SyncModalLaunchers = () => null; | ||
|
|
||
| export const launchErrorModal = mockLaunchErrorModal; |
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you check if these mocks are used anywhere? I can't find any references in a quick search |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| /** | ||
| * Mock for warning-modal-handler | ||
| * Used in Jest tests to avoid rendering actual modals | ||
| */ | ||
|
|
||
| export const mockLaunchWarningModal = jest.fn((props, onConfirm) => { | ||
| // Immediately call onConfirm by default to simulate user confirming | ||
| onConfirm?.(); | ||
| }); | ||
|
|
||
| export const useSyncWarningModalLauncher = jest.fn(); | ||
|
|
||
| export const launchWarningModal = mockLaunchWarningModal; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,200 @@ | ||
| import { render } from '@testing-library/react'; | ||
| import { useSyncWarningModalLauncher, launchWarningModal } from '../warning-modal-handler'; | ||
|
|
||
| // Mock useOverlay | ||
| const mockLauncher = jest.fn(); | ||
| jest.mock('@console/dynamic-plugin-sdk/src/app/modal-support/useOverlay', () => ({ | ||
| useOverlay: () => mockLauncher, | ||
| })); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thinking if it's better to add OverlayProvider to renderWithProviders, and then use that. that way we are testing more code |
||
|
|
||
| describe('warning-modal-handler', () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| describe('useSyncWarningModalLauncher', () => { | ||
| it('should sync the launcher on mount', () => { | ||
| const TestComponent = () => { | ||
| useSyncWarningModalLauncher(); | ||
| return null; | ||
| }; | ||
|
|
||
| render(<TestComponent />); | ||
|
|
||
| const onConfirm = jest.fn(); | ||
| const onCancel = jest.fn(); | ||
|
|
||
| // Call the module-level function | ||
| launchWarningModal( | ||
| { | ||
| title: 'Test Warning', | ||
| children: 'Are you sure?', | ||
| confirmButtonLabel: 'Confirm', | ||
| }, | ||
| onConfirm, | ||
| onCancel, | ||
| ); | ||
|
|
||
| // Should have called the mocked overlay launcher | ||
| expect(mockLauncher).toHaveBeenCalledWith(expect.anything(), { | ||
| title: 'Test Warning', | ||
| children: 'Are you sure?', | ||
| confirmButtonLabel: 'Confirm', | ||
| onConfirm: expect.any(Function), | ||
| onClose: expect.any(Function), | ||
| }); | ||
| }); | ||
|
|
||
| it('should cleanup launcher on unmount', () => { | ||
| const TestComponent = () => { | ||
| useSyncWarningModalLauncher(); | ||
| return null; | ||
| }; | ||
|
|
||
| const { unmount } = render(<TestComponent />); | ||
|
|
||
| unmount(); | ||
|
|
||
| // Should throw error when launcher is not initialized | ||
| expect(() => { | ||
| launchWarningModal( | ||
| { | ||
| title: 'Test Warning', | ||
| children: 'Are you sure?', | ||
| }, | ||
| jest.fn(), | ||
| ); | ||
| }).toThrow('Warning modal launcher not initialized'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('launchWarningModal', () => { | ||
| it('should launch warning modal when launcher is initialized', () => { | ||
| const TestComponent = () => { | ||
| useSyncWarningModalLauncher(); | ||
| return null; | ||
| }; | ||
|
|
||
| render(<TestComponent />); | ||
|
|
||
| const onConfirm = jest.fn(); | ||
| const onCancel = jest.fn(); | ||
|
|
||
| launchWarningModal( | ||
| { | ||
| title: 'Delete Resource', | ||
| children: 'Are you sure you want to delete?', | ||
| confirmButtonLabel: 'Delete', | ||
| }, | ||
| onConfirm, | ||
| onCancel, | ||
| ); | ||
|
|
||
| expect(mockLauncher).toHaveBeenCalledWith(expect.anything(), { | ||
| title: 'Delete Resource', | ||
| children: 'Are you sure you want to delete?', | ||
| confirmButtonLabel: 'Delete', | ||
| onConfirm: expect.any(Function), | ||
| onClose: expect.any(Function), | ||
| }); | ||
| }); | ||
|
|
||
| it('should throw error when launcher is not initialized', () => { | ||
| expect(() => { | ||
| launchWarningModal( | ||
| { | ||
| title: 'Test', | ||
| children: 'Test message', | ||
| }, | ||
| jest.fn(), | ||
| ); | ||
| }).toThrow('Warning modal launcher not initialized'); | ||
| }); | ||
|
|
||
| it('should call onConfirm callback when user confirms', () => { | ||
| const TestComponent = () => { | ||
| useSyncWarningModalLauncher(); | ||
| return null; | ||
| }; | ||
|
|
||
| render(<TestComponent />); | ||
|
|
||
| const onConfirm = jest.fn(); | ||
| const onCancel = jest.fn(); | ||
|
|
||
| launchWarningModal( | ||
| { | ||
| title: 'Confirm Action', | ||
| children: 'Proceed?', | ||
| }, | ||
| onConfirm, | ||
| onCancel, | ||
| ); | ||
|
|
||
| // Get the onConfirm callback that was passed to the launcher | ||
| const launcherCall = mockLauncher.mock.calls[0]; | ||
| const launcherProps = launcherCall[1]; | ||
| launcherProps.onConfirm(); | ||
|
|
||
| expect(onConfirm).toHaveBeenCalled(); | ||
| expect(onCancel).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should call onCancel callback when user cancels', () => { | ||
| const TestComponent = () => { | ||
| useSyncWarningModalLauncher(); | ||
| return null; | ||
| }; | ||
|
|
||
| render(<TestComponent />); | ||
|
|
||
| const onConfirm = jest.fn(); | ||
| const onCancel = jest.fn(); | ||
|
|
||
| launchWarningModal( | ||
| { | ||
| title: 'Confirm Action', | ||
| children: 'Proceed?', | ||
| }, | ||
| onConfirm, | ||
| onCancel, | ||
| ); | ||
|
|
||
| // Get the onClose callback that was passed to the launcher | ||
| const launcherCall = mockLauncher.mock.calls[0]; | ||
| const launcherProps = launcherCall[1]; | ||
| launcherProps.onClose(); | ||
|
|
||
| expect(onCancel).toHaveBeenCalled(); | ||
| expect(onConfirm).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should handle optional callbacks gracefully', () => { | ||
| const TestComponent = () => { | ||
| useSyncWarningModalLauncher(); | ||
| return null; | ||
| }; | ||
|
|
||
| render(<TestComponent />); | ||
|
|
||
| // Call without callbacks | ||
| expect(() => { | ||
| launchWarningModal({ | ||
| title: 'Info', | ||
| children: 'Just showing info', | ||
| }); | ||
| }).not.toThrow(); | ||
|
|
||
| expect(mockLauncher).toHaveBeenCalled(); | ||
|
|
||
| // Verify calling the callbacks doesn't throw | ||
| const launcherCall = mockLauncher.mock.calls[0]; | ||
| const launcherProps = launcherCall[1]; | ||
|
|
||
| expect(() => { | ||
| launcherProps.onConfirm(); | ||
| launcherProps.onClose(); | ||
| }).not.toThrow(); | ||
| }); | ||
| }); | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you check if these mocks are used anywhere? I can't find any references