Skip to content

Commit 1df2375

Browse files
committed
test: add test for access to parentViewModel
1 parent dbf52f7 commit 1df2375

File tree

3 files changed

+72
-2
lines changed

3 files changed

+72
-2
lines changed

post-build.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ postBuildScript({
1515
commitAllCurrentChanges: true,
1616
createTag: true,
1717
githubRepoLink: 'https://github.com/js2me/mobx-view-model',
18-
cleanupCommand: 'pnpm clean && npm deprecate mobx-vm-entities "This project has been renamed. Install mobx-view-model instead."',
18+
cleanupCommand: 'pnpm clean',
1919
targetPackageJson,
2020
otherNames: ['mobx-view-model']
2121
})

src/hoc/with-view-model.test.tsx

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { act, fireEvent, render, screen } from '@testing-library/react';
22
import { observer } from 'mobx-react-lite';
3-
import { ReactNode, useState } from 'react';
3+
import { PropsWithChildren, ReactNode, useState } from 'react';
44
import { describe, expect, test, vi } from 'vitest';
55

66
import { ViewModelStore, ViewModelStoreImpl, ViewModelsProvider } from '..';
77
import { createCounter } from '../utils';
8+
import { EmptyObject } from '../utils/types';
89
import { ViewModelMock } from '../view-model/view-model.impl.test';
910

1011
import { ViewModelProps, withViewModel } from './with-view-model';
@@ -222,6 +223,64 @@ describe('withViewModel', () => {
222223
expect(vm?.payload).toEqual({ counter: 3 });
223224
});
224225

226+
test('access to parent view model x3', async ({ task }) => {
227+
class VM1 extends ViewModelMock {
228+
vm1Value = 'foo';
229+
}
230+
const Component1 = withViewModel(VM1)(({
231+
children,
232+
model,
233+
}: PropsWithChildren & ViewModelProps<VM1>) => {
234+
return <div data-testid={`vm-${model.vm1Value}`}>{children}</div>;
235+
});
236+
237+
class VM2 extends ViewModelMock<EmptyObject, VM1> {
238+
vm2Value = 'bar';
239+
}
240+
const Component2 = withViewModel(VM2)(({
241+
children,
242+
model,
243+
}: PropsWithChildren & ViewModelProps<VM2>) => {
244+
return (
245+
<div
246+
data-testid={`vm-${model.vm2Value}-${model.parentViewModel.vm1Value}`}
247+
>
248+
{children}
249+
</div>
250+
);
251+
});
252+
253+
class VM3 extends ViewModelMock<EmptyObject, VM2> {
254+
vm3Value = 'baz';
255+
}
256+
const Component3 = withViewModel(VM3)(({
257+
children,
258+
model,
259+
}: PropsWithChildren & ViewModelProps<VM3>) => {
260+
return (
261+
<div
262+
data-testid={`vm-${model.vm3Value}-${model.parentViewModel.vm2Value}`}
263+
>
264+
{children}
265+
</div>
266+
);
267+
});
268+
269+
const { container } = await act(async () =>
270+
render(
271+
<Component1>
272+
<Component2>
273+
<Component3 />
274+
</Component2>
275+
</Component1>,
276+
),
277+
);
278+
279+
expect(container.firstChild).toMatchFileSnapshot(
280+
`../../tests/snapshots/hoc/with-view-model/${task.name}.html`,
281+
);
282+
});
283+
225284
describe('with ViewModelStore', () => {
226285
test('renders', async () => {
227286
class VM extends ViewModelMock {}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<div
2+
data-testid="vm-foo"
3+
>
4+
<div
5+
data-testid="vm-bar-foo"
6+
>
7+
<div
8+
data-testid="vm-baz-bar"
9+
/>
10+
</div>
11+
</div>

0 commit comments

Comments
 (0)