@@ -156,6 +156,8 @@ describe('hot module replacement', () => {
156
156
const childId = 'test-child-keep-alive'
157
157
const unmountSpy = jest . fn ( )
158
158
const mountSpy = jest . fn ( )
159
+ const activeSpy = jest . fn ( )
160
+ const deactiveSpy = jest . fn ( )
159
161
160
162
const Child : ComponentOptions = {
161
163
__hmrId : childId ,
@@ -169,24 +171,51 @@ describe('hot module replacement', () => {
169
171
170
172
const Parent : ComponentOptions = {
171
173
components : { Child } ,
172
- render : compileToFunction ( `<KeepAlive><Child/></KeepAlive>` )
174
+ data ( ) {
175
+ return { toggle : true }
176
+ } ,
177
+ render : compileToFunction (
178
+ `<button @click="toggle = !toggle"></button><KeepAlive><Child v-if="toggle" /></KeepAlive>`
179
+ )
173
180
}
174
181
175
182
render ( h ( Parent ) , root )
176
- expect ( serializeInner ( root ) ) . toBe ( `<div>0</div>` )
183
+ expect ( serializeInner ( root ) ) . toBe ( `<button></button>< div>0</div>` )
177
184
178
185
reload ( childId , {
179
186
__hmrId : childId ,
180
187
data ( ) {
181
188
return { count : 1 }
182
189
} ,
183
190
mounted : mountSpy ,
191
+ unmounted : unmountSpy ,
192
+ activated : activeSpy ,
193
+ deactivated : deactiveSpy ,
184
194
render : compileToFunction ( `<div>{{ count }}</div>` )
185
195
} )
186
196
await nextTick ( )
187
- expect ( serializeInner ( root ) ) . toBe ( `<div>1</div>` )
197
+ expect ( serializeInner ( root ) ) . toBe ( `<button></button><div>1</div>` )
198
+ expect ( unmountSpy ) . toHaveBeenCalledTimes ( 1 )
199
+ expect ( mountSpy ) . toHaveBeenCalledTimes ( 1 )
200
+ expect ( activeSpy ) . toHaveBeenCalledTimes ( 1 )
201
+ expect ( deactiveSpy ) . toHaveBeenCalledTimes ( 0 )
202
+
203
+
204
+ // should not unmount when toggling
205
+ triggerEvent ( root . children [ 1 ] as TestElement , 'click' )
206
+ await nextTick ( )
207
+ expect ( unmountSpy ) . toHaveBeenCalledTimes ( 1 )
208
+ expect ( mountSpy ) . toHaveBeenCalledTimes ( 1 )
209
+ expect ( activeSpy ) . toHaveBeenCalledTimes ( 1 )
210
+ expect ( deactiveSpy ) . toHaveBeenCalledTimes ( 1 )
211
+
212
+ // should not mount when toggling
213
+ triggerEvent ( root . children [ 1 ] as TestElement , 'click' )
214
+ await nextTick ( )
188
215
expect ( unmountSpy ) . toHaveBeenCalledTimes ( 1 )
189
216
expect ( mountSpy ) . toHaveBeenCalledTimes ( 1 )
217
+ expect ( activeSpy ) . toHaveBeenCalledTimes ( 2 )
218
+ expect ( deactiveSpy ) . toHaveBeenCalledTimes ( 1 )
190
219
} )
191
220
192
221
test ( 'reload class component' , async ( ) => {
0 commit comments