Skip to content

Commit 6e9bc06

Browse files
committed
test: add test for access to parentViewModel (with view model store)
1 parent 1df2375 commit 6e9bc06

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

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

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,5 +344,74 @@ describe('withViewModel', () => {
344344

345345
expect(viewModels).toBeDefined();
346346
});
347+
348+
test('access to parent view model x3', async ({ task }) => {
349+
const vmStore = new ViewModelStoreImpl();
350+
351+
const Wrapper = ({ children }: { children?: ReactNode }) => {
352+
return (
353+
<ViewModelsProvider value={vmStore}>{children}</ViewModelsProvider>
354+
);
355+
};
356+
357+
class VM1 extends ViewModelMock {
358+
vm1Value = 'foo';
359+
}
360+
const Component1 = withViewModel(VM1)(({
361+
children,
362+
model,
363+
}: PropsWithChildren & ViewModelProps<VM1>) => {
364+
return <div data-testid={`vm-${model.vm1Value}`}>{children}</div>;
365+
});
366+
367+
class VM2 extends ViewModelMock<EmptyObject, VM1> {
368+
vm2Value = 'bar';
369+
}
370+
const Component2 = withViewModel(VM2)(({
371+
children,
372+
model,
373+
}: PropsWithChildren & ViewModelProps<VM2>) => {
374+
return (
375+
<div
376+
data-testid={`vm-${model.vm2Value}-${model.parentViewModel.vm1Value}`}
377+
>
378+
{children}
379+
</div>
380+
);
381+
});
382+
383+
class VM3 extends ViewModelMock<EmptyObject, VM2> {
384+
vm3Value = 'baz';
385+
}
386+
const Component3 = withViewModel(VM3)(({
387+
children,
388+
model,
389+
}: PropsWithChildren & ViewModelProps<VM3>) => {
390+
return (
391+
<div
392+
data-testid={`vm-${model.vm3Value}-${model.parentViewModel.vm2Value}`}
393+
>
394+
{children}
395+
</div>
396+
);
397+
});
398+
399+
const { container } = await act(async () =>
400+
render(
401+
<Component1>
402+
<Component2>
403+
<Component3 />
404+
</Component2>
405+
</Component1>,
406+
{
407+
wrapper: Wrapper,
408+
},
409+
),
410+
);
411+
412+
expect(container.firstChild).toMatchFileSnapshot(
413+
`../../tests/snapshots/hoc/with-view-model/view-model-store/${task.name}.html`,
414+
);
415+
});
347416
});
348417
});
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)