Skip to content

Commit

Permalink
Fix: Navigate to design page on settings save
Browse files Browse the repository at this point in the history
  • Loading branch information
shivamG640 authored and lordrip committed Jan 7, 2025
1 parent 0bdf733 commit 5f57196
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 40 deletions.
9 changes: 9 additions & 0 deletions packages/ui-tests/stories/settings/SettingsForm.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import { SettingsForm } from '@kaoto/kaoto';
import { SettingsProvider, ReloadContext, DefaultSettingsAdapter } from '@kaoto/kaoto/testing';
import { Meta, StoryFn } from '@storybook/react';
import { reactRouterOutlet, reactRouterParameters, withRouter } from 'storybook-addon-remix-react-router';

export default {
title: 'Settings/SettingsForm',
decorators: [withRouter],
parameters: {
reactRouter: reactRouterParameters({
routing: reactRouterOutlet({
path: '*',
}),
}),
},
component: SettingsForm,
} as Meta<typeof SettingsForm>;

Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/assets/settingsSchema.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"additionalProperties": false,
"description": "JSON Schema for Kaoto configuration",
Expand Down
58 changes: 19 additions & 39 deletions packages/ui/src/components/Settings/SettingsForm.test.tsx
Original file line number Diff line number Diff line change
@@ -1,79 +1,59 @@
import { act, fireEvent, render } from '@testing-library/react';
import { act, fireEvent, render, screen } from '@testing-library/react';
import { AbstractSettingsAdapter, DefaultSettingsAdapter } from '../../models/settings';
import { ReloadContext, SettingsProvider } from '../../providers';
import { SettingsForm } from './SettingsForm';
import { MemoryRouter } from 'react-router-dom';

describe('SettingsForm', () => {
let reloadPage: jest.Mock;
let settingsAdapter: AbstractSettingsAdapter;

const wrapper = ({ children }: { children: React.ReactNode }) => {
return (
<MemoryRouter>
<ReloadContext.Provider value={{ reloadPage, lastRender: 0 }}>
<SettingsProvider adapter={settingsAdapter}>{children}</SettingsProvider>
</ReloadContext.Provider>
</MemoryRouter>
);
};

beforeEach(() => {
reloadPage = jest.fn();
settingsAdapter = new DefaultSettingsAdapter();
render(<SettingsForm />, { wrapper });
});

it('should render', () => {
const wrapper = render(
<ReloadContext.Provider value={{ reloadPage, lastRender: 0 }}>
<SettingsProvider adapter={settingsAdapter}>
<SettingsForm />
</SettingsProvider>
</ReloadContext.Provider>,
);

expect(wrapper.getByTestId('settings-form')).toMatchSnapshot();
expect(screen.getByTestId('settings-form')).toMatchSnapshot();
});

it('should update settings upon clicking save', () => {
const wrapper = render(
<ReloadContext.Provider value={{ reloadPage, lastRender: 0 }}>
<SettingsProvider adapter={settingsAdapter}>
<SettingsForm />
</SettingsProvider>
</ReloadContext.Provider>,
);

act(() => {
const input = wrapper.getByLabelText('Camel Catalog URL');
const input = screen.getByLabelText('Camel Catalog URL');
fireEvent.change(input, { target: { value: 'http://localhost:8080' } });
});

act(() => {
const button = wrapper.getByTestId('settings-form-save-btn');
const button = screen.getByTestId('settings-form-save-btn');
fireEvent.click(button);
});

expect(settingsAdapter.getSettings().catalogUrl).toBe('http://localhost:8080');
});

it('should not update settings if the save button was clicked', () => {
const wrapper = render(
<ReloadContext.Provider value={{ reloadPage, lastRender: 0 }}>
<SettingsProvider adapter={settingsAdapter}>
<SettingsForm />
</SettingsProvider>
</ReloadContext.Provider>,
);

it('should not update settings if the save button was not clicked', () => {
act(() => {
const input = wrapper.getByLabelText('Camel Catalog URL');
const input = screen.getByLabelText('Camel Catalog URL');
fireEvent.change(input, { target: { value: 'http://localhost:8080' } });
});

expect(settingsAdapter.getSettings().catalogUrl).not.toBe('http://localhost:8080');
});

it('should reload the page upon clicking save', () => {
const wrapper = render(
<ReloadContext.Provider value={{ reloadPage, lastRender: 0 }}>
<SettingsProvider adapter={settingsAdapter}>
<SettingsForm />
</SettingsProvider>
</ReloadContext.Provider>,
);

act(() => {
const button = wrapper.getByTestId('settings-form-save-btn');
const button = screen.getByTestId('settings-form-save-btn');
fireEvent.click(button);
});

Expand Down
4 changes: 4 additions & 0 deletions packages/ui/src/components/Settings/SettingsForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ import { SettingsModel } from '../../models/settings';
import { SchemaBridgeProvider } from '../../providers/schema-bridge.provider';
import { SettingsContext } from '../../providers/settings.provider';
import { CustomAutoForm } from '../Form/CustomAutoForm';
import { useNavigate } from 'react-router-dom';
import { Links } from '../../router/links.models';

export const SettingsForm: FunctionComponent = () => {
const settingsAdapter = useContext(SettingsContext);
const navigate = useNavigate();
const { lastRender, reloadPage } = useReloadContext();
const [settings, setSettings] = useState(settingsAdapter.getSettings());

Expand All @@ -20,6 +23,7 @@ export const SettingsForm: FunctionComponent = () => {
const onSave = useCallback(() => {
settingsAdapter.saveSettings(settings);
reloadPage();
navigate(Links.Home);
}, [reloadPage, settings, settingsAdapter]);

return (
Expand Down

0 comments on commit 5f57196

Please sign in to comment.