11import {
2+ children ,
23 createIf ,
34 insert ,
45 nextTick ,
56 ref ,
67 renderEffect ,
78 setText ,
89 template ,
10+ withDirectives ,
911} from '../src'
1012import type { Mock } from 'vitest'
1113import { makeRender } from './_utils'
14+ import { unmountComponent } from '../src/apiRender'
1215
1316const define = makeRender ( )
1417
@@ -24,6 +27,8 @@ describe('createIf', () => {
2427 let spyElseFn : Mock < any , any >
2528 const count = ref ( 0 )
2629
30+ const spyConditionFn = vi . fn ( ( ) => count . value )
31+
2732 // templates can be reused through caching.
2833 const t0 = template ( '<div></div>' )
2934 const t1 = template ( '<p></p>' )
@@ -34,7 +39,7 @@ describe('createIf', () => {
3439
3540 insert (
3641 createIf (
37- ( ) => count . value ,
42+ spyConditionFn ,
3843 // v-if
3944 ( spyIfFn ||= vi . fn ( ( ) => {
4045 const n2 = t1 ( )
@@ -55,24 +60,28 @@ describe('createIf', () => {
5560 } ) . render ( )
5661
5762 expect ( host . innerHTML ) . toBe ( '<div><p>zero</p><!--if--></div>' )
63+ expect ( spyConditionFn ) . toHaveBeenCalledTimes ( 1 )
5864 expect ( spyIfFn ! ) . toHaveBeenCalledTimes ( 0 )
5965 expect ( spyElseFn ! ) . toHaveBeenCalledTimes ( 1 )
6066
6167 count . value ++
6268 await nextTick ( )
6369 expect ( host . innerHTML ) . toBe ( '<div><p>1</p><!--if--></div>' )
70+ expect ( spyConditionFn ) . toHaveBeenCalledTimes ( 2 )
6471 expect ( spyIfFn ! ) . toHaveBeenCalledTimes ( 1 )
6572 expect ( spyElseFn ! ) . toHaveBeenCalledTimes ( 1 )
6673
6774 count . value ++
6875 await nextTick ( )
6976 expect ( host . innerHTML ) . toBe ( '<div><p>2</p><!--if--></div>' )
77+ expect ( spyConditionFn ) . toHaveBeenCalledTimes ( 3 )
7078 expect ( spyIfFn ! ) . toHaveBeenCalledTimes ( 1 )
7179 expect ( spyElseFn ! ) . toHaveBeenCalledTimes ( 1 )
7280
7381 count . value = 0
7482 await nextTick ( )
7583 expect ( host . innerHTML ) . toBe ( '<div><p>zero</p><!--if--></div>' )
84+ expect ( spyConditionFn ) . toHaveBeenCalledTimes ( 4 )
7685 expect ( spyIfFn ! ) . toHaveBeenCalledTimes ( 1 )
7786 expect ( spyElseFn ! ) . toHaveBeenCalledTimes ( 2 )
7887 } )
@@ -124,4 +133,113 @@ describe('createIf', () => {
124133 await nextTick ( )
125134 expect ( host . innerHTML ) . toBe ( '<!--if-->' )
126135 } )
136+
137+ test ( 'should work with directive hooks' , async ( ) => {
138+ const calls : string [ ] = [ ]
139+ const show1 = ref ( true )
140+ const show2 = ref ( true )
141+ const update = ref ( 0 )
142+
143+ const spyConditionFn1 = vi . fn ( ( ) => show1 . value )
144+ const spyConditionFn2 = vi . fn ( ( ) => show2 . value )
145+
146+ const vDirective : any = {
147+ created : ( el : any , { value } : any ) => calls . push ( `${ value } created` ) ,
148+ beforeMount : ( el : any , { value } : any ) =>
149+ calls . push ( `${ value } beforeMount` ) ,
150+ mounted : ( el : any , { value } : any ) => calls . push ( `${ value } mounted` ) ,
151+ beforeUpdate : ( el : any , { value } : any ) =>
152+ calls . push ( `${ value } beforeUpdate` ) ,
153+ updated : ( el : any , { value } : any ) => calls . push ( `${ value } updated` ) ,
154+ beforeUnmount : ( el : any , { value } : any ) =>
155+ calls . push ( `${ value } beforeUnmount` ) ,
156+ unmounted : ( el : any , { value } : any ) => calls . push ( `${ value } unmounted` ) ,
157+ }
158+
159+ const t0 = template ( '<p></p>' )
160+ const { instance } = define ( ( ) => {
161+ const n1 = createIf (
162+ spyConditionFn1 ,
163+ ( ) => {
164+ const n2 = t0 ( )
165+ withDirectives ( children ( n2 , 0 ) , [
166+ [ vDirective , ( ) => ( update . value , '1' ) ] ,
167+ ] )
168+ return n2
169+ } ,
170+ ( ) =>
171+ createIf (
172+ spyConditionFn2 ,
173+ ( ) => {
174+ const n2 = t0 ( )
175+ withDirectives ( children ( n2 , 0 ) , [ [ vDirective , ( ) => '2' ] ] )
176+ return n2
177+ } ,
178+ ( ) => {
179+ const n2 = t0 ( )
180+ withDirectives ( children ( n2 , 0 ) , [ [ vDirective , ( ) => '3' ] ] )
181+ return n2
182+ } ,
183+ ) ,
184+ )
185+ return [ n1 ]
186+ } ) . render ( )
187+
188+ await nextTick ( )
189+ expect ( calls ) . toEqual ( [ '1 created' , '1 beforeMount' , '1 mounted' ] )
190+ calls . length = 0
191+ expect ( spyConditionFn1 ) . toHaveBeenCalledTimes ( 1 )
192+ expect ( spyConditionFn2 ) . toHaveBeenCalledTimes ( 0 )
193+
194+ show1 . value = false
195+ await nextTick ( )
196+ expect ( calls ) . toEqual ( [
197+ '1 beforeUnmount' ,
198+ '2 created' ,
199+ '2 beforeMount' ,
200+ '1 unmounted' ,
201+ '2 mounted' ,
202+ ] )
203+ calls . length = 0
204+ expect ( spyConditionFn1 ) . toHaveBeenCalledTimes ( 2 )
205+ expect ( spyConditionFn2 ) . toHaveBeenCalledTimes ( 1 )
206+
207+ show2 . value = false
208+ await nextTick ( )
209+ expect ( calls ) . toEqual ( [
210+ '2 beforeUnmount' ,
211+ '3 created' ,
212+ '3 beforeMount' ,
213+ '2 unmounted' ,
214+ '3 mounted' ,
215+ ] )
216+ calls . length = 0
217+ expect ( spyConditionFn1 ) . toHaveBeenCalledTimes ( 2 )
218+ expect ( spyConditionFn2 ) . toHaveBeenCalledTimes ( 2 )
219+
220+ show1 . value = true
221+ await nextTick ( )
222+ expect ( calls ) . toEqual ( [
223+ '3 beforeUnmount' ,
224+ '1 created' ,
225+ '1 beforeMount' ,
226+ '3 unmounted' ,
227+ '1 mounted' ,
228+ ] )
229+ calls . length = 0
230+ expect ( spyConditionFn1 ) . toHaveBeenCalledTimes ( 3 )
231+ expect ( spyConditionFn2 ) . toHaveBeenCalledTimes ( 2 )
232+
233+ update . value ++
234+ await nextTick ( )
235+ expect ( calls ) . toEqual ( [ '1 beforeUpdate' , '1 updated' ] )
236+ calls . length = 0
237+ expect ( spyConditionFn1 ) . toHaveBeenCalledTimes ( 3 )
238+ expect ( spyConditionFn2 ) . toHaveBeenCalledTimes ( 2 )
239+
240+ unmountComponent ( instance )
241+ expect ( calls ) . toEqual ( [ '1 beforeUnmount' , '1 unmounted' ] )
242+ expect ( spyConditionFn1 ) . toHaveBeenCalledTimes ( 3 )
243+ expect ( spyConditionFn2 ) . toHaveBeenCalledTimes ( 2 )
244+ } )
127245} )
0 commit comments