|
1 | 1 | import { act, fireEvent, render, screen } from '@testing-library/react'; |
2 | 2 | import { observer } from 'mobx-react-lite'; |
3 | | -import { ReactNode, useState } from 'react'; |
| 3 | +import { PropsWithChildren, ReactNode, useState } from 'react'; |
4 | 4 | import { describe, expect, test, vi } from 'vitest'; |
5 | 5 |
|
6 | 6 | import { ViewModelStore, ViewModelStoreImpl, ViewModelsProvider } from '..'; |
7 | 7 | import { createCounter } from '../utils'; |
| 8 | +import { EmptyObject } from '../utils/types'; |
8 | 9 | import { ViewModelMock } from '../view-model/view-model.impl.test'; |
9 | 10 |
|
10 | 11 | import { ViewModelProps, withViewModel } from './with-view-model'; |
@@ -222,6 +223,64 @@ describe('withViewModel', () => { |
222 | 223 | expect(vm?.payload).toEqual({ counter: 3 }); |
223 | 224 | }); |
224 | 225 |
|
| 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 | + |
225 | 284 | describe('with ViewModelStore', () => { |
226 | 285 | test('renders', async () => { |
227 | 286 | class VM extends ViewModelMock {} |
|
0 commit comments