|
| 1 | +import { SuggestionRegistryProvider } from '@kaoto/forms'; |
1 | 2 | import { act, fireEvent, render, screen } from '@testing-library/react'; |
2 | 3 | import { MemoryRouter } from 'react-router-dom'; |
3 | 4 |
|
4 | 5 | import { AbstractSettingsAdapter, DefaultSettingsAdapter } from '../../models/settings'; |
5 | 6 | import { ReloadContext, SettingsProvider } from '../../providers'; |
| 7 | +import { TestRuntimeProviderWrapper } from '../../stubs/TestRuntimeProviderWrapper'; |
6 | 8 | import { SettingsForm } from './SettingsForm'; |
7 | 9 |
|
8 | 10 | describe('SettingsForm', () => { |
9 | 11 | let reloadPage: jest.Mock; |
10 | 12 | let settingsAdapter: AbstractSettingsAdapter; |
| 13 | + const { Provider: RuntimeProvider } = TestRuntimeProviderWrapper(); |
11 | 14 |
|
12 | 15 | const wrapper = ({ children }: { children: React.ReactNode }) => { |
13 | 16 | return ( |
14 | 17 | <MemoryRouter> |
15 | 18 | <ReloadContext.Provider value={{ reloadPage, lastRender: 0 }}> |
16 | | - <SettingsProvider adapter={settingsAdapter}>{children}</SettingsProvider> |
| 19 | + <RuntimeProvider> |
| 20 | + <SettingsProvider adapter={settingsAdapter}> |
| 21 | + <SuggestionRegistryProvider>{children}</SuggestionRegistryProvider> |
| 22 | + </SettingsProvider> |
| 23 | + </RuntimeProvider> |
17 | 24 | </ReloadContext.Provider> |
18 | 25 | </MemoryRouter> |
19 | 26 | ); |
@@ -52,12 +59,29 @@ describe('SettingsForm', () => { |
52 | 59 | expect(settingsAdapter.getSettings().catalogUrl).not.toBe('http://localhost:8080'); |
53 | 60 | }); |
54 | 61 |
|
55 | | - it('should reload the page upon clicking save', () => { |
56 | | - act(() => { |
| 62 | + it('should reload the page upon clicking save', async () => { |
| 63 | + await act(async () => { |
57 | 64 | const button = screen.getByTestId('settings-form-save-btn'); |
58 | 65 | fireEvent.click(button); |
59 | 66 | }); |
60 | 67 |
|
61 | 68 | expect(reloadPage).toHaveBeenCalledTimes(1); |
62 | 69 | }); |
| 70 | + |
| 71 | + it('should display error alert when save fails', async () => { |
| 72 | + // Mock saveSettings to throw an error |
| 73 | + const errorMessage = 'Failed to save settings to storage'; |
| 74 | + settingsAdapter.saveSettings = jest.fn().mockRejectedValue(new Error(errorMessage)); |
| 75 | + |
| 76 | + await act(async () => { |
| 77 | + const button = screen.getByTestId('settings-form-save-btn'); |
| 78 | + fireEvent.click(button); |
| 79 | + }); |
| 80 | + |
| 81 | + // Wait for error alert to appear |
| 82 | + const errorAlert = await screen.findByText('Failed to save settings.'); |
| 83 | + expect(errorAlert).toBeInTheDocument(); |
| 84 | + expect(screen.getByText(errorMessage)).toBeInTheDocument(); |
| 85 | + expect(reloadPage).not.toHaveBeenCalled(); |
| 86 | + }); |
63 | 87 | }); |
0 commit comments