@@ -3,10 +3,11 @@ import { observer } from 'mobx-react-lite';
33import { PropsWithChildren , ReactNode , useState } from 'react' ;
44import { describe , expect , test , vi } from 'vitest' ;
55
6- import { ViewModelStore , ViewModelStoreImpl , ViewModelsProvider } from '..' ;
6+ import { ViewModelStore , ViewModelsProvider } from '..' ;
77import { createCounter } from '../utils' ;
88import { EmptyObject } from '../utils/types' ;
99import { ViewModelMock } from '../view-model/view-model.impl.test' ;
10+ import { ViewModelStoreMock } from '../view-model/view-model.store.impl.test' ;
1011
1112import { ViewModelProps , withViewModel } from './with-view-model' ;
1213
@@ -223,14 +224,16 @@ describe('withViewModel', () => {
223224 expect ( vm ?. payload ) . toEqual ( { counter : 3 } ) ;
224225 } ) ;
225226
226- test ( 'access to parent view model x3' , async ( { task } ) => {
227+ test ( 'access to parent view model x3' , async ( { task, expect } ) => {
227228 class VM1 extends ViewModelMock {
228229 vm1Value = 'foo' ;
229230 }
230231 const Component1 = withViewModel ( VM1 ) ( ( {
231232 children,
232233 model,
233234 } : PropsWithChildren & ViewModelProps < VM1 > ) => {
235+ expect ( model . isMounted ) . toBeTruthy ( ) ;
236+ expect ( model . spies . mount ) . toBeCalledTimes ( 1 ) ;
234237 return < div data-testid = { `vm-${ model . vm1Value } ` } > { children } </ div > ;
235238 } ) ;
236239
@@ -241,6 +244,8 @@ describe('withViewModel', () => {
241244 children,
242245 model,
243246 } : PropsWithChildren & ViewModelProps < VM2 > ) => {
247+ expect ( model . isMounted ) . toBeTruthy ( ) ;
248+ expect ( model . spies . mount ) . toBeCalledTimes ( 1 ) ;
244249 return (
245250 < div
246251 data-testid = { `vm-${ model . vm2Value } -${ model . parentViewModel . vm1Value } ` }
@@ -257,6 +262,8 @@ describe('withViewModel', () => {
257262 children,
258263 model,
259264 } : PropsWithChildren & ViewModelProps < VM3 > ) => {
265+ expect ( model . isMounted ) . toBeTruthy ( ) ;
266+ expect ( model . spies . mount ) . toBeCalledTimes ( 1 ) ;
260267 return (
261268 < div
262269 data-testid = { `vm-${ model . vm3Value } -${ model . parentViewModel . vm2Value } ` }
@@ -292,7 +299,7 @@ describe('withViewModel', () => {
292299 ) ;
293300 } ) ;
294301 const Component = withViewModel ( VM , { generateId : ( ) => '1' } ) ( View ) ;
295- const vmStore = new ViewModelStoreImpl ( ) ;
302+ const vmStore = new ViewModelStoreMock ( ) ;
296303
297304 const Wrapper = ( { children } : { children ?: ReactNode } ) => {
298305 return (
@@ -328,7 +335,7 @@ describe('withViewModel', () => {
328335 ) ;
329336 } ) ;
330337 const Component = withViewModel ( VM , { generateId : ( ) => '1' } ) ( View ) ;
331- const vmStore = new ViewModelStoreImpl ( ) ;
338+ const vmStore = new ViewModelStoreMock ( ) ;
332339
333340 const Wrapper = ( { children } : { children ?: ReactNode } ) => {
334341 return (
@@ -343,10 +350,15 @@ describe('withViewModel', () => {
343350 ) ;
344351
345352 expect ( viewModels ) . toBeDefined ( ) ;
353+ expect ( vmStore . spies . get ) . toHaveBeenCalledTimes ( 0 ) ;
354+ expect ( vmStore . _instanceAttachedCount . size ) . toBe ( 1 ) ;
355+ expect ( vmStore . _unmountingViews . size ) . toBe ( 0 ) ;
356+ expect ( vmStore . mountedViewsCount ) . toBe ( 1 ) ;
357+ expect ( vmStore . _mountingViews . size ) . toBe ( 0 ) ;
346358 } ) ;
347359
348360 test ( 'access to parent view model x3' , async ( { task } ) => {
349- const vmStore = new ViewModelStoreImpl ( ) ;
361+ const vmStore = new ViewModelStoreMock ( ) ;
350362
351363 const Wrapper = ( { children } : { children ?: ReactNode } ) => {
352364 return (
@@ -412,6 +424,11 @@ describe('withViewModel', () => {
412424 expect ( container . firstChild ) . toMatchFileSnapshot (
413425 `../../tests/snapshots/hoc/with-view-model/view-model-store/${ task . name } .html` ,
414426 ) ;
427+ expect ( vmStore . spies . get ) . toHaveBeenCalledTimes ( 0 ) ;
428+ expect ( vmStore . _instanceAttachedCount . size ) . toBe ( 3 ) ;
429+ expect ( vmStore . _unmountingViews . size ) . toBe ( 0 ) ;
430+ expect ( vmStore . mountedViewsCount ) . toBe ( 3 ) ;
431+ expect ( vmStore . _mountingViews . size ) . toBe ( 0 ) ;
415432 } ) ;
416433 } ) ;
417434} ) ;
0 commit comments