Routing: Update alerting template routes (#94366)

* Templates: Remove Switch routes

* Update tests

* Fix test

* Revert mock behaviour and render AppNotificationList

---------

Co-authored-by: Tom Ratcliffe <tom.ratcliffe@grafana.com>
pull/94401/head^2
Alex Khomenko 1 year ago committed by GitHub
parent 3924751827
commit 38f57d270a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 42
      public/app/features/alerting/unified/Templates.test.tsx
  2. 26
      public/app/features/alerting/unified/Templates.tsx
  3. 9
      public/app/features/alerting/unified/components/contact-points/DuplicateMessageTemplate.tsx
  4. 2
      public/app/features/alerting/unified/components/contact-points/EditMessageTemplate.tsx

@ -1,4 +1,6 @@
import { InitialEntry } from 'history/createMemoryHistory';
import * as React from 'react'; import * as React from 'react';
import { Route, Routes } from 'react-router-dom-v5-compat';
import { Props } from 'react-virtualized-auto-sizer'; import { Props } from 'react-virtualized-auto-sizer';
import { render, screen, within } from 'test/test-utils'; import { render, screen, within } from 'test/test-utils';
import { byRole } from 'testing-library-selector'; import { byRole } from 'testing-library-selector';
@ -54,23 +56,35 @@ beforeEach(() => {
grantUserPermissions([AccessControlAction.AlertingNotificationsRead, AccessControlAction.AlertingNotificationsWrite]); grantUserPermissions([AccessControlAction.AlertingNotificationsRead, AccessControlAction.AlertingNotificationsWrite]);
}); });
const setup = (initialEntries: InitialEntry[]) => {
return render(
<>
<AppNotificationList />
<Routes>
<Route path="/alerting/notifications/templates/*" element={<Templates />} />
</Routes>
</>,
{
historyOptions: { initialEntries },
}
);
};
describe('Templates routes', () => { describe('Templates routes', () => {
it('allows duplication of template with spaces in name', async () => { it('allows duplication of template with spaces in name', async () => {
render(<Templates />, { setup([navUrl.duplicate('template%20with%20spaces')]);
historyOptions: { initialEntries: [navUrl.duplicate('template%20with%20spaces')] },
});
expect(await screen.findByText('Edit payload')).toBeInTheDocument(); expect(await screen.findByText('Edit payload')).toBeInTheDocument();
}); });
it('allows editing of template with spaces in name', async () => { it('allows editing of template with spaces in name', async () => {
render(<Templates />, { historyOptions: { initialEntries: [navUrl.edit('template%20with%20spaces')] } }); setup([navUrl.edit('template%20with%20spaces')]);
expect(await screen.findByText('Edit payload')).toBeInTheDocument(); expect(await screen.findByText('Edit payload')).toBeInTheDocument();
}); });
it('renders empty template form', async () => { it('renders empty template form', async () => {
render(<Templates />, { historyOptions: { initialEntries: [navUrl.new] } }); setup([navUrl.new]);
const form = await ui.templateForm.find(); const form = await ui.templateForm.find();
@ -83,9 +97,7 @@ describe('Templates K8s API', () => {
testWithFeatureToggles(['alertingApiServer']); testWithFeatureToggles(['alertingApiServer']);
it('form edit renders with correct form values', async () => { it('form edit renders with correct form values', async () => {
render(<Templates />, { setup([navUrl.edit('k8s-custom-email-resource-name')]);
historyOptions: { initialEntries: [navUrl.edit('k8s-custom-email-resource-name')] },
});
const form = await ui.templateForm.find(); const form = await ui.templateForm.find();
@ -97,9 +109,7 @@ describe('Templates K8s API', () => {
}); });
it('renders duplicate template form with correct values', async () => { it('renders duplicate template form with correct values', async () => {
render(<Templates />, { setup([navUrl.duplicate('k8s-custom-email-resource-name')]);
historyOptions: { initialEntries: [navUrl.duplicate('k8s-custom-email-resource-name')] },
});
const form = await ui.templateForm.find(); const form = await ui.templateForm.find();
@ -111,15 +121,7 @@ describe('Templates K8s API', () => {
}); });
it('updates a template', async () => { it('updates a template', async () => {
const { user } = render( const { user } = setup([navUrl.edit('k8s-custom-email-resource-name')]);
<>
<Templates />
<AppNotificationList />
</>,
{
historyOptions: { initialEntries: [navUrl.edit('k8s-custom-email-resource-name')] },
}
);
const form = await ui.templateForm.find(); const form = await ui.templateForm.find();

@ -1,15 +1,11 @@
import { Route, Switch } from 'react-router-dom'; import { Routes, Route } from 'react-router-dom-v5-compat';
import { withErrorBoundary } from '@grafana/ui'; import { withErrorBoundary } from '@grafana/ui';
import { SafeDynamicImport } from 'app/core/components/DynamicImports/SafeDynamicImport';
import { AlertmanagerPageWrapper } from './components/AlertingPageWrapper'; import { AlertmanagerPageWrapper } from './components/AlertingPageWrapper';
import DuplicateMessageTemplate from './components/contact-points/DuplicateMessageTemplate';
const EditMessageTemplate = SafeDynamicImport(() => import('./components/contact-points/EditMessageTemplate')); import EditMessageTemplate from './components/contact-points/EditMessageTemplate';
const NewMessageTemplate = SafeDynamicImport(() => import('./components/contact-points/NewMessageTemplate')); import NewMessageTemplate from './components/contact-points/NewMessageTemplate';
const DuplicateMessageTemplate = SafeDynamicImport(
() => import('./components/contact-points/DuplicateMessageTemplate')
);
const NotificationTemplates = (): JSX.Element => ( const NotificationTemplates = (): JSX.Element => (
<AlertmanagerPageWrapper <AlertmanagerPageWrapper
@ -17,15 +13,11 @@ const NotificationTemplates = (): JSX.Element => (
accessType="notification" accessType="notification"
pageNav={{ id: 'templates', text: 'Notification templates', subTitle: 'Create and edit notification templates' }} pageNav={{ id: 'templates', text: 'Notification templates', subTitle: 'Create and edit notification templates' }}
> >
<Switch> <Routes>
<Route exact={true} path="/alerting/notifications/templates/:name/edit" component={EditMessageTemplate} /> <Route path=":name/edit" element={<EditMessageTemplate />} />
<Route exact={true} path="/alerting/notifications/templates/new" component={NewMessageTemplate} /> <Route path="new" element={<NewMessageTemplate />} />
<Route <Route path=":name/duplicate" element={<DuplicateMessageTemplate />} />
exact={true} </Routes>
path="/alerting/notifications/templates/:name/duplicate"
component={DuplicateMessageTemplate}
/>
</Switch>
</AlertmanagerPageWrapper> </AlertmanagerPageWrapper>
); );

@ -1,4 +1,4 @@
import { RouteChildrenProps } from 'react-router-dom'; import { useParams } from 'react-router-dom-v5-compat';
import { Alert, LoadingPlaceholder } from '@grafana/ui'; import { Alert, LoadingPlaceholder } from '@grafana/ui';
import { EntityNotFound } from 'app/core/components/PageNotFound/EntityNotFound'; import { EntityNotFound } from 'app/core/components/PageNotFound/EntityNotFound';
@ -12,13 +12,12 @@ import { TemplateForm } from '../receivers/TemplateForm';
import { useGetNotificationTemplate, useNotificationTemplates } from './useNotificationTemplates'; import { useGetNotificationTemplate, useNotificationTemplates } from './useNotificationTemplates';
type Props = RouteChildrenProps<{ name: string }>;
const notFoundComponent = <EntityNotFound entity="Notification template" />; const notFoundComponent = <EntityNotFound entity="Notification template" />;
const DuplicateMessageTemplate = ({ match }: Props) => { const DuplicateMessageTemplate = () => {
const { selectedAlertmanager } = useAlertmanager(); const { selectedAlertmanager } = useAlertmanager();
const templateUid = match?.params.name ? decodeURIComponent(match?.params.name) : undefined; const { name } = useParams<{ name: string }>();
const templateUid = name ? decodeURIComponent(name) : undefined;
const { const {
currentData: template, currentData: template,

@ -1,4 +1,4 @@
import { useParams } from 'react-router-dom'; import { useParams } from 'react-router-dom-v5-compat';
import { Alert, LoadingPlaceholder } from '@grafana/ui'; import { Alert, LoadingPlaceholder } from '@grafana/ui';
import { EntityNotFound } from 'app/core/components/PageNotFound/EntityNotFound'; import { EntityNotFound } from 'app/core/components/PageNotFound/EntityNotFound';

Loading…
Cancel
Save