Skip to content

Commit 61d6e41

Browse files
committed
test: add tests for useViewModel hook; internal: rename context ActiveViewContext -> ActiveViewModelContext (id value string -> AnyViewModel value)
1 parent 6ec365f commit 61d6e41

File tree

7 files changed

+402
-18
lines changed

7 files changed

+402
-18
lines changed

.eslintrc.cjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ module.exports = {
2222
'unicorn/consistent-function-scoping': 'off',
2323
'unicorn/no-this-assignment': 'off',
2424
'@typescript-eslint/ban-ts-comment': 'off',
25-
'@typescript-eslint/no-this-alias': 'off'
25+
'@typescript-eslint/no-this-alias': 'off',
26+
'react-hooks/rules-of-hooks': 'off',
2627
},
2728
parserOptions: {
2829
project: 'tsconfig.test.json',

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "4.2.0",
44
"scripts": {
55
"clean": "rimraf dist",
6-
"check": "eslint . --fix && pnpm test",
6+
"check": "eslint . --fix && pnpm test:coverage",
77
"prebuild": "npm run clean && npm run check",
88
"build": "tsc && node ./post-build.mjs",
99
"pub": "PUBLISH=true pnpm run build",
@@ -12,7 +12,8 @@
1212
"pub:major": "PUBLISH=true PUBLISH_VERSION=major pnpm run build",
1313
"test": "vitest run",
1414
"test:watch": "vitest watch",
15-
"test:coverage": "vitest run --coverage"
15+
"test:coverage": "vitest run --coverage",
16+
"dev": "pnpm test:watch"
1617
},
1718
"keywords": [
1819
"mobx",
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { createContext } from 'react';
22

3-
// will contains the view model id
4-
export const ActiveViewContext = createContext<string>('');
3+
import { AnyViewModel } from '../view-model';
4+
5+
// will contains the view model
6+
export const ActiveViewModelContext = createContext<AnyViewModel>(null as any);

src/hoc/with-view-model.tsx

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

12-
import { ActiveViewContext, ViewModelsContext } from '../contexts';
12+
import { ActiveViewModelContext, ViewModelsContext } from '../contexts';
1313
import { generateVMId } from '../utils';
1414
import { AnyObject, Class, EmptyObject, Maybe } from '../utils/types';
1515
import { AnyViewModel, ViewModel, ViewModelCreateConfig } from '../view-model';
@@ -96,15 +96,15 @@ export function withViewModel(
9696

9797
const idRef = useRef<string>('');
9898
const viewModels = useContext(ViewModelsContext);
99-
const parentViewModelId = useContext(ActiveViewContext) || null;
99+
const parentViewModel = useContext(ActiveViewModelContext) || null;
100100

101101
if (!idRef.current) {
102102
idRef.current =
103103
viewModels?.generateViewModelId({
104104
ctx,
105105
id: config?.id,
106106
VM: Model,
107-
parentViewModelId,
107+
parentViewModelId: parentViewModel?.id,
108108
fallback: config?.fallback,
109109
instances,
110110
}) ??
@@ -117,12 +117,12 @@ export function withViewModel(
117117
if (!instances.has(id)) {
118118
const configCreate: ViewModelCreateConfig<any> = {
119119
id,
120-
parentViewModelId,
120+
parentViewModelId: parentViewModel?.id,
121121
payload,
122122
VM: Model,
123123
viewModels,
124124
parentViewModel:
125-
(parentViewModelId && instances.get(parentViewModelId)) || null,
125+
(parentViewModel && instances.get(parentViewModel?.id)) || null,
126126
fallback: config?.fallback,
127127
instances,
128128
ctx,
@@ -157,9 +157,9 @@ export function withViewModel(
157157

158158
if ((!viewModels || viewModels.isAbleToRenderView(id)) && instance) {
159159
return (
160-
<ActiveViewContext.Provider value={id}>
160+
<ActiveViewModelContext.Provider value={instance}>
161161
<Component {...(componentProps as any)} model={instance} />
162-
</ActiveViewContext.Provider>
162+
</ActiveViewModelContext.Provider>
163163
);
164164
}
165165

0 commit comments

Comments
 (0)