Routing: Update alerting routes to react router 6 (#94469)

* Update alertingRuleEditor

* Update Policy.test

* Update AlertRuleForm

* Update ReceiversSection

* Update SimplifiedRuleEditor.test.tsx

* Update CloneRule.tsx
This commit is contained in:
Alex Khomenko
2024-10-10 07:33:16 +03:00
committed by GitHub
parent e0217c37a1
commit 97249d15d1
6 changed files with 36 additions and 24 deletions

View File

@ -1,10 +1,10 @@
import { render, renderHook, screen, within } from '@testing-library/react'; import { renderHook, screen, within } from '@testing-library/react';
import userEvent from '@testing-library/user-event'; import userEvent from '@testing-library/user-event';
import { first, noop } from 'lodash'; import { first, noop } from 'lodash';
import { Router } from 'react-router-dom'; import { Routes, Route } from 'react-router-dom-v5-compat';
import { CompatRouter } from 'react-router-dom-v5-compat'; import { render } from 'test/test-utils';
import { config, locationService } from '@grafana/runtime'; import { config } from '@grafana/runtime';
import { contextSrv } from 'app/core/core'; import { contextSrv } from 'app/core/core';
import { import {
AlertmanagerGroup, AlertmanagerGroup,
@ -345,13 +345,17 @@ describe('Policy', () => {
}); });
}); });
// Doesn't matter which path the routes use, it just needs to match the initialEntries history entry to render the element
const renderPolicy = (element: JSX.Element) => const renderPolicy = (element: JSX.Element) =>
render( render(
<Router history={locationService.getHistory()}> <Routes>
<CompatRouter> <Route path={'/'} element={<AlertmanagerProvider accessType="notification">{element}</AlertmanagerProvider>} />
<AlertmanagerProvider accessType="notification">{element}</AlertmanagerProvider> </Routes>,
</CompatRouter> {
</Router> historyOptions: {
initialEntries: ['/'],
},
}
); );
const eq = MatcherOperator.equal; const eq = MatcherOperator.equal;

View File

@ -1,6 +1,6 @@
import { css, cx } from '@emotion/css'; import { css, cx } from '@emotion/css';
import * as React from 'react'; import * as React from 'react';
import { Link } from 'react-router-dom'; import { Link } from 'react-router-dom-v5-compat';
import { useToggle } from 'react-use'; import { useToggle } from 'react-use';
import { GrafanaTheme2 } from '@grafana/data'; import { GrafanaTheme2 } from '@grafana/data';

View File

@ -1,7 +1,7 @@
import { css } from '@emotion/css'; import { css } from '@emotion/css';
import { useCallback, useEffect, useMemo, useState } from 'react'; import { useCallback, useEffect, useMemo, useState } from 'react';
import { FormProvider, SubmitErrorHandler, UseFormWatch, useForm } from 'react-hook-form'; import { FormProvider, SubmitErrorHandler, UseFormWatch, useForm } from 'react-hook-form';
import { useParams } from 'react-router-dom'; import { useParams } from 'react-router-dom-v5-compat';
import { GrafanaTheme2 } from '@grafana/data'; import { GrafanaTheme2 } from '@grafana/data';
import { config, locationService } from '@grafana/runtime'; import { config, locationService } from '@grafana/runtime';
@ -82,7 +82,7 @@ export const AlertRuleForm = ({ existing, prefill }: Props) => {
const routeParams = useParams<{ type: string; id: string }>(); const routeParams = useParams<{ type: string; id: string }>();
const ruleType = translateRouteParamToRuleType(routeParams.type); const ruleType = translateRouteParamToRuleType(routeParams.type);
const uidFromParams = routeParams.id; const uidFromParams = routeParams.id || '';
const [showDeleteModal, setShowDeleteModal] = useState<boolean>(false); const [showDeleteModal, setShowDeleteModal] = useState<boolean>(false);

View File

@ -1,5 +1,5 @@
import { ReactNode } from 'react'; import { ReactNode } from 'react';
import { Route } from 'react-router-dom'; import { Routes, Route } from 'react-router-dom-v5-compat';
import { ui } from 'test/helpers/alertingRuleEditor'; import { ui } from 'test/helpers/alertingRuleEditor';
import { clickSelectOption } from 'test/helpers/selectOptionInTest'; import { clickSelectOption } from 'test/helpers/selectOptionInTest';
import { render, screen, waitForElementToBeRemoved, userEvent } from 'test/test-utils'; import { render, screen, waitForElementToBeRemoved, userEvent } from 'test/test-utils';
@ -157,7 +157,10 @@ describe('Can create a new grafana managed alert using simplified routing', () =
function renderSimplifiedRuleEditor() { function renderSimplifiedRuleEditor() {
return render( return render(
<AlertmanagerProvider alertmanagerSourceName={GRAFANA_DATASOURCE_NAME} accessType="notification"> <AlertmanagerProvider alertmanagerSourceName={GRAFANA_DATASOURCE_NAME} accessType="notification">
<Route path={['/alerting/new/:type', '/alerting/:id/edit']} component={RuleEditor} /> <Routes>
<Route path={'/alerting/new/:type'} element={<RuleEditor />} />
<Route path={'/alerting/:id/edit'} element={<RuleEditor />} />
</Routes>
</AlertmanagerProvider>, </AlertmanagerProvider>,
{ historyOptions: { initialEntries: ['/alerting/new/alerting'] } } { historyOptions: { initialEntries: ['/alerting/new/alerting'] } }
); );

View File

@ -1,6 +1,5 @@
import { forwardRef, useState } from 'react'; import { forwardRef, useState } from 'react';
import { useLocation } from 'react-router-dom'; import { Navigate, useLocation } from 'react-router-dom-v5-compat';
import { Navigate } from 'react-router-dom-v5-compat';
import { Button, ConfirmModal } from '@grafana/ui'; import { Button, ConfirmModal } from '@grafana/ui';
import { RuleIdentifier } from 'app/types/unified-alerting'; import { RuleIdentifier } from 'app/types/unified-alerting';

View File

@ -1,4 +1,4 @@
import { Route } from 'react-router-dom'; import { Routes, Route } from 'react-router-dom-v5-compat';
import { render } from 'test/test-utils'; import { render } from 'test/test-utils';
import { byRole, byTestId, byText } from 'testing-library-selector'; import { byRole, byTestId, byText } from 'testing-library-selector';
@ -35,11 +35,17 @@ export const ui = {
}; };
export function renderRuleEditor(identifier?: string, recording = false) { export function renderRuleEditor(identifier?: string, recording = false) {
return render(<Route path={['/alerting/new/:type', '/alerting/:id/edit']} component={RuleEditor} />, { return render(
<Routes>
<Route path={'/alerting/new/:type'} element={<RuleEditor />} />
<Route path={'/alerting/:id/edit'} element={<RuleEditor />} />
</Routes>,
{
historyOptions: { historyOptions: {
initialEntries: [ initialEntries: [
identifier ? `/alerting/${identifier}/edit` : `/alerting/new/${recording ? 'recording' : 'alerting'}`, identifier ? `/alerting/${identifier}/edit` : `/alerting/new/${recording ? 'recording' : 'alerting'}`,
], ],
}, },
}); }
);
} }