Skip to content

Commit d52fd2d

Browse files
renovate[bot]afonsojramosBrendan Mulholland
authored
fix(deps): update react-router monorepo to v6 (major) (#635)
* fix(deps): update react-router monorepo to v6 * chore: re-apply v6 changes * fix: `react-router` tests * Remove duplicate route * Replace route that missed it in conversion --------- Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: Afonso Jorge Ramos <[email protected]> Co-authored-by: Brendan Mulholland <[email protected]>
1 parent 78c08d7 commit d52fd2d

13 files changed

+136
-139
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@
118118
"react-dom": "18.2.0",
119119
"react-emojione": "5.0.1",
120120
"react-final-form": "6.5.9",
121-
"react-router": "5.3.4",
122-
"react-router-dom": "5.3.4",
121+
"react-router": "6.16.0",
122+
"react-router-dom": "6.16.0",
123123
"react-transition-group": "4.4.5",
124124
"ts-loader": "9.4.4",
125125
"typescript": "5.2.2"

pnpm-lock.yaml

Lines changed: 22 additions & 42 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/app.tsx

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import React, { useContext } from 'react';
22
import {
3-
Redirect,
3+
Navigate,
44
HashRouter as Router,
55
Route,
6-
Switch,
6+
Routes,
77
useLocation,
88
} from 'react-router-dom';
99

@@ -23,7 +23,7 @@ function RequireAuth({ children }) {
2323
return isLoggedIn ? (
2424
children
2525
) : (
26-
<Redirect to={{ pathname: '/login', state: { from: location } }} />
26+
<Navigate to="/login" replace state={{ from: location }} />
2727
);
2828
}
2929

@@ -34,31 +34,30 @@ export const App = () => {
3434
<div className="flex flex-col pl-14 h-full">
3535
<Loading />
3636
<Sidebar />
37-
38-
<Switch>
39-
<Route path="/" exact>
40-
<RequireAuth>
41-
<NotificationsRoute />
42-
</RequireAuth>
43-
</Route>
44-
<Route path="/settings" exact>
45-
<RequireAuth>
46-
<SettingsRoute />
47-
</RequireAuth>
48-
</Route>
49-
<Route path="/login">
50-
<LoginRoute />
51-
</Route>
52-
<Route path="/login">
53-
<LoginRoute />
54-
</Route>
55-
<Route path="/login-enterprise">
56-
<LoginEnterpriseRoute />
57-
</Route>
58-
<Route path="/login-token">
59-
<LoginWithToken />
60-
</Route>
61-
</Switch>
37+
<Routes>
38+
<Route
39+
path="/"
40+
element={
41+
<RequireAuth>
42+
<NotificationsRoute />
43+
</RequireAuth>
44+
}
45+
/>
46+
<Route
47+
path="/settings"
48+
element={
49+
<RequireAuth>
50+
<SettingsRoute />
51+
</RequireAuth>
52+
}
53+
/>
54+
<Route path="/login" element={<LoginRoute />} />
55+
<Route
56+
path="/login-enterprise"
57+
element={<LoginEnterpriseRoute />}
58+
/>
59+
<Route path="/login-token" element={<LoginWithToken />} />
60+
</Routes>
6261
</div>
6362
</Router>
6463
</AppProvider>

src/components/Sidebar.test.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ import { mockedAccountNotifications } from '../__mocks__/mockedData';
1212
import { AppContext } from '../context/App';
1313
import { Sidebar } from './Sidebar';
1414

15+
const mockNavigate = jest.fn();
16+
jest.mock('react-router-dom', () => ({
17+
...jest.requireActual('react-router-dom'),
18+
useNavigate: () => mockNavigate,
19+
}));
20+
1521
describe('components/Sidebar.tsx', () => {
1622
const fetchNotifications = jest.fn();
1723
const history = createMemoryHistory();
@@ -73,17 +79,15 @@ describe('components/Sidebar.tsx', () => {
7379
});
7480

7581
it('go to the settings route', () => {
76-
const pushMock = jest.spyOn(history, 'push');
77-
7882
const { getByLabelText } = render(
7983
<AppContext.Provider value={{ isLoggedIn: true, notifications: [] }}>
80-
<Router history={history}>
84+
<Router location={history.location} navigator={history}>
8185
<Sidebar />
8286
</Router>
8387
</AppContext.Provider>,
8488
);
8589
fireEvent.click(getByLabelText('Settings'));
86-
expect(pushMock).toHaveBeenCalledTimes(1);
90+
expect(mockNavigate).toHaveBeenNthCalledWith(1, '/settings');
8791
});
8892

8993
it('opens github in the notifications page', () => {

src/components/Sidebar.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { BellIcon } from '@primer/octicons-react';
22
import { ipcRenderer, shell } from 'electron';
33
import React, { useCallback, useContext, useMemo } from 'react';
4-
import { useHistory, useLocation } from 'react-router-dom';
4+
import { useNavigate, useLocation } from 'react-router-dom';
55

66
import { Logo } from '../components/Logo';
77
import { AppContext } from '../context/App';
@@ -11,7 +11,7 @@ import { IconRefresh } from '../icons/Refresh';
1111
import { Constants } from '../utils/constants';
1212

1313
export const Sidebar: React.FC = () => {
14-
const history = useHistory();
14+
const navigate = useNavigate();
1515
const location = useLocation();
1616

1717
const { isLoggedIn } = useContext(AppContext);
@@ -66,7 +66,7 @@ export const Sidebar: React.FC = () => {
6666
<button
6767
className={footerButtonClasses}
6868
onClick={() => {
69-
history.replace('/');
69+
navigate('/', { replace: true });
7070
fetchNotifications();
7171
}}
7272
aria-label="Refresh Notifications"
@@ -78,9 +78,9 @@ export const Sidebar: React.FC = () => {
7878
className={footerButtonClasses}
7979
onClick={() => {
8080
if (location.pathname.startsWith('/settings')) {
81-
history.replace('/');
81+
navigate('/', { replace: true });
8282
} else {
83-
history.push('/settings');
83+
navigate('/settings');
8484
}
8585
}}
8686
aria-label="Settings"

src/routes/Login.test.tsx

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,17 @@ const { ipcRenderer } = require('electron');
1010
import { AppContext } from '../context/App';
1111
import { LoginRoute } from './Login';
1212

13+
const mockNavigate = jest.fn();
14+
jest.mock('react-router-dom', () => ({
15+
...jest.requireActual('react-router-dom'),
16+
useNavigate: () => mockNavigate,
17+
}));
18+
1319
describe('routes/Login.tsx', () => {
1420
const history = createMemoryHistory();
15-
const pushMock = jest.spyOn(history, 'push');
16-
const replaceMock = jest.spyOn(history, 'replace');
17-
18-
beforeEach(function () {
19-
pushMock.mockReset();
2021

22+
beforeEach(() => {
23+
mockNavigate.mockReset();
2124
jest.spyOn(ipcRenderer, 'send');
2225
});
2326

@@ -34,35 +37,34 @@ describe('routes/Login.tsx', () => {
3437
it('should redirect to notifications once logged in', () => {
3538
const { rerender } = render(
3639
<AppContext.Provider value={{ isLoggedIn: false }}>
37-
<Router history={history}>
40+
<Router location={history.location} navigator={history}>
3841
<LoginRoute />
3942
</Router>
4043
</AppContext.Provider>,
4144
);
4245

4346
rerender(
4447
<AppContext.Provider value={{ isLoggedIn: true }}>
45-
<Router history={history}>
48+
<Router location={history.location} navigator={history}>
4649
<LoginRoute />
4750
</Router>
4851
</AppContext.Provider>,
4952
);
5053

5154
expect(ipcRenderer.send).toHaveBeenCalledTimes(1);
5255
expect(ipcRenderer.send).toHaveBeenCalledWith('reopen-window');
53-
expect(replaceMock).toHaveBeenCalledTimes(1);
56+
expect(mockNavigate).toHaveBeenNthCalledWith(1, '/', { replace: true });
5457
});
5558

5659
it('should navigate to login with github enterprise', () => {
5760
const { getByLabelText } = render(
58-
<Router history={history}>
61+
<Router location={history.location} navigator={history}>
5962
<LoginRoute />
6063
</Router>,
6164
);
6265

6366
fireEvent.click(getByLabelText('Login with GitHub Enterprise'));
6467

65-
expect(pushMock).toHaveBeenCalledTimes(1);
66-
expect(pushMock).toHaveBeenCalledWith('/login-enterprise');
68+
expect(mockNavigate).toHaveBeenNthCalledWith(1, '/login-enterprise');
6769
});
6870
});

src/routes/Login.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
const { ipcRenderer } = require('electron');
22

33
import React, { useCallback, useContext, useEffect } from 'react';
4-
import { useHistory } from 'react-router-dom';
4+
import { useNavigate } from 'react-router-dom';
55

66
import { AppContext } from '../context/App';
77
import { Logo } from '../components/Logo';
88

99
export const LoginRoute: React.FC = () => {
10-
const history = useHistory();
10+
const navigate = useNavigate();
1111
const { isLoggedIn, login } = useContext(AppContext);
1212

1313
useEffect(() => {
1414
if (isLoggedIn) {
1515
ipcRenderer.send('reopen-window');
16-
history.replace('/');
16+
navigate('/', { replace: true });
1717
}
1818
}, [isLoggedIn]);
1919

@@ -46,15 +46,15 @@ export const LoginRoute: React.FC = () => {
4646

4747
<button
4848
className={loginButtonClass}
49-
onClick={() => history.push('/login-enterprise')}
49+
onClick={() => navigate('/login-enterprise')}
5050
aria-label="Login with GitHub Enterprise"
5151
>
5252
Login to GitHub Enterprise
5353
</button>
5454

5555
<button
5656
className="bg-none hover:text-gray-800 dark:text-gray-100 dark:hover:text-gray-300 mt-4 focus:outline-none"
57-
onClick={() => history.push('/login-token')}
57+
onClick={() => navigate('/login-token')}
5858
aria-label="Login with Personal Token"
5959
>
6060
<small>or login with a personal token</small>

0 commit comments

Comments
 (0)