1
1
import {
2
+ children ,
2
3
createIf ,
3
4
insert ,
4
5
nextTick ,
5
6
ref ,
6
7
renderEffect ,
7
8
setText ,
8
9
template ,
10
+ withDirectives ,
9
11
} from '../src'
10
12
import type { Mock } from 'vitest'
11
13
import { makeRender } from './_utils'
14
+ import { unmountComponent } from '../src/apiRender'
12
15
13
16
const define = makeRender ( )
14
17
@@ -24,6 +27,8 @@ describe('createIf', () => {
24
27
let spyElseFn : Mock < any , any >
25
28
const count = ref ( 0 )
26
29
30
+ const spyConditionFn = vi . fn ( ( ) => count . value )
31
+
27
32
// templates can be reused through caching.
28
33
const t0 = template ( '<div></div>' )
29
34
const t1 = template ( '<p></p>' )
@@ -34,7 +39,7 @@ describe('createIf', () => {
34
39
35
40
insert (
36
41
createIf (
37
- ( ) => count . value ,
42
+ spyConditionFn ,
38
43
// v-if
39
44
( spyIfFn ||= vi . fn ( ( ) => {
40
45
const n2 = t1 ( )
@@ -55,24 +60,28 @@ describe('createIf', () => {
55
60
} ) . render ( )
56
61
57
62
expect ( host . innerHTML ) . toBe ( '<div><p>zero</p><!--if--></div>' )
63
+ expect ( spyConditionFn ) . toHaveBeenCalledTimes ( 1 )
58
64
expect ( spyIfFn ! ) . toHaveBeenCalledTimes ( 0 )
59
65
expect ( spyElseFn ! ) . toHaveBeenCalledTimes ( 1 )
60
66
61
67
count . value ++
62
68
await nextTick ( )
63
69
expect ( host . innerHTML ) . toBe ( '<div><p>1</p><!--if--></div>' )
70
+ expect ( spyConditionFn ) . toHaveBeenCalledTimes ( 2 )
64
71
expect ( spyIfFn ! ) . toHaveBeenCalledTimes ( 1 )
65
72
expect ( spyElseFn ! ) . toHaveBeenCalledTimes ( 1 )
66
73
67
74
count . value ++
68
75
await nextTick ( )
69
76
expect ( host . innerHTML ) . toBe ( '<div><p>2</p><!--if--></div>' )
77
+ expect ( spyConditionFn ) . toHaveBeenCalledTimes ( 3 )
70
78
expect ( spyIfFn ! ) . toHaveBeenCalledTimes ( 1 )
71
79
expect ( spyElseFn ! ) . toHaveBeenCalledTimes ( 1 )
72
80
73
81
count . value = 0
74
82
await nextTick ( )
75
83
expect ( host . innerHTML ) . toBe ( '<div><p>zero</p><!--if--></div>' )
84
+ expect ( spyConditionFn ) . toHaveBeenCalledTimes ( 4 )
76
85
expect ( spyIfFn ! ) . toHaveBeenCalledTimes ( 1 )
77
86
expect ( spyElseFn ! ) . toHaveBeenCalledTimes ( 2 )
78
87
} )
@@ -124,4 +133,113 @@ describe('createIf', () => {
124
133
await nextTick ( )
125
134
expect ( host . innerHTML ) . toBe ( '<!--if-->' )
126
135
} )
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
+ } )
127
245
} )
0 commit comments