1- import { act , render , screen , } from '@testing-library/react' ;
1+ import { act , render , screen } from '@testing-library/react' ;
22import { observer } from 'mobx-react-lite' ;
33import { ReactNode } from 'react' ;
44import { describe , expect , test } from 'vitest' ;
55
66import { ViewModelsProvider } from '..' ;
7+ import { createCounter } from '../utils' ;
78import { TestViewModelStoreImpl } from '../view-model/abstract-view-model.store.test' ;
89import { TestViewModelImpl } from '../view-model/view-model.impl.test' ;
910
1011import { ViewModelProps , withViewModel } from './with-view-model' ;
11- import { createCounter } from '../utils' ;
1212
1313const createIdGenerator = ( prefix ?: string ) => {
1414 const counter = createCounter ( ) ;
15- return ( ) => ( prefix || '' ) + counter ( ) . toString ( )
16- }
15+ return ( ) => ( prefix ?? '' ) + counter ( ) . toString ( ) ;
16+ } ;
1717
1818describe ( 'withViewModel' , ( ) => {
1919 test ( 'renders' , ( ) => {
20- class VM extends TestViewModelImpl { }
20+ class VM extends TestViewModelImpl { }
2121 const View = ( { model } : ViewModelProps < VM > ) => {
2222 return < div > { `hello ${ model . id } ` } </ div > ;
2323 } ;
24- const Component = withViewModel ( VM , { generateId : createIdGenerator ( ) } ) ( View ) ;
24+ const Component = withViewModel ( VM , { generateId : createIdGenerator ( ) } ) (
25+ View ,
26+ ) ;
2527
2628 render ( < Component /> ) ;
2729 expect ( screen . getByText ( 'hello VM_0' ) ) . toBeDefined ( ) ;
2830 } ) ;
2931
3032 test ( 'renders nesting' , ( ) => {
31- class VM1 extends TestViewModelImpl { }
32- const View1 = ( {
33+ const Component1 = withViewModel ( TestViewModelImpl ) ( ( {
3334 children,
34- } : ViewModelProps < VM1 > & { children ?: ReactNode } ) => {
35+ } : {
36+ children ?: ReactNode ;
37+ } ) => {
3538 return (
36- < div data-testid = " parent-container" >
39+ < div data-testid = { ' parent-container' } >
3740 < div > parent</ div >
38- < div > { children } </ div >
41+ { children }
3942 </ div >
4043 ) ;
41- } ;
42- const Component1 = withViewModel ( VM1 , { generateId : createIdGenerator ( ) } ) ( View1 ) ;
43-
44- const Component2 = withViewModel ( class VM2 extends TestViewModelImpl { } , { generateId : createIdGenerator ( ) } ) (
45- ( ) => {
46- return < div > child</ div > ;
47- } ,
48-
49- ) ;
44+ } ) ;
45+ const Component2 = withViewModel ( TestViewModelImpl ) ( ( ) => {
46+ return < div > child</ div > ;
47+ } ) ;
5048
51- render (
49+ const { container } = render (
5250 < Component1 >
5351 < Component2 />
5452 </ Component1 > ,
5553 ) ;
56- expect ( screen . getByTestId ( 'parent-container' ) ) . toBe ( `<div
57- data-testid="parent-container"
58- >
59- <div>
60- parent
61- </div>
62- <div>
63- <div>
64- child
65- </div>
66- </div>
67- </div>` ) ;
54+
55+ expect ( container . firstChild ) . toMatchInlineSnapshot ( `
56+ <div
57+ data-testid="parent-container"
58+ >
59+ <div>
60+ parent
61+ </div>
62+ <div>
63+ child
64+ </div>
65+ </div>
66+ ` ) ;
6867 } ) ;
6968
7069 test ( 'renders twice' , async ( ) => {
71- class VM extends TestViewModelImpl { }
70+ class VM extends TestViewModelImpl { }
7271 const View = ( { model } : ViewModelProps < VM > ) => {
7372 return < div > { `hello ${ model . id } ` } </ div > ;
7473 } ;
75- const Component = withViewModel ( VM , { generateId : createIdGenerator ( ) } ) ( View ) ;
74+ const Component = withViewModel ( VM , { generateId : createIdGenerator ( ) } ) (
75+ View ,
76+ ) ;
7677
7778 render (
7879 < >
@@ -85,7 +86,7 @@ describe('withViewModel', () => {
8586 } ) ;
8687
8788 test ( 'renders with fixed id' , ( ) => {
88- class VM extends TestViewModelImpl { }
89+ class VM extends TestViewModelImpl { }
8990 const View = ( { model } : ViewModelProps < VM > ) => {
9091 return < div > { `hello ${ model . id } ` } </ div > ;
9192 } ;
@@ -96,7 +97,7 @@ describe('withViewModel', () => {
9697 } ) ;
9798
9899 test ( 'renders twice with fixed id' , async ( ) => {
99- class VM extends TestViewModelImpl { }
100+ class VM extends TestViewModelImpl { }
100101 const View = ( { model } : ViewModelProps < VM > ) => {
101102 return < div > { `hello ${ model . id } ` } </ div > ;
102103 } ;
@@ -112,15 +113,15 @@ describe('withViewModel', () => {
112113 } ) ;
113114
114115 test ( 'renders with view model store' , async ( ) => {
115- class VM extends TestViewModelImpl { }
116+ class VM extends TestViewModelImpl { }
116117 const View = observer ( ( { model } : ViewModelProps < VM > ) => {
117118 return (
118119 < div >
119- < div > { `hello my friend. Model has id ? ${ ! ! model . id } ` } </ div >
120+ < div > { `hello my friend. Model id is ${ model . id } ` } </ div >
120121 </ div >
121122 ) ;
122123 } ) ;
123- const Component = withViewModel ( VM ) ( View ) ;
124+ const Component = withViewModel ( VM , { generateId : ( ) => '1' } ) ( View ) ;
124125 const vmStore = new TestViewModelStoreImpl ( ) ;
125126
126127 const Wrapper = ( { children } : { children ?: ReactNode } ) => {
@@ -135,8 +136,6 @@ describe('withViewModel', () => {
135136 } ) ,
136137 ) ;
137138
138- expect (
139- screen . getByText ( 'hello my friend. Model has id ? true' ) ,
140- ) . toBeDefined ( ) ;
139+ expect ( screen . getByText ( 'hello my friend. Model id is VM_1' ) ) . toBeDefined ( ) ;
141140 } ) ;
142141} ) ;
0 commit comments