Skip to content

Commit 3995a71

Browse files
committed
test: reload keep-alive slot
1 parent d34c1ac commit 3995a71

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

packages/compiler-vapor/src/generators/component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import { genEventHandler } from './event'
4040
import { genDirectiveModifiers, genDirectivesForElement } from './directive'
4141
import { genBlock } from './block'
4242
import { genModelHandler } from './vModel'
43-
import { isBuiltInComponent } from '../utils'
43+
import { isBuiltInComponent, isKeepAliveTag } from '../utils'
4444

4545
export function genCreateComponent(
4646
operation: CreateComponentIRNode,
@@ -460,7 +460,7 @@ function genSlotBlockWithProps(oper: SlotBlockIRNode, context: CodegenContext) {
460460
]
461461
}
462462

463-
if (node.type === NodeTypes.ELEMENT) {
463+
if (node.type === NodeTypes.ELEMENT && !isKeepAliveTag(node.tag)) {
464464
// wrap with withVaporCtx to ensure correct currentInstance inside slot
465465
blockFn = [`${context.helper('withVaporCtx')}(`, ...blockFn, `)`]
466466
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,9 @@ describe('hot module replacement', () => {
163163
expect(mountSpy).toHaveBeenCalledTimes(1)
164164
})
165165

166-
test.todo('reload KeepAlive slot', async () => {
166+
test('reload KeepAlive slot', async () => {
167167
const root = document.createElement('div')
168+
document.body.appendChild(root)
168169
const childId = 'test-child-keep-alive'
169170
const unmountSpy = vi.fn()
170171
const mountSpy = vi.fn()
@@ -201,6 +202,7 @@ describe('hot module replacement', () => {
201202

202203
reload(childId, {
203204
__hmrId: childId,
205+
__vapor: true,
204206
setup() {
205207
onMounted(mountSpy)
206208
onUnmounted(unmountSpy)
@@ -219,15 +221,15 @@ describe('hot module replacement', () => {
219221
expect(deactivatedSpy).toHaveBeenCalledTimes(0)
220222

221223
// should not unmount when toggling
222-
triggerEvent('click', root.children[1] as Element)
224+
triggerEvent('click', root.children[0] as Element)
223225
await nextTick()
224226
expect(unmountSpy).toHaveBeenCalledTimes(1)
225227
expect(mountSpy).toHaveBeenCalledTimes(1)
226228
expect(activeSpy).toHaveBeenCalledTimes(1)
227229
expect(deactivatedSpy).toHaveBeenCalledTimes(1)
228230

229231
// should not mount when toggling
230-
triggerEvent('click', root.children[1] as Element)
232+
triggerEvent('click', root.children[0] as Element)
231233
await nextTick()
232234
expect(unmountSpy).toHaveBeenCalledTimes(1)
233235
expect(mountSpy).toHaveBeenCalledTimes(1)

packages/runtime-vapor/src/hmr.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
isKeepAlive,
23
popWarningContext,
34
pushWarningContext,
45
setCurrentInstance,
@@ -35,6 +36,11 @@ export function hmrReload(
3536
instance: VaporComponentInstance,
3637
newComp: VaporComponent,
3738
): void {
39+
// if parent is KeepAlive, we need to rerender it
40+
if (instance.parent && isKeepAlive(instance.parent)) {
41+
instance.parent.hmrRerender!()
42+
return
43+
}
3844
const normalized = normalizeBlock(instance.block)
3945
const parent = normalized[0].parentNode!
4046
const anchor = normalized[normalized.length - 1].nextSibling

0 commit comments

Comments
 (0)