Skip to content

Commit 3813611

Browse files
update routeHasChanged() to check route name and group instead of object reference (#125)
* update routeHasChanged() to check route name and group instead of object ref * ignore group when checking routeHasChanged * update test name * remove group, canTransitionOut and canTransitionIn from types * check path at routeHasChanged() Co-authored-by: Liam Ma <[email protected]>
1 parent e8ebfc0 commit 3813611

File tree

6 files changed

+23
-57
lines changed

6 files changed

+23
-57
lines changed

src/__tests__/unit/controllers/resource-store/utils/get-resources-for-next-location/test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,12 @@ describe('getResourcesForNextLocation()', () => {
6969
describe('when the next route does not match the prev route', () => {
7070
it('should request all resources on the next route', async () => {
7171
const prevRoute = {
72+
name: 'prev-route',
7273
path: '/prev-route',
7374
resources: [mockResource],
7475
};
7576
const nextRoute = {
77+
name: 'next-route',
7678
path: '/next-route',
7779
resources: [
7880
mockResource,

src/__tests__/unit/controllers/resource-store/utils/route-checks/test.ts

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,33 @@ import {
33
routeHasResources,
44
} from '../../../../../../controllers/resource-store/utils/route-checks';
55

6+
const mockRoute = {
7+
name: 'foo',
8+
path: '/some-path',
9+
component: () => null,
10+
};
11+
612
describe('routeHasChanged()', () => {
7-
it('should return true if the route objects do not match', () => {
13+
it('should return true if the route name does not match', () => {
814
expect(
9-
routeHasChanged(
10-
// @ts-ignore - not providing all properties on mock
11-
{
12-
path: '/some-path',
13-
component: () => null,
14-
},
15-
{
16-
path: '/another-path',
17-
component: () => null,
18-
}
19-
)
15+
routeHasChanged(mockRoute, {
16+
...mockRoute,
17+
name: 'bar',
18+
})
2019
).toBeTruthy();
2120
});
2221

23-
it('should return true if the prev route is null', () => {
22+
it('should return true if the route path does not match', () => {
2423
expect(
25-
routeHasChanged(
26-
null,
27-
// @ts-ignore - not providing all properties on mock
28-
{
29-
path: '/another-path',
30-
component: () => null,
31-
}
32-
)
24+
routeHasChanged(mockRoute, {
25+
...mockRoute,
26+
path: '/bar',
27+
})
3328
).toBeTruthy();
3429
});
3530

36-
it('should return false if the routes match', () => {
37-
const route = {
38-
path: '/some-path',
39-
component: () => null,
40-
};
41-
// @ts-ignore - not providing all properties on mock
42-
expect(routeHasChanged(route, route)).toBeFalsy();
31+
it('should return false if the route name matches', () => {
32+
expect(routeHasChanged(mockRoute, { ...mockRoute })).toBeFalsy();
4333
});
4434
});
4535

src/__tests__/unit/controllers/router-store/test.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,7 @@ describe('SPA Router store', () => {
737737
},
738738
{
739739
...mockRoute,
740+
name: 'bar',
740741
path: '/bar/a',
741742
component: ComponentB,
742743
resources: [resourceA, resourceB],

src/common/types.ts

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,6 @@ export type InvariantRoute = {
175175
path: string;
176176
exact?: boolean;
177177

178-
/** Used to prevent transitions between app groups */
179-
group?: string;
180-
181178
/** Unique name for the route */
182179
name: string;
183180

@@ -200,26 +197,6 @@ export type Route = InvariantRoute & {
200197
/** The component to render on match, typed explicitly */
201198
component: ComponentType<RouteContext>;
202199

203-
/**
204-
* Triggered before leaving the route, can trigger full page reload if returns (or resolves) false.
205-
* Defaults to true.
206-
*/
207-
canTransitionOut?: (
208-
currentRouteMatch: MatchedRoute,
209-
nextRouteMatch: MatchedRoute,
210-
props: any
211-
) => boolean | Promise<boolean>;
212-
213-
/**
214-
* Triggered before entering the route, can trigger full page reload if returns (or resolves) false.
215-
* Defaults to true.
216-
*/
217-
canTransitionIn?: (
218-
currentRouteMatch: MatchedRoute,
219-
nextRouteMatch: MatchedRoute,
220-
props: any
221-
) => boolean | Promise<boolean>;
222-
223200
/**
224201
* The resources for the route
225202
*/

src/controllers/resource-store/utils/route-checks/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,5 @@ import { Route } from '../../../../common/types';
33
export const routeHasResources = (route: Route | null): boolean =>
44
!!(route && route.resources && route.resources.length > 0);
55

6-
export const routeHasChanged = (
7-
prev: Route | null,
8-
next: Route | null
9-
): boolean => prev !== next;
6+
export const routeHasChanged = (prev: Route, next: Route): boolean =>
7+
prev.name !== next.name || prev.path !== next.path;

src/types.js.flow

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,6 @@ export type RouteResourceUpdater<T> = (
109109
export type InvariantRoute = {
110110
path: string,
111111
exact?: boolean,
112-
/** Used to prevent transitions between app groups */
113-
group?: string,
114112
/** Unique name for the route */
115113
name: string,
116114
/**

0 commit comments

Comments
 (0)