@@ -151,6 +151,73 @@ describe('hot module replacement', () => {
151
151
expect ( mountSpy ) . toHaveBeenCalledTimes ( 1 )
152
152
} )
153
153
154
+ // #7042
155
+ test ( 'reload KeepAlive slot' , async ( ) => {
156
+ const root = nodeOps . createElement ( 'div' )
157
+ const childId = 'test-child-keep-alive'
158
+ const unmountSpy = jest . fn ( )
159
+ const mountSpy = jest . fn ( )
160
+ const activeSpy = jest . fn ( )
161
+ const deactiveSpy = jest . fn ( )
162
+
163
+ const Child : ComponentOptions = {
164
+ __hmrId : childId ,
165
+ data ( ) {
166
+ return { count : 0 }
167
+ } ,
168
+ unmounted : unmountSpy ,
169
+ render : compileToFunction ( `<div>{{ count }}</div>` )
170
+ }
171
+ createRecord ( childId , Child )
172
+
173
+ const Parent : ComponentOptions = {
174
+ components : { Child } ,
175
+ data ( ) {
176
+ return { toggle : true }
177
+ } ,
178
+ render : compileToFunction (
179
+ `<button @click="toggle = !toggle"></button><KeepAlive><Child v-if="toggle" /></KeepAlive>`
180
+ )
181
+ }
182
+
183
+ render ( h ( Parent ) , root )
184
+ expect ( serializeInner ( root ) ) . toBe ( `<button></button><div>0</div>` )
185
+
186
+ reload ( childId , {
187
+ __hmrId : childId ,
188
+ data ( ) {
189
+ return { count : 1 }
190
+ } ,
191
+ mounted : mountSpy ,
192
+ unmounted : unmountSpy ,
193
+ activated : activeSpy ,
194
+ deactivated : deactiveSpy ,
195
+ render : compileToFunction ( `<div>{{ count }}</div>` )
196
+ } )
197
+ await nextTick ( )
198
+ expect ( serializeInner ( root ) ) . toBe ( `<button></button><div>1</div>` )
199
+ expect ( unmountSpy ) . toHaveBeenCalledTimes ( 1 )
200
+ expect ( mountSpy ) . toHaveBeenCalledTimes ( 1 )
201
+ expect ( activeSpy ) . toHaveBeenCalledTimes ( 1 )
202
+ expect ( deactiveSpy ) . toHaveBeenCalledTimes ( 0 )
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 ( )
215
+ expect ( unmountSpy ) . toHaveBeenCalledTimes ( 1 )
216
+ expect ( mountSpy ) . toHaveBeenCalledTimes ( 1 )
217
+ expect ( activeSpy ) . toHaveBeenCalledTimes ( 2 )
218
+ expect ( deactiveSpy ) . toHaveBeenCalledTimes ( 1 )
219
+ } )
220
+
154
221
test ( 'reload class component' , async ( ) => {
155
222
const root = nodeOps . createElement ( 'div' )
156
223
const childId = 'test4-child'
0 commit comments