Skip to content

Commit 022892e

Browse files
Routing: Add CompatRouter to tests (grafana#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 (grafana#92127) --------- Co-authored-by: Tom Ratcliffe <tom.ratcliffe@grafana.com>
1 parent 7bee9d5 commit 022892e

File tree

7 files changed

+34
-30
lines changed

7 files changed

+34
-30
lines changed

public/app/features/alerting/unified/RedirectToRuleViewer.test.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ describe('Redirect to Rule viewer', () => {
7676
});
7777

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

114117
it('should properly decode source name', () => {
118+
// TODO: Fix console warning that happens once CompatRouter is wrapped around this component render
119+
jest.spyOn(console, 'warn').mockImplementation();
120+
115121
const rulesMatchingSpy = jest.spyOn(combinedRuleHooks, 'useCloudCombinedRulesMatching').mockReturnValue({
116122
rules: [mockedRules[0]],
117123
loading: false,

public/app/features/alerting/unified/RuleEditorGrafanaRules.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ describe('RuleEditor grafana managed rules', () => {
120120
const folderInput = await ui.inputs.folder.find();
121121
await clickSelectOption(folderInput, 'Folder A');
122122
const groupInput = await ui.inputs.group.find();
123-
await userEvent.click(byRole('combobox').get(groupInput));
123+
await userEvent.click(await byRole('combobox').find(groupInput));
124124
await clickSelectOption(groupInput, grafanaRulerGroup.name);
125125
await userEvent.type(ui.inputs.annotationValue(1).get(), 'some description');
126126

public/app/features/alerting/unified/RuleList.test.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ const renderRuleList = () => {
9292
return render(
9393
<TestProvider>
9494
<RuleList />
95-
</TestProvider>
95+
</TestProvider>,
96+
{ renderWithRouter: false }
9697
);
9798
};
9899

public/app/features/dashboard/components/ShareModal/SharePublicDashboard/SharePublicDashboard.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ const alertTests = () => {
136136
});
137137

138138
await renderSharePublicDashboard({ dashboard });
139-
expect(screen.queryByTestId(selectors.UnsupportedDataSourcesWarningAlert)).toBeInTheDocument();
139+
expect(await screen.findByTestId(selectors.UnsupportedDataSourcesWarningAlert)).toBeInTheDocument();
140140
});
141141
};
142142

public/app/features/scopes/testUtils.tsx

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { screen } from '@testing-library/react';
22
import { KBarProvider } from 'kbar';
3-
import { getWrapper, render } from 'test/test-utils';
3+
import { render } from 'test/test-utils';
44

55
import { Scope, ScopeDashboardBinding, ScopeNode } from '@grafana/data';
66
import {
@@ -16,10 +16,8 @@ import {
1616
VizPanel,
1717
} from '@grafana/scenes';
1818
import { AppChrome } from 'app/core/components/AppChrome/AppChrome';
19-
import { AppChromeService } from 'app/core/components/AppChrome/AppChromeService';
2019
import { DashboardControls } from 'app/features/dashboard-scene/scene//DashboardControls';
2120
import { DashboardScene } from 'app/features/dashboard-scene/scene/DashboardScene';
22-
import { configureStore } from 'app/store/configureStore';
2321

2422
import { ScopesFacade } from './ScopesFacadeScene';
2523
import { scopesDashboardsScene, scopesSelectorScene } from './instance';
@@ -464,24 +462,12 @@ export function buildTestScene(overrides: Partial<DashboardScene> = {}) {
464462
}
465463

466464
export function renderDashboard(dashboardScene: DashboardScene) {
467-
const store = configureStore();
468-
const chrome = new AppChromeService();
469-
chrome.update({ chromeless: false });
470-
const Wrapper = getWrapper({ store, renderWithRouter: true, grafanaContext: { chrome } });
471-
472465
return render(
473466
<KBarProvider>
474-
<Wrapper>
475-
<AppChrome>
476-
<dashboardScene.Component model={dashboardScene} />
477-
</AppChrome>
478-
</Wrapper>
479-
</KBarProvider>,
480-
{
481-
historyOptions: {
482-
initialEntries: ['/'],
483-
},
484-
}
467+
<AppChrome>
468+
<dashboardScene.Component model={dashboardScene} />
469+
</AppChrome>
470+
</KBarProvider>
485471
);
486472
}
487473

public/test/helpers/TestProvider.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Store } from '@reduxjs/toolkit';
22
import * as React from 'react';
33
import { Provider } from 'react-redux';
44
import { Router } from 'react-router-dom';
5+
import { CompatRouter } from 'react-router-dom-v5-compat';
56
import { getGrafanaContextMock } from 'test/mocks/getGrafanaContextMock';
67

78
import { locationService } from '@grafana/runtime';
@@ -35,8 +36,10 @@ export function TestProvider(props: Props) {
3536
<Provider store={store}>
3637
<Router history={locationService.getHistory()}>
3738
<ModalsContextProvider>
38-
<GrafanaContext.Provider value={context}>{children}</GrafanaContext.Provider>
39-
<ModalRoot />
39+
<CompatRouter>
40+
<GrafanaContext.Provider value={context}>{children}</GrafanaContext.Provider>
41+
<ModalRoot />
42+
</CompatRouter>
4043
</ModalsContextProvider>
4144
</Router>
4245
</Provider>

public/test/test-utils.tsx

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { Fragment, PropsWithChildren } from 'react';
66
import * as React from 'react';
77
import { Provider } from 'react-redux';
88
import { Router } from 'react-router-dom';
9+
import { CompatRouter } from 'react-router-dom-v5-compat';
910
import { getGrafanaContextMock } from 'test/mocks/getGrafanaContextMock';
1011

1112
import { HistoryWrapper, LocationServiceProvider, setLocationService } from '@grafana/runtime';
@@ -49,17 +50,22 @@ const getWrapper = ({
4950
grafanaContext?: Partial<GrafanaContextType>;
5051
}) => {
5152
const reduxStore = store || configureStore();
52-
/**
53-
* Conditional router - either a MemoryRouter or just a Fragment
54-
*/
55-
const PotentialRouter = renderWithRouter ? Router : Fragment;
5653

5754
// Create a fresh location service for each test - otherwise we run the risk
5855
// of it being stateful in between runs
5956
const history = createMemoryHistory(historyOptions);
6057
const locationService = new HistoryWrapper(history);
6158
setLocationService(locationService);
6259

60+
/**
61+
* Conditional router - either a MemoryRouter or just a Fragment
62+
*/
63+
const PotentialRouter = renderWithRouter
64+
? ({ children }: PropsWithChildren) => <Router history={history}>{children}</Router>
65+
: ({ children }: PropsWithChildren) => <Fragment>{children}</Fragment>;
66+
67+
const PotentialCompatRouter = renderWithRouter ? CompatRouter : Fragment;
68+
6369
const context = {
6470
...getGrafanaContextMock(),
6571
...grafanaContext,
@@ -73,9 +79,11 @@ const getWrapper = ({
7379
return (
7480
<Provider store={reduxStore}>
7581
<GrafanaContext.Provider value={context}>
76-
<PotentialRouter history={history}>
82+
<PotentialRouter>
7783
<LocationServiceProvider service={locationService}>
78-
<ModalsContextProvider>{children}</ModalsContextProvider>
84+
<PotentialCompatRouter>
85+
<ModalsContextProvider>{children}</ModalsContextProvider>
86+
</PotentialCompatRouter>
7987
</LocationServiceProvider>
8088
</PotentialRouter>
8189
</GrafanaContext.Provider>

0 commit comments

Comments
 (0)