Skip to content
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

Fix: Navigate to design page on settings save #1872

Merged
merged 1 commit into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading