Skip to content

Commit afb21f7

Browse files
committed
test: fix keepalive transition out-in test case
1 parent 1ea7756 commit afb21f7

File tree

1 file changed

+87
-2
lines changed

1 file changed

+87
-2
lines changed

packages/runtime-core/__tests__/hmr.spec.ts

Lines changed: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ describe('hot module replacement', () => {
176176
return { toggle: true }
177177
},
178178
render: compileToFunction(
179-
`<button @click="toggle = !toggle"></button><KeepAlive><Child v-if="toggle" /></KeepAlive>`
179+
`<button @click="toggle = !toggle" />
180+
<KeepAlive><Child v-if="toggle" /></KeepAlive>`
180181
)
181182
}
182183

@@ -243,7 +244,10 @@ describe('hot module replacement', () => {
243244
return { toggle: true }
244245
},
245246
render: compileToFunction(
246-
`<button @click="toggle = !toggle"></button><BaseTransition mode="out-in"><KeepAlive><Child v-if="toggle" /></KeepAlive></BaseTransition>`
247+
`<button @click="toggle = !toggle" />
248+
<BaseTransition>
249+
<KeepAlive><Child v-if="toggle" /></KeepAlive>
250+
</BaseTransition>`
247251
)
248252
}
249253

@@ -271,6 +275,87 @@ describe('hot module replacement', () => {
271275
// should not unmount when toggling
272276
triggerEvent(root.children[1] as TestElement, 'click')
273277
await nextTick()
278+
expect(serializeInner(root)).toBe(`<button></button><!--v-if-->`)
279+
expect(unmountSpy).toHaveBeenCalledTimes(1)
280+
expect(mountSpy).toHaveBeenCalledTimes(1)
281+
expect(activeSpy).toHaveBeenCalledTimes(1)
282+
expect(deactiveSpy).toHaveBeenCalledTimes(1)
283+
284+
// should not mount when toggling
285+
triggerEvent(root.children[1] as TestElement, 'click')
286+
await nextTick()
287+
expect(serializeInner(root)).toBe(`<button></button><div>1</div>`)
288+
expect(unmountSpy).toHaveBeenCalledTimes(1)
289+
expect(mountSpy).toHaveBeenCalledTimes(1)
290+
expect(activeSpy).toHaveBeenCalledTimes(2)
291+
expect(deactiveSpy).toHaveBeenCalledTimes(1)
292+
})
293+
294+
test('reload KeepAlive slot in Transition with out-in', async () => {
295+
const root = nodeOps.createElement('div')
296+
const childId = 'test-transition-keep-alive-reload-with-out-in'
297+
const unmountSpy = vi.fn()
298+
const mountSpy = vi.fn()
299+
const activeSpy = vi.fn()
300+
const deactiveSpy = vi.fn()
301+
302+
const Child: ComponentOptions = {
303+
__hmrId: childId,
304+
name: 'original',
305+
data() {
306+
return { count: 0 }
307+
},
308+
unmounted: unmountSpy,
309+
render: compileToFunction(`<div>{{ count }}</div>`)
310+
}
311+
createRecord(childId, Child)
312+
313+
const Parent: ComponentOptions = {
314+
components: { Child },
315+
data() {
316+
return { toggle: true }
317+
},
318+
methods: {
319+
// @ts-ignore
320+
onLeave(_, done) {
321+
setTimeout(done, 0)
322+
}
323+
},
324+
render: compileToFunction(
325+
`<button @click="toggle = !toggle" />
326+
<BaseTransition mode="out-in" @leave="onLeave">
327+
<KeepAlive><Child v-if="toggle" /></KeepAlive>
328+
</BaseTransition>`
329+
)
330+
}
331+
332+
render(h(Parent), root)
333+
expect(serializeInner(root)).toBe(`<button></button><div>0</div>`)
334+
335+
reload(childId, {
336+
__hmrId: childId,
337+
name: 'updated',
338+
data() {
339+
return { count: 1 }
340+
},
341+
mounted: mountSpy,
342+
unmounted: unmountSpy,
343+
activated: activeSpy,
344+
deactivated: deactiveSpy,
345+
render: compileToFunction(`<div>{{ count }}</div>`)
346+
})
347+
await nextTick()
348+
await new Promise(r => setTimeout(r, 0))
349+
expect(serializeInner(root)).toBe(`<button></button><div>1</div>`)
350+
expect(unmountSpy).toHaveBeenCalledTimes(1)
351+
expect(mountSpy).toHaveBeenCalledTimes(1)
352+
expect(activeSpy).toHaveBeenCalledTimes(1)
353+
expect(deactiveSpy).toHaveBeenCalledTimes(0)
354+
355+
// should not unmount when toggling
356+
triggerEvent(root.children[1] as TestElement, 'click')
357+
await nextTick()
358+
await new Promise(r => setTimeout(r, 0))
274359
expect(serializeInner(root)).toBe(`<button></button><!---->`)
275360
expect(unmountSpy).toHaveBeenCalledTimes(1)
276361
expect(mountSpy).toHaveBeenCalledTimes(1)

0 commit comments

Comments
 (0)