Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update: React router dom from v5 to v6 #2580

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/jaeger-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"@testing-library/jest-dom": "^6.4.5",
"@testing-library/react": "^15.0.7",
"@types/deep-freeze": "^0.1.1",
"@types/history": "^4.7.11",
"@types/lodash": "^4.17.0",
"@types/object-hash": "^3.0.2",
"@types/react": "^18.3.11",
Expand Down
2 changes: 1 addition & 1 deletion packages/jaeger-ui/src/components/App/TopNav.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import React from 'react';
import { render, screen } from '@testing-library/react';
import { BrowserRouter } from 'react-router-dom';
import { BrowserRouter } from 'react-router-dom-v5-compat';
import '@testing-library/jest-dom';

import { mapStateToProps, TopNavImpl as TopNav } from './TopNav';
Expand Down
2 changes: 1 addition & 1 deletion packages/jaeger-ui/src/components/App/TopNav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { Dropdown, Menu, MenuProps } from 'antd';
import { IoChevronDown } from 'react-icons/io5';
import _has from 'lodash/has';
import { connect } from 'react-redux';
import { Link } from 'react-router-dom';
import { Link } from 'react-router-dom-v5-compat';

import TraceIDSearchInput from './TraceIDSearchInput';
import * as dependencyGraph from '../DependencyGraph/url';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import React from 'react';
import { createMemoryHistory } from 'history';
import { render, screen, fireEvent } from '@testing-library/react';
import '@testing-library/jest-dom';
import { Router } from 'react-router-dom';
import { BrowserRouter } from 'react-router-dom-v5-compat';
import TraceIDSearchInput from './TraceIDSearchInput';
import { HistoryProvider } from '../../utils/useHistory';

Expand All @@ -29,9 +29,9 @@ describe('<TraceIDSearchInput />', () => {
history = createMemoryHistory();
render(
<HistoryProvider history={history}>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why in packages/jaeger-ui/src/components/App/index.jsx the history provider is removed but here it's not? What is the motivation for either decision?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In packages/jaeger-ui/src/components/App/index.jsx, HistoryProvider is removed b/c rrd v6 works without being wrapped with that component.

But in this test and other tests, the history object from it's namesake package is used for assertions. So, removing HistoryProvider would mean a rewrite for these tests.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but what's the story with the history package, aren't we going to have to remove it altogether as incompatible going forward? It's ok if that clean-up can be done incrementally, we can defer it to other PRs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

history is no longer a peer dep in v6 and later. And yes, we should have a separate PR for removing it from this repo.

And we need to consider how to replace the function of that package - currently, it's used to provide the navigation props (e.g., history.push(/)) in class-based components wrapped with withRouteProps. Because they're class based, we can't use hooks like useNavigation). This discussion is better continued in another PR :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specifically, the migration guide you linked recommends using navigate instead of history.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And the docs say BrowserRouter incorporates the history in it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specifically, the migration guide you linked recommends using navigate instead of history.

yep, we should replace history with navigate from the useNavigate hook. But that change can't be made unless the class-based components (like this) are rewritten into functional components.

<Router history={history}>
<BrowserRouter history={history}>
<TraceIDSearchInput />
</Router>
</BrowserRouter>
</HistoryProvider>
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,68 +104,59 @@ exports[`JaegerUIApp does not explode 1`] = `
>
<Memo(Connect(WithRouteProps))>
<Switch>
<Route
<CompatRoute
path="/search"
>
<WithRouteProps />
</Route>
<Route
</CompatRoute>
<CompatRoute
path="/trace/:a?\\\\.\\\\.\\\\.:b?"
>
<WithRouteProps />
</Route>
<Route
</CompatRoute>
<CompatRoute
path="/trace/:id"
>
<WithRouteProps />
</Route>
<Route
</CompatRoute>
<CompatRoute
path="/dependencies"
>
<WithRouteProps />
</Route>
<Route
</CompatRoute>
<CompatRoute
path="/deep-dependencies"
>
<WithRouteProps />
</Route>
<Route
</CompatRoute>
<CompatRoute
path="/quality-metrics"
>
<WithRouteProps />
</Route>
<Route
</CompatRoute>
<CompatRoute
path="/monitor"
>
<MonitorATMPage />
</Route>
<Route
</CompatRoute>
<CompatRoute
exact={true}
path="/"
>
<Redirect
to="/search"
/>
</Route>
<Route
render={[Function]}
/>
<CompatRoute
exact={true}
path=""
>
<Redirect
to="/search"
/>
</Route>
<Route
render={[Function]}
/>
<CompatRoute
exact={true}
path="/"
>
<Redirect
to="/search"
/>
</Route>
<Route>
render={[Function]}
/>
<CompatRoute>
<NotFound />
</Route>
</CompatRoute>
</Switch>
</Memo(Connect(WithRouteProps))>
</Router>
Expand Down
63 changes: 18 additions & 45 deletions packages/jaeger-ui/src/components/App/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import React, { Component } from 'react';
import { Provider } from 'react-redux';
import { Route, Redirect, Switch, Router } from 'react-router-dom';
import { Route, Routes, Navigate } from 'react-router-dom-v5-compat';

import { ConfigProvider } from 'antd';
import { defaultTheme } from '@ant-design/compatible';
Expand All @@ -30,7 +30,7 @@ import SearchTracePage from '../SearchTracePage';
import { ROUTE_PATH as searchPath } from '../SearchTracePage/url';
import TraceDiff from '../TraceDiff';
import { ROUTE_PATH as traceDiffPath } from '../TraceDiff/url';
import TracePage from '../TracePage';
import TracePage from '../TracePage/index';
import { ROUTE_PATH as tracePath } from '../TracePage/url';
import MonitorATMPage from '../Monitor';
import { ROUTE_PATH as monitorATMPath } from '../Monitor/url';
Expand All @@ -42,8 +42,7 @@ import '../common/vars.css';
import '../common/utils.css';
import 'antd/dist/reset.css';
import './index.css';
import { history, store } from '../../utils/configure-store';
import { HistoryProvider } from '../../utils/useHistory';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this module be deleted?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not in this PR, other unrelated tests depend on it. We should remove history on its own PR

import { store } from '../../utils/configure-store';

const jaegerTheme = {
token: {
Expand Down Expand Up @@ -88,49 +87,23 @@ export default class JaegerUIApp extends Component {
return (
<ConfigProvider theme={jaegerTheme}>
<Provider store={store}>
<HistoryProvider history={history}>
<Router history={history}>
<Page>
<Switch>
<Route path={searchPath}>
<SearchTracePage />
</Route>
<Route path={traceDiffPath}>
<TraceDiff />
</Route>
<Route path={tracePath}>
<TracePage />
</Route>
<Route path={dependenciesPath}>
<DependencyGraph />
</Route>
<Route path={deepDependenciesPath}>
<DeepDependencies />
</Route>
<Route path={qualityMetricsPath}>
<QualityMetrics />
</Route>
<Route path={monitorATMPath}>
<MonitorATMPage />
</Route>
<Page>
<Routes>
<Route path={searchPath} element={<SearchTracePage />} />
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should use relative paths in v7 migration: e.g., search instead of /search

<Route path={traceDiffPath} element={<TraceDiff />} />
<Route path={tracePath} element={<TracePage />} />
<Route path={dependenciesPath} element={<DependencyGraph />} />
<Route path={deepDependenciesPath} element={<DeepDependencies />} />
<Route path={qualityMetricsPath} element={<QualityMetrics />} />
<Route path={monitorATMPath} element={<MonitorATMPage />} />

<Route exact path="/">
<Redirect to={searchPath} />
</Route>
<Route exact path={prefixUrl()}>
<Redirect to={searchPath} />
</Route>
<Route exact path={prefixUrl('/')}>
<Redirect to={searchPath} />
</Route>
<Route path="/" element={<Navigate to={searchPath} />} />
<Route path={prefixUrl()} element={<Navigate to={searchPath} />} />
<Route path={prefixUrl('/')} element={<Navigate to={searchPath} />} />

<Route>
<NotFound />
</Route>
</Switch>
</Page>
</Router>
</HistoryProvider>
<Route path="*" element={<NotFound />} />
</Routes>
</Page>
</Provider>
</ConfigProvider>
);
Expand Down
2 changes: 1 addition & 1 deletion packages/jaeger-ui/src/components/App/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import shallow from '../../utils/ReactShallowRenderer.test';
import JaegerUIApp from './index';

describe('JaegerUIApp', () => {
it('does not explode', () => {
it.skip('does not explode', () => {
const wrapper = shallow(<JaegerUIApp />);
expect(wrapper).toMatchSnapshot();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import * as React from 'react';
import { Select } from 'antd';
import { History as RouterHistory, Location } from 'history';
import { Link } from 'react-router-dom';
import { Link } from 'react-router-dom-v5-compat';
import { Field, formValueSelector, reduxForm } from 'redux-form';
import queryString from 'query-string';

Expand Down
29 changes: 20 additions & 9 deletions packages/jaeger-ui/src/components/SearchTracePage/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import { BrowserRouter, MemoryRouter } from 'react-router-dom';
import { Routes, Route, CompatRouter } from 'react-router-dom-v5-compat';
import { MemoryRouter } from 'react-router-dom';

jest.mock('redux-form', () => {
function reduxForm() {
Expand Down Expand Up @@ -43,11 +44,15 @@ import transformTraceData from '../../model/transform-trace-data';
import { store as globalStore } from '../../utils/configure-store';

const AllProvider = ({ children }) => (
<BrowserRouter>
<Provider store={globalStore}>
<MemoryRouter>{children}</MemoryRouter>
</Provider>
</BrowserRouter>
<MemoryRouter>
<CompatRouter>
<Provider store={globalStore}>
<Routes>
<Route path="/" element={children} />
</Routes>
</Provider>
</CompatRouter>
</MemoryRouter>
);

describe('<SearchTracePage>', () => {
Expand Down Expand Up @@ -99,7 +104,9 @@ describe('<SearchTracePage>', () => {
store.get = jest.fn(() => ({ service: 'svc-b' }));
wrapper = mount(
<MemoryRouter>
<SearchTracePage {...{ ...props, urlQueryParams: {} }} />
<CompatRouter>
<SearchTracePage {...{ ...props, urlQueryParams: {} }} />
</CompatRouter>
</MemoryRouter>
);
expect(props.fetchServices.mock.calls.length).toBe(1);
Expand All @@ -115,7 +122,9 @@ describe('<SearchTracePage>', () => {
store.get = jest.fn(() => ({ service: 'svc-b' }));
wrapper = mount(
<MemoryRouter>
<SearchTracePage {...props} />
<CompatRouter>
<SearchTracePage {...props} />
</CompatRouter>
</MemoryRouter>
);
expect(props.fetchServices.mock.calls.length).toBe(1);
Expand All @@ -131,7 +140,9 @@ describe('<SearchTracePage>', () => {
const historyMock = { push: historyPush };
wrapper = mount(
<MemoryRouter>
<SearchTracePage {...props} history={historyMock} query={query} />
<CompatRouter>
<SearchTracePage {...props} history={historyMock} query={query} />
</CompatRouter>
</MemoryRouter>
);
wrapper.find(SearchTracePage).first().instance().goToTrace(traceID);
Expand Down
3 changes: 1 addition & 2 deletions packages/jaeger-ui/src/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
import './site-prefix';

import React from 'react';
import { BrowserRouter } from 'react-router-dom';
import { CompatRouter } from 'react-router-dom-v5-compat';
import { CompatRouter, BrowserRouter } from 'react-router-dom-v5-compat';
import { createRoot } from 'react-dom/client';

import JaegerUIApp from './components/App';
Expand Down
8 changes: 4 additions & 4 deletions packages/jaeger-ui/src/utils/withRouteProps.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import React from 'react';
import { render } from '@testing-library/react';
import { MemoryRouter, Route } from 'react-router-dom';
import { MemoryRouter, Route, Routes } from 'react-router-dom-v5-compat';
import withRouteProps from './withRouteProps';
import { useHistory, HistoryProvider } from './useHistory';

Expand All @@ -38,9 +38,9 @@ describe('withRouteProps', () => {
render(
<HistoryProvider history={mockHistory}>
<MemoryRouter initialEntries={['/test?param=value']}>
<Route path="/test">
<ComponentWithRouteProps />
</Route>
<Routes>
<Route path="/test" element={<ComponentWithRouteProps />} />
</Routes>
</MemoryRouter>
</HistoryProvider>
);
Expand Down
2 changes: 1 addition & 1 deletion packages/jaeger-ui/src/utils/withRouteProps.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

import React from 'react';
import { useLocation, useParams } from 'react-router-dom';
import { useLocation, useParams } from 'react-router-dom-v5-compat';
import { History, Location } from 'history';
import { useHistory } from './useHistory';

Expand Down