Skip to content

Commit 349a3db

Browse files
authored
Add compatibility for goBack and goForward with history@5 (#163)
* In #113 compatibility with history@5 was added. In history@5 the `goForward` and `goBack` functions were renamed to `forward` and `back` though. This means that `react-resource-router` will currently fail on these actions. This PR fixes that and adds compatibility for these two actions with history@5 * Type fix for union type of history@4 and history@5 in unit test * Explain in comment which method is used in history@4 vs history@5
1 parent c4a4e05 commit 349a3db

File tree

5 files changed

+55
-9
lines changed

5 files changed

+55
-9
lines changed

package-lock.json

Lines changed: 23 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
"@testing-library/react": "^10.0.4",
9494
"@types/enzyme": "^3.10.5",
9595
"@types/history": "^4.7.6",
96+
"@types/history-5": "npm:@types/history@^5.0.0",
9697
"@types/jest": "^28.1.7",
9798
"@types/lodash.debounce": "^4.0.6",
9899
"@types/lodash.noop": "^3.0.6",

src/__tests__/unit/common/utils/history/test.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,11 @@ describe('createLegacyHistory', () => {
138138
it('should go back in history via history', () => {
139139
const history = createLegacyHistory();
140140
window.history.back = jest.fn();
141-
history.goBack();
141+
if ('goBack' in history) {
142+
history.goBack();
143+
} else {
144+
throw new Error('goBack was expected but not found');
145+
}
142146
expect(window.history.back).toHaveBeenCalled();
143147
});
144148
});
@@ -147,7 +151,11 @@ describe('createLegacyHistory', () => {
147151
it('should go forward in history via history', () => {
148152
const history = createLegacyHistory();
149153
window.history.forward = jest.fn();
150-
history.goForward();
154+
if ('goForward' in history) {
155+
history.goForward();
156+
} else {
157+
throw new Error('goForward was expected but not found');
158+
}
151159
expect(window.history.forward).toHaveBeenCalled();
152160
});
153161
});

src/common/types.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
} from 'react';
1010

1111
import { History, Location as HistoryLocationShape } from 'history';
12+
import { History as History5 } from 'history-5';
1213

1314
export type LocationShape = HistoryLocationShape;
1415

@@ -20,10 +21,10 @@ export type Location = {
2021
hash: string;
2122
};
2223

23-
export type BrowserHistory = Omit<
24-
History,
25-
'location' | 'go' | 'createHref' | 'push' | 'replace'
26-
> & {
24+
export type BrowserHistory = (
25+
| Omit<History, 'location' | 'go' | 'createHref' | 'push' | 'replace'>
26+
| Omit<History5, 'location' | 'go' | 'createHref' | 'push' | 'replace'>
27+
) & {
2728
location: Location;
2829
push: (path: string) => void;
2930
replace: (path: string) => void;

src/controllers/router-store/index.tsx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,15 +309,29 @@ const actions: AllRouterActions = {
309309
({ getState }) => {
310310
const { history } = getState();
311311

312-
history.goBack();
312+
// history@4 uses goBack(), history@5 uses back()
313+
if ('goBack' in history) {
314+
history.goBack();
315+
} else if ('back' in history) {
316+
history.back();
317+
} else {
318+
throw new Error('History does not support goBack');
319+
}
313320
},
314321

315322
goForward:
316323
() =>
317324
({ getState }) => {
318325
const { history } = getState();
319326

320-
history.goForward();
327+
// history@4 uses goForward(), history@5 uses forward()
328+
if ('goForward' in history) {
329+
history.goForward();
330+
} else if ('forward' in history) {
331+
history.forward();
332+
} else {
333+
throw new Error('History does not support goForward');
334+
}
321335
},
322336

323337
registerBlock:

0 commit comments

Comments
 (0)