Routing: Add CompatRouter to tests (#92114)

* Add CompatRouter

* Fix tests

* Add CompatRouter to TestProvider

* Use findBy

* Remove AppChromeService

* Remove historyOptions

* Routing: Fix alerting/test utils issues from react compat router usage (#92127)

---------

Co-authored-by: Tom Ratcliffe <tom.ratcliffe@grafana.com>
This commit is contained in:
Alex Khomenko
2024-08-22 13:05:12 +03:00
committed by GitHub
parent 7bee9d5e0f
commit 022892ef72
7 changed files with 34 additions and 30 deletions

View File

@ -6,6 +6,7 @@ import { Fragment, PropsWithChildren } from 'react';
import * as React from 'react';
import { Provider } from 'react-redux';
import { Router } from 'react-router-dom';
import { CompatRouter } from 'react-router-dom-v5-compat';
import { getGrafanaContextMock } from 'test/mocks/getGrafanaContextMock';
import { HistoryWrapper, LocationServiceProvider, setLocationService } from '@grafana/runtime';
@ -49,10 +50,6 @@ const getWrapper = ({
grafanaContext?: Partial<GrafanaContextType>;
}) => {
const reduxStore = store || configureStore();
/**
* Conditional router - either a MemoryRouter or just a Fragment
*/
const PotentialRouter = renderWithRouter ? Router : Fragment;
// Create a fresh location service for each test - otherwise we run the risk
// of it being stateful in between runs
@ -60,6 +57,15 @@ const getWrapper = ({
const locationService = new HistoryWrapper(history);
setLocationService(locationService);
/**
* Conditional router - either a MemoryRouter or just a Fragment
*/
const PotentialRouter = renderWithRouter
? ({ children }: PropsWithChildren) => <Router history={history}>{children}</Router>
: ({ children }: PropsWithChildren) => <Fragment>{children}</Fragment>;
const PotentialCompatRouter = renderWithRouter ? CompatRouter : Fragment;
const context = {
...getGrafanaContextMock(),
...grafanaContext,
@ -73,9 +79,11 @@ const getWrapper = ({
return (
<Provider store={reduxStore}>
<GrafanaContext.Provider value={context}>
<PotentialRouter history={history}>
<PotentialRouter>
<LocationServiceProvider service={locationService}>
<ModalsContextProvider>{children}</ModalsContextProvider>
<PotentialCompatRouter>
<ModalsContextProvider>{children}</ModalsContextProvider>
</PotentialCompatRouter>
</LocationServiceProvider>
</PotentialRouter>
</GrafanaContext.Provider>