Skip to content

Commit f5b1eb4

Browse files
committed
test(reactant-share): fix testing
1 parent 78bcbc8 commit f5b1eb4

File tree

7 files changed

+590
-47
lines changed

7 files changed

+590
-47
lines changed

packages/reactant-share/src/checkPatches.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { routerModuleName } from './constants';
12
import { ActionOptions } from './interfaces';
23

34
export const checkPatches = (
@@ -16,7 +17,7 @@ export const checkPatches = (
1617
if (
1718
oldState &&
1819
typeof oldState === 'object' &&
19-
path[0] !== 'reactant:router'
20+
path[0] !== routerModuleName
2021
) {
2122
const state = path.join('.');
2223
console.warn(

packages/reactant-share/src/constants.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@ export const syncToClientsName = '@@reactant:syncToClients';
1111
export const syncWorkerRouterName = '@@reactant:syncWorkerRouter';
1212

1313
export const SharedAppOptions = Symbol('SharedAppOptions');
14+
15+
export const storageModuleName = 'Storage';
16+
export const routerModuleName = 'Router';

packages/reactant-share/src/router.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ import type {
1717
} from 'reactant-router';
1818
import type { LocationState } from 'history';
1919
import {
20+
routerModuleName,
2021
SharedAppOptions,
22+
storageModuleName,
2123
syncRouterName,
2224
syncWorkerRouterName,
2325
} from './constants';
@@ -40,7 +42,7 @@ export interface IRouterOptions extends IBaseRouterOptions {
4042
}
4143

4244
@injectable({
43-
name: 'Router',
45+
name: routerModuleName,
4446
})
4547
class ReactantRouter extends BaseReactantRouter {
4648
constructor(
@@ -191,9 +193,10 @@ class ReactantRouter extends BaseReactantRouter {
191193
if (rehydrated) {
192194
stopWatching();
193195
const router = this._routers[this.portDetector.name];
194-
if (router) {
195-
this._changeRoutingOnSever(this.portDetector.name, router);
196-
}
196+
this._changeRoutingOnSever(
197+
this.portDetector.name,
198+
router ?? this.defaultHistory
199+
);
197200
}
198201
}
199202
);
@@ -288,11 +291,12 @@ class ReactantRouter extends BaseReactantRouter {
288291
}
289292

290293
protected get enableCacheRouting() {
291-
const { Storage } = (this as any)[modulesKey];
294+
const Storage = (this as any)[modulesKey][storageModuleName];
295+
const routerPersistConfig = Storage?.persistConfig[routerModuleName];
292296
return (
293-
Storage?.persistConfig.Router &&
294-
(Storage.persistConfig.Router!.whitelist?.includes('_routers') ||
295-
Storage.persistConfig.Router!.blacklist?.includes('_routers') === false)
297+
routerPersistConfig &&
298+
(routerPersistConfig!.whitelist?.includes('_routers') ||
299+
routerPersistConfig!.blacklist?.includes('_routers') === false)
296300
);
297301
}
298302

@@ -304,7 +308,7 @@ class ReactantRouter extends BaseReactantRouter {
304308
hash: '',
305309
state: undefined,
306310
},
307-
};
311+
} as RouterState;
308312

309313
protected dispatchChanged(router: RouterState) {
310314
this.store?.dispatch(

packages/reactant-share/src/storage.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ import type {
1010
SetStorageOptions,
1111
} from 'reactant-storage';
1212
import { PortDetector } from './portDetector';
13+
import { storageModuleName } from './constants';
1314

1415
export interface IStorageOptions extends IBaseStorageOptions {
1516
//
1617
}
1718

1819
@injectable({
19-
name: 'Storage',
20+
name: storageModuleName,
2021
})
2122
class ReactantStorage extends BaseReactantStorage {
2223
constructor(
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
export class MemoryStorage {
2+
constructor(public data: Record<string, any> = {}) {}
3+
4+
getItem(key: string): Promise<string> {
5+
return new Promise((resolve) => {
6+
resolve(this.data[key]);
7+
});
8+
}
9+
10+
setItem(key: string, item: string) {
11+
return new Promise((resolve) => {
12+
this.data[key] = item;
13+
resolve(undefined);
14+
});
15+
}
16+
17+
removeItem(key: string) {
18+
return new Promise((resolve) => {
19+
delete this.data[key];
20+
resolve(undefined);
21+
});
22+
}
23+
}

0 commit comments

Comments
 (0)