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>
pull/92278/head
Alex Khomenko 10 months ago committed by GitHub
parent 7bee9d5e0f
commit 022892ef72
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 6
      public/app/features/alerting/unified/RedirectToRuleViewer.test.tsx
  2. 2
      public/app/features/alerting/unified/RuleEditorGrafanaRules.test.tsx
  3. 3
      public/app/features/alerting/unified/RuleList.test.tsx
  4. 2
      public/app/features/dashboard/components/ShareModal/SharePublicDashboard/SharePublicDashboard.test.tsx
  5. 24
      public/app/features/scopes/testUtils.tsx
  6. 7
      public/test/helpers/TestProvider.tsx
  7. 20
      public/test/test-utils.tsx

@ -76,6 +76,9 @@ describe('Redirect to Rule viewer', () => {
}); });
it('should properly decode rule name', () => { it('should properly decode rule name', () => {
// TODO: Fix console warning that happens once CompatRouter is wrapped around this component render
jest.spyOn(console, 'warn').mockImplementation();
const rulesMatchingSpy = jest.spyOn(combinedRuleHooks, 'useCloudCombinedRulesMatching').mockReturnValue({ const rulesMatchingSpy = jest.spyOn(combinedRuleHooks, 'useCloudCombinedRulesMatching').mockReturnValue({
rules: [mockedRules[0]], rules: [mockedRules[0]],
loading: false, loading: false,
@ -112,6 +115,9 @@ describe('Redirect to Rule viewer', () => {
}); });
it('should properly decode source name', () => { it('should properly decode source name', () => {
// TODO: Fix console warning that happens once CompatRouter is wrapped around this component render
jest.spyOn(console, 'warn').mockImplementation();
const rulesMatchingSpy = jest.spyOn(combinedRuleHooks, 'useCloudCombinedRulesMatching').mockReturnValue({ const rulesMatchingSpy = jest.spyOn(combinedRuleHooks, 'useCloudCombinedRulesMatching').mockReturnValue({
rules: [mockedRules[0]], rules: [mockedRules[0]],
loading: false, loading: false,

@ -120,7 +120,7 @@ describe('RuleEditor grafana managed rules', () => {
const folderInput = await ui.inputs.folder.find(); const folderInput = await ui.inputs.folder.find();
await clickSelectOption(folderInput, 'Folder A'); await clickSelectOption(folderInput, 'Folder A');
const groupInput = await ui.inputs.group.find(); const groupInput = await ui.inputs.group.find();
await userEvent.click(byRole('combobox').get(groupInput)); await userEvent.click(await byRole('combobox').find(groupInput));
await clickSelectOption(groupInput, grafanaRulerGroup.name); await clickSelectOption(groupInput, grafanaRulerGroup.name);
await userEvent.type(ui.inputs.annotationValue(1).get(), 'some description'); await userEvent.type(ui.inputs.annotationValue(1).get(), 'some description');

@ -92,7 +92,8 @@ const renderRuleList = () => {
return render( return render(
<TestProvider> <TestProvider>
<RuleList /> <RuleList />
</TestProvider> </TestProvider>,
{ renderWithRouter: false }
); );
}; };

@ -136,7 +136,7 @@ const alertTests = () => {
}); });
await renderSharePublicDashboard({ dashboard }); await renderSharePublicDashboard({ dashboard });
expect(screen.queryByTestId(selectors.UnsupportedDataSourcesWarningAlert)).toBeInTheDocument(); expect(await screen.findByTestId(selectors.UnsupportedDataSourcesWarningAlert)).toBeInTheDocument();
}); });
}; };

@ -1,6 +1,6 @@
import { screen } from '@testing-library/react'; import { screen } from '@testing-library/react';
import { KBarProvider } from 'kbar'; import { KBarProvider } from 'kbar';
import { getWrapper, render } from 'test/test-utils'; import { render } from 'test/test-utils';
import { Scope, ScopeDashboardBinding, ScopeNode } from '@grafana/data'; import { Scope, ScopeDashboardBinding, ScopeNode } from '@grafana/data';
import { import {
@ -16,10 +16,8 @@ import {
VizPanel, VizPanel,
} from '@grafana/scenes'; } from '@grafana/scenes';
import { AppChrome } from 'app/core/components/AppChrome/AppChrome'; import { AppChrome } from 'app/core/components/AppChrome/AppChrome';
import { AppChromeService } from 'app/core/components/AppChrome/AppChromeService';
import { DashboardControls } from 'app/features/dashboard-scene/scene//DashboardControls'; import { DashboardControls } from 'app/features/dashboard-scene/scene//DashboardControls';
import { DashboardScene } from 'app/features/dashboard-scene/scene/DashboardScene'; import { DashboardScene } from 'app/features/dashboard-scene/scene/DashboardScene';
import { configureStore } from 'app/store/configureStore';
import { ScopesFacade } from './ScopesFacadeScene'; import { ScopesFacade } from './ScopesFacadeScene';
import { scopesDashboardsScene, scopesSelectorScene } from './instance'; import { scopesDashboardsScene, scopesSelectorScene } from './instance';
@ -464,24 +462,12 @@ export function buildTestScene(overrides: Partial<DashboardScene> = {}) {
} }
export function renderDashboard(dashboardScene: DashboardScene) { export function renderDashboard(dashboardScene: DashboardScene) {
const store = configureStore();
const chrome = new AppChromeService();
chrome.update({ chromeless: false });
const Wrapper = getWrapper({ store, renderWithRouter: true, grafanaContext: { chrome } });
return render( return render(
<KBarProvider> <KBarProvider>
<Wrapper> <AppChrome>
<AppChrome> <dashboardScene.Component model={dashboardScene} />
<dashboardScene.Component model={dashboardScene} /> </AppChrome>
</AppChrome> </KBarProvider>
</Wrapper>
</KBarProvider>,
{
historyOptions: {
initialEntries: ['/'],
},
}
); );
} }

@ -2,6 +2,7 @@ import { Store } from '@reduxjs/toolkit';
import * as React from 'react'; import * as React from 'react';
import { Provider } from 'react-redux'; import { Provider } from 'react-redux';
import { Router } from 'react-router-dom'; import { Router } from 'react-router-dom';
import { CompatRouter } from 'react-router-dom-v5-compat';
import { getGrafanaContextMock } from 'test/mocks/getGrafanaContextMock'; import { getGrafanaContextMock } from 'test/mocks/getGrafanaContextMock';
import { locationService } from '@grafana/runtime'; import { locationService } from '@grafana/runtime';
@ -35,8 +36,10 @@ export function TestProvider(props: Props) {
<Provider store={store}> <Provider store={store}>
<Router history={locationService.getHistory()}> <Router history={locationService.getHistory()}>
<ModalsContextProvider> <ModalsContextProvider>
<GrafanaContext.Provider value={context}>{children}</GrafanaContext.Provider> <CompatRouter>
<ModalRoot /> <GrafanaContext.Provider value={context}>{children}</GrafanaContext.Provider>
<ModalRoot />
</CompatRouter>
</ModalsContextProvider> </ModalsContextProvider>
</Router> </Router>
</Provider> </Provider>

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

Loading…
Cancel
Save