Skip to content

Commit 96a657c

Browse files
committed
fix: typings of ComponentWithLazyPageViewModel type
1 parent b3ff650 commit 96a657c

File tree

4 files changed

+113
-11
lines changed

4 files changed

+113
-11
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"prebuild": "npm run clean && npm run check",
1010
"build": "tsc && node ./post-build.mjs",
1111
"pub": "PUBLISH=true pnpm run build",
12+
"prepublish": "pnpm test",
1213
"pub:patch": "PUBLISH=true PUBLISH_VERSION=patch pnpm run build",
1314
"pub:minor": "PUBLISH=true PUBLISH_VERSION=minor pnpm run build",
1415
"pub:major": "PUBLISH=true PUBLISH_VERSION=major pnpm run build",
@@ -41,7 +42,7 @@
4142
"peerDependencies": {
4243
"mobx": "^6.12.4",
4344
"mobx-react-lite": "^4.0.7",
44-
"mobx-vm-entities": "^5.0.10",
45+
"mobx-vm-entities": "^5.0.19",
4546
"react": "^18.3.1",
4647
"wouter": "^3.3.5"
4748
},

pnpm-lock.yaml

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import { act, render, screen } from '@testing-library/react';
2+
import { createCounter } from 'mobx-vm-entities/utils/counter';
3+
import { describe, expect, test } from 'vitest';
4+
5+
import { PageViewModelMock } from '../page-view-model/page-view-model.impl.test';
6+
7+
import { withLazyPageViewModel } from './with-lazy-page-view-model';
8+
import { PageViewModelProps } from './with-page-view-model';
9+
10+
const createIdGenerator = (prefix?: string) => {
11+
const counter = createCounter();
12+
return () => (prefix ?? '') + counter().toString();
13+
};
14+
15+
describe('withLazyPageViewModel', () => {
16+
test('renders (required path param)', async () => {
17+
class VM extends PageViewModelMock<{ foo: string }> {
18+
mount() {
19+
super.mount();
20+
}
21+
}
22+
const View = ({ model }: PageViewModelProps<VM>) => {
23+
return <div data-testid={'view'}>{`hello ${model.id}`}</div>;
24+
};
25+
const Component = withLazyPageViewModel(
26+
async () => {
27+
return {
28+
Model: VM,
29+
View,
30+
};
31+
},
32+
{
33+
generateId: createIdGenerator(),
34+
},
35+
);
36+
37+
await act(async () => render(<Component params={{ foo: 'bar' }} />));
38+
expect(screen.getByText('hello VM_0')).toBeDefined();
39+
});
40+
test('renders (optional path param)', async () => {
41+
class VM extends PageViewModelMock<{ foo?: string }> {
42+
mount() {
43+
super.mount();
44+
}
45+
}
46+
const View = ({ model }: PageViewModelProps<VM>) => {
47+
return <div data-testid={'view'}>{`hello ${model.id}`}</div>;
48+
};
49+
const Component = withLazyPageViewModel(
50+
async () => {
51+
return {
52+
Model: VM,
53+
View,
54+
};
55+
},
56+
{
57+
generateId: createIdGenerator(),
58+
},
59+
);
60+
61+
await act(async () => render(<Component />));
62+
expect(screen.getByText('hello VM_0')).toBeDefined();
63+
});
64+
test('renders (no path params)', async () => {
65+
class VM extends PageViewModelMock {
66+
mount() {
67+
super.mount();
68+
}
69+
}
70+
const View = ({ model }: PageViewModelProps<VM>) => {
71+
return <div data-testid={'view'}>{`hello ${model.id}`}</div>;
72+
};
73+
const Component = withLazyPageViewModel(
74+
async () => {
75+
return {
76+
Model: VM,
77+
View,
78+
};
79+
},
80+
{
81+
generateId: createIdGenerator(),
82+
},
83+
);
84+
85+
await act(async () => render(<Component />));
86+
expect(screen.getByText('hello VM_0')).toBeDefined();
87+
});
88+
});

src/hoc/with-lazy-page-view-model.tsx

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,32 @@ import {
33
ViewModelHocConfig,
44
withLazyViewModel,
55
} from 'mobx-vm-entities';
6+
import { LoadableMixin } from 'mobx-vm-entities/lib/react-simple-loadable';
7+
import { AnyObject } from 'mobx-vm-entities/utils/types';
68
import { ComponentType } from 'react';
79

8-
import { PageViewModel } from '../page-view-model';
10+
import { AnyPageViewModel, PageViewModel } from '../page-view-model';
911

10-
export const withLazyPageViewModel = <
12+
import {
13+
ComponentWithPageViewModel,
14+
PageViewModelProps,
15+
} from './with-page-view-model';
16+
17+
export type ComponentWithLazyPageViewModel<
18+
TViewModel extends AnyPageViewModel,
19+
TComponentOriginProps extends AnyObject = PageViewModelProps<TViewModel>,
20+
> = ComponentWithPageViewModel<TViewModel, TComponentOriginProps> &
21+
LoadableMixin;
22+
23+
export function withLazyPageViewModel<
1124
TViewModel extends PageViewModel<any, any>,
1225
TView extends ComponentType<any>,
1326
>(
1427
loadFunction: () => Promise<LazyViewAndModel<TViewModel, TView>>,
1528
config?: ViewModelHocConfig<any>,
16-
) => {
29+
): ComponentWithLazyPageViewModel<TViewModel, TView> {
1730
return withLazyViewModel(loadFunction, {
1831
...config,
1932
getPayload: config?.getPayload ?? ((props) => props.params),
20-
});
21-
};
33+
}) as unknown as ComponentWithLazyPageViewModel<TViewModel, TView>;
34+
}

0 commit comments

Comments
 (0)