fix(keep-alive): do not invoke onVnodeBeforeUnmount if is KeepAlive component (#1079)

This commit is contained in:
Carlos Rodrigues
2020-04-30 19:52:03 +01:00
committed by GitHub
parent 2e0373bb7a
commit 239270c38a
2 changed files with 102 additions and 4 deletions

View File

@@ -7,7 +7,14 @@ import {
KeepAlive,
serializeInner,
nextTick,
ComponentOptions
ComponentOptions,
markRaw,
inject,
defineComponent,
ComponentPublicInstance,
Ref,
cloneVNode,
provide
} from '@vue/runtime-test'
import { KeepAliveProps } from '../../src/components/KeepAlive'
@@ -559,4 +566,91 @@ describe('KeepAlive', () => {
expect(serializeInner(root)).toBe(`1`)
})
})
it('should not call onVnodeUnmounted', async () => {
const Foo = markRaw({
name: 'Foo',
render() {
return h('Foo')
}
})
const Bar = markRaw({
name: 'Bar',
render() {
return h('Bar')
}
})
const spyMounted = jest.fn()
const spyUnmounted = jest.fn()
const RouterView = defineComponent({
setup(_, { slots }) {
const Component = inject<Ref<ComponentPublicInstance>>('component')
const refView = ref()
let componentProps = {
ref: refView,
onVnodeMounted() {
spyMounted()
},
onVnodeUnmounted() {
spyUnmounted()
}
}
return () => {
const child: any = slots.default!({
Component: Component!.value
})[0]
const innerChild = child.children[0]
child.children[0] = cloneVNode(innerChild, componentProps)
return child
}
}
})
let toggle: () => void = () => {}
const App = defineComponent({
setup() {
const component = ref(Foo)
provide('component', component)
toggle = () => {
component.value = component.value === Foo ? Bar : Foo
}
return {
component,
toggle
}
},
render() {
return h(RouterView, null, {
default: ({ Component }: any) => h(KeepAlive, null, [h(Component)])
})
}
})
render(h(App), root)
await nextTick()
expect(spyMounted).toHaveBeenCalledTimes(1)
expect(spyUnmounted).toHaveBeenCalledTimes(0)
toggle()
await nextTick()
expect(spyMounted).toHaveBeenCalledTimes(2)
expect(spyUnmounted).toHaveBeenCalledTimes(0)
toggle()
await nextTick()
render(null, root)
await nextTick()
expect(spyMounted).toHaveBeenCalledTimes(2)
expect(spyUnmounted).toHaveBeenCalledTimes(2)
})
})