Skip to content

Commit 2950d52

Browse files
committed
fix routeError tests, and useRouteActivatedHook deps
1 parent 8b02a32 commit 2950d52

File tree

6 files changed

+25
-43
lines changed

6 files changed

+25
-43
lines changed

static/app/types/legacyReactRouter.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type {UIMatch} from 'react-router-dom';
12
/**
23
* These are vendored from react-router v3
34
*
@@ -71,6 +72,7 @@ export interface WithRouterProps<P = Record<string, string | undefined>, Q = any
7172

7273
export interface RouteContextInterface<P = Record<string, string | undefined>, Q = any> {
7374
location: Location<Q>;
75+
matches: Array<UIMatch<unknown, unknown>>;
7476
params: P;
7577
router: InjectedRouter<P, Q>;
7678
routes: PlainRoute[];

static/app/utils/getRouteStringFromRoutes.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ export type Props =
2121
* concatenate all of the following routes. Skips any routes without a path
2222
*
2323
* @param params.matches An array of UIMatch objects from react-router-dom `useMatches()`
24-
* @param @deprecated params.routes An array of route objects from react-router
2524
* @returns A route path string
2625
*/
2726
export function getRouteStringFromRoutes({routes, matches}: Props): string {

static/app/views/routeAnalyticsContextProvider.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {createContext, useMemo} from 'react';
2+
import {useMatches} from 'react-router-dom';
23

34
import {HookStore} from 'sentry/stores/hookStore';
45
import type {RouteContextInterface} from 'sentry/types/legacyReactRouter';
@@ -45,6 +46,7 @@ export function RouteAnalyticsContextProvider({children}: Props) {
4546
routes: useRoutes(),
4647
router: useRouter(),
4748
location: useLocation(),
49+
matches: useMatches(),
4850
};
4951

5052
const {

static/app/views/routeError.spec.tsx

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,11 @@ import * as Sentry from '@sentry/react';
22

33
import {render, waitFor} from 'sentry-test/reactTestingLibrary';
44

5-
import {useRoutes} from 'sentry/utils/useRoutes';
5+
import {getRouteStringFromRoutes} from 'sentry/utils/getRouteStringFromRoutes';
66
import RouteError from 'sentry/views/routeError';
77

8-
jest.mock('sentry/utils/useRoutes');
9-
10-
jest
11-
.mocked(useRoutes)
12-
.mockReturnValue([
13-
{path: '/'},
14-
{path: '/:orgId/'},
15-
{path: '/organizations/:orgId/'},
16-
{path: 'api-keys/'},
17-
]);
8+
jest.mock('sentry/utils/getRouteStringFromRoutes');
9+
jest.mocked(getRouteStringFromRoutes).mockReturnValue('/organizations/:orgId/api-keys/');
1810

1911
describe('RouteError', () => {
2012
it('captures errors with sentry', async () => {

static/gsApp/hooks/useRouteActivatedHook.spec.tsx

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import type {UIMatch} from 'react-router-dom';
12
import {LocationFixture} from 'sentry-fixture/locationFixture';
23
import {OrganizationFixture} from 'sentry-fixture/organization';
3-
import {RouterPropsFixture} from 'sentry-fixture/routerPropsFixture';
44

55
import {ProjectFixture} from 'getsentry-test/fixtures/project';
66
import {SubscriptionFixture} from 'getsentry-test/fixtures/subscription';
@@ -18,6 +18,13 @@ const HALF_ADVANCE_PERIOD = DELAY_TIME_MS * 0.6;
1818

1919
jest.mock('getsentry/utils/rawTrackAnalyticsEvent');
2020

21+
function makeMatch(path: string): UIMatch {
22+
return {id: path, pathname: path, params: {}, data: undefined, handle: {path}};
23+
}
24+
25+
const SETTINGS_MATCHES = [makeMatch('/settings/:orgId/projects/:projectId/')];
26+
const RELEASES_MATCHES = [makeMatch('/organizations/:orgId/releases/:release/')];
27+
2128
describe('useRouteActivatedHook', () => {
2229
const organization = OrganizationFixture();
2330
const project = ProjectFixture({organization});
@@ -27,18 +34,13 @@ describe('useRouteActivatedHook', () => {
2734
function genProps(extraRouteParams = {}): any {
2835
const props = {
2936
organization,
30-
...RouterPropsFixture({
31-
location: LocationFixture({
32-
pathname: `/settings/${organization.slug}/${project.slug}/`,
33-
}),
34-
params: {orgId: organization.slug},
35-
routes: [
36-
{path: '/'},
37-
{path: '/settings/'},
38-
{path: ':orgId/'},
39-
{path: 'projects/:projectId/'},
40-
],
37+
location: LocationFixture({
38+
pathname: `/settings/${organization.slug}/${project.slug}/`,
4139
}),
40+
params: {orgId: organization.slug},
41+
routes: [],
42+
router: {} as any,
43+
matches: SETTINGS_MATCHES,
4244
...extraRouteParams,
4345
};
4446
return props;
@@ -173,14 +175,7 @@ describe('useRouteActivatedHook', () => {
173175
location: LocationFixture({
174176
pathname: `/organizations/${organization.slug}/releases/some-release/`,
175177
}),
176-
routes: [
177-
{path: '/'},
178-
{path: '/organizations/:orgId/releases/'},
179-
{path: ':release/'},
180-
],
181-
...LocationFixture({
182-
pathname: `/organizations/${organization.slug}/releases/some-release/`,
183-
}),
178+
matches: RELEASES_MATCHES,
184179
});
185180
const loadTime = Date.now();
186181
act(() => rerender(newProps));
@@ -225,6 +220,7 @@ describe('useRouteActivatedHook', () => {
225220
it('route changes triggers early analytics event', () => {
226221
jest.useFakeTimers();
227222
let loadTime = Date.now();
223+
228224
const {result, rerender} = renderHook(useRouteActivatedHook, {
229225
initialProps: props,
230226
});
@@ -235,14 +231,7 @@ describe('useRouteActivatedHook', () => {
235231
location: LocationFixture({
236232
pathname: `/organizations/${organization.slug}/releases/some-release/`,
237233
}),
238-
routes: [
239-
{path: '/'},
240-
{path: '/organizations/:orgId/releases/'},
241-
{path: ':release/'},
242-
],
243-
...LocationFixture({
244-
pathname: `/organizations/${organization.slug}/releases/some-release/`,
245-
}),
234+
matches: RELEASES_MATCHES,
246235
});
247236
expect(rawTrackAnalyticsEvent).toHaveBeenCalledTimes(0);
248237

static/gsApp/hooks/useRouteActivatedHook.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import {useCallback, useEffect, useState} from 'react';
2-
import {useMatches} from 'react-router-dom';
32

43
import type {Hooks} from 'sentry/types/hooks';
54
import type {Organization} from 'sentry/types/organization';
@@ -18,8 +17,7 @@ export const DELAY_TIME_MS = 7000;
1817

1918
type Props = Parameters<Hooks['react-hook:route-activated']>[0];
2019

21-
export function useRouteActivatedHook({location}: Props) {
22-
const matches = useMatches();
20+
export function useRouteActivatedHook({location, matches}: Props) {
2321
const [analyticsParams, _setRouteAnalyticsParams] = useState({});
2422
const [disableAnalytics, _setDisableRouteAnalytics] = useState(false);
2523
const [hasSentAnalytics, setHasSentAnalytics] = useState(false);

0 commit comments

Comments
 (0)