Skip to content

Commit 945fc93

Browse files
committed
feat!: use 7.x version of mobx-location-history (api like history package), mark type deprecation
1 parent 879fed9 commit 945fc93

File tree

7 files changed

+118
-91
lines changed

7 files changed

+118
-91
lines changed

.eslintrc.cjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module.exports = {
88
'error',
99
{ ignore: Object.keys(packageJson.peerDependencies) },
1010
],
11+
'sonarjs/deprecation': 'off'
1112
},
1213
overrides: [
1314
{
@@ -23,7 +24,8 @@ module.exports = {
2324
'@typescript-eslint/ban-ts-comment': 'off',
2425
'@typescript-eslint/no-this-alias': 'off',
2526
'react-hooks/rules-of-hooks': 'off',
26-
'sonarjs/use-type-alias': 'off'
27+
'sonarjs/use-type-alias': 'off',
28+
'sonarjs/deprecation': 'off'
2729
},
2830
parserOptions: {
2931
project: 'tsconfig.test.json',

README.md

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,28 @@ _**MobX** integration with **Wouter**_
2020

2121
## What package has
2222

23+
### [`Router`](src/router/router.ts)
24+
25+
Router for client navigation
26+
27+
### Usage
28+
29+
```ts
30+
import { Router, Location, History, QueryParams } from "mobx-wouter";
31+
32+
const router = new Router({
33+
history: //, new Location()
34+
queryParams: //, new QueryParams(),
35+
abortSignal: //
36+
})
37+
```
38+
39+
40+
### [`Location`, `History`, `QueryParams` and etc](https://github.com/js2me/mobx-location-history)
41+
42+
Exports from [mobx-location-history](https://github.com/js2me/mobx-location-history)
43+
44+
2345
### [`PageViewModelBase`](src/page-view-model/page-view-model.base.ts), [`PageViewModel`](src/page-view-model/page-view-model.ts)
2446

2547
Simple [`ViewModel`](https://github.com/js2me/mobx-view-model?tab=readme-ov-file#mobx-view-model) wrapper for pages
@@ -63,23 +85,3 @@ export const HomePage = withPageViewModel(HomePageVM)(HomePageView);
6385

6486
Same as [`withPageViewModel()`](src/page-view-model/with-page-view-model.tsx) but with lazy loading view and view model
6587

66-
### [`MobxRouter`](src/router/router.ts)
67-
68-
Router for client navigation
69-
70-
### [`Location`, `History`, `QueryParams` and etc](https://github.com/js2me/mobx-location-history)
71-
72-
Exports from [mobx-location-history](https://github.com/js2me/mobx-location-history)
73-
74-
75-
### Usage
76-
77-
```ts
78-
import { Router, Location, History, QueryParams } from "mobx-wouter";
79-
80-
const router = new Router({
81-
history: //, new Location()
82-
queryParams: //, new QueryParams(),
83-
abortSignal: //
84-
})
85-
```

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
},
4040
"type": "module",
4141
"dependencies": {
42-
"mobx-location-history": "^5.1.0",
42+
"mobx-location-history": "^7.0.3",
4343
"yummies": "^3.2.6"
4444
},
4545
"peerDependencies": {

pnpm-lock.yaml

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

src/router/router.test.ts

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
1-
import { History, To } from 'mobx-location-history';
1+
import { createBrowserHistory, History } from 'mobx-location-history';
22
import { beforeEach, describe, expect, test, vi } from 'vitest';
33

44
import { Router } from './router.js';
5-
import {
6-
RouterNavigateParams,
7-
RouterToConfig,
8-
RouterType,
9-
} from './router.types.js';
5+
import { RouterNavigateParams, RouterToConfig } from './router.types.js';
106

11-
export class HistoryMock extends History {
12-
spies = {
13-
push: vi.fn((to: To, state?: any) => super.push(to, state)),
14-
replace: vi.fn((to: To, state?: any) => super.replace(to, state)),
7+
type RouterType = 'browser' | 'hash';
8+
9+
export const mockHistory = (history: History) => {
10+
const spies = {
11+
push: vi.spyOn(history, 'push'),
12+
replace: vi.spyOn(history, 'replace'),
1513
};
1614

17-
push(to: To, state?: any): void {
18-
return this.spies.push(to, state);
19-
}
15+
const clearMocks = () => {
16+
spies.push.mockClear();
17+
spies.replace.mockClear();
18+
};
2019

21-
replace(to: To, state?: any): void {
22-
return this.spies.replace(to, state);
23-
}
24-
}
20+
return {
21+
...history,
22+
spies,
23+
clearMocks,
24+
};
25+
};
2526

2627
describe('router', () => {
27-
const mockHistory = new HistoryMock();
28+
const historyMock = mockHistory(createBrowserHistory());
2829

2930
beforeEach(() => {
3031
history.replaceState(null, '', '/');
3132
location.hash = '';
32-
mockHistory.spies.push.mockClear();
33-
mockHistory.spies.replace.mockClear();
33+
historyMock.clearMocks();
3434
});
3535

3636
type NavigateTestConfig = {
@@ -500,7 +500,7 @@ describe('router', () => {
500500

501501
test(testName, () => {
502502
const mockRouter = new Router({
503-
history: mockHistory,
503+
history: historyMock,
504504
type,
505505
});
506506

@@ -509,19 +509,22 @@ describe('router', () => {
509509
expect(location.href).toBe(expectedHref);
510510

511511
if (pushStateCalls != null) {
512-
expect(mockHistory.spies.push).toHaveBeenCalledTimes(pushStateCalls);
512+
expect(historyMock.spies.push).toHaveBeenCalledTimes(pushStateCalls);
513513
}
514514
if (replaceStateCalls != null) {
515-
expect(mockHistory.spies.replace).toHaveBeenCalledTimes(
515+
expect(historyMock.spies.replace).toHaveBeenCalledTimes(
516516
replaceStateCalls,
517517
);
518518
}
519519
if (expectedSearch != null) {
520520
expect(location.search).toBe(expectedSearch);
521521
}
522522
if (args?.[1]?.state != null) {
523-
expect(mockHistory.state).toStrictEqual(args[1].state);
524-
expect(history.state).toStrictEqual(args[1].state);
523+
expect(historyMock.location.state).toStrictEqual(args[1].state);
524+
expect(history.state).toStrictEqual({
525+
...history.state,
526+
usr: args[1].state,
527+
});
525528
}
526529
});
527530
};

src/router/router.ts

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import {
2-
IQueryParams,
32
History,
3+
IQueryParams,
44
QueryParams,
55
buildSearchString,
6-
AnyHistory,
6+
createBrowserHistory,
7+
createHashHistory,
78
} from 'mobx-location-history';
89
import { startTransition } from 'react';
910

@@ -13,27 +14,27 @@ import {
1314
RouterNavigateParams,
1415
RouterPath,
1516
RouterToConfig,
16-
RouterType,
1717
} from './router.types.js';
1818

19-
export class Router<THistory extends AnyHistory = AnyHistory>
19+
export class Router<THistory extends History = History>
2020
implements IRouter<THistory>
2121
{
2222
history: THistory;
23-
location: THistory['location'];
2423
queryParams: IQueryParams;
2524
baseUrl: string | undefined;
26-
type: RouterType;
2725

2826
constructor(protected config: RouterConfig<THistory>) {
2927
this.baseUrl = config.baseUrl;
30-
this.type = config.type ?? 'browser';
3128
this.history =
32-
config.history ??
33-
(new History({
34-
abortSignal: config.abortSignal,
35-
}) as unknown as THistory);
36-
this.location = config.location ?? this.history.location;
29+
config.history ?? (createBrowserHistory() as unknown as THistory);
30+
if (config.history) {
31+
this.history = config.history;
32+
} else if (config.type === 'hash') {
33+
this.history = createHashHistory() as unknown as THistory;
34+
} else {
35+
this.history = createBrowserHistory() as unknown as THistory;
36+
}
37+
3738
this.queryParams =
3839
config.queryParams ?? new QueryParams({ history: this.history });
3940
}
@@ -62,12 +63,16 @@ export class Router<THistory extends AnyHistory = AnyHistory>
6263
}
6364
}
6465

65-
createUrl(to: RouterToConfig, type?: RouterType): string {
66+
get location() {
67+
return this.history.location;
68+
}
69+
70+
createUrl(to: RouterToConfig): string {
6671
const path = this.createPath(to);
6772

6873
return [
6974
path.baseUrl,
70-
type === 'hash' ? '#' : '',
75+
this.config.type === 'hash' ? '#' : '',
7176
path.pathname,
7277
path.hash && `#${path.hash}`,
7378
path.search,
@@ -76,18 +81,15 @@ export class Router<THistory extends AnyHistory = AnyHistory>
7681

7782
protected hashNavigate(to: RouterToConfig, options?: RouterNavigateParams) {
7883
const path = this.createPath(to);
79-
const url = this.createUrl(
80-
{
81-
...path,
82-
// This is fixes bug with pathname endings /
83-
// If location.pathname is /test-foo then after navigation to /test-foo#bar
84-
// navigation back will not work
85-
// If location.pathname is /test-foo/ then after navigation to /test-foo/#/bar
86-
// navigation back will not work
87-
baseUrl: this.location.pathname,
88-
},
89-
this.type,
90-
);
84+
const url = this.createUrl({
85+
...path,
86+
// This is fixes bug with pathname endings /
87+
// If location.pathname is /test-foo then after navigation to /test-foo#bar
88+
// navigation back will not work
89+
// If location.pathname is /test-foo/ then after navigation to /test-foo/#/bar
90+
// navigation back will not work
91+
baseUrl: this.location.pathname,
92+
});
9193
const state = options?.state ?? null;
9294

9395
this.wrapInViewTransition(() => {
@@ -101,7 +103,7 @@ export class Router<THistory extends AnyHistory = AnyHistory>
101103
options?: RouterNavigateParams,
102104
) {
103105
const path = this.createPath(to);
104-
const url = this.createUrl(path, this.type);
106+
const url = this.createUrl(path);
105107
const state = options?.state ?? null;
106108

107109
this.wrapInViewTransition(() => {
@@ -140,7 +142,7 @@ export class Router<THistory extends AnyHistory = AnyHistory>
140142
}
141143

142144
navigate(to: RouterToConfig, options?: RouterNavigateParams): void {
143-
if (this.type === 'hash') {
145+
if (this.config.type === 'hash') {
144146
this.hashNavigate(to, options);
145147
} else {
146148
this.browserNavigate(to, options);
@@ -158,6 +160,6 @@ export class Router<THistory extends AnyHistory = AnyHistory>
158160
*/
159161
export const MobxRouter = Router;
160162

161-
export const createRouter = <THistory extends AnyHistory>(
163+
export const createRouter = <THistory extends History>(
162164
config: RouterConfig<THistory>,
163165
) => new Router<THistory>(config);

0 commit comments

Comments
 (0)