Skip to content

Commit f69bc37

Browse files
authored
fix: allow to use KeepAlive or keep-alive in stubs (#1892)
* fix: allow to use KeepAlive or keep-alive in stubs * refactor: use isTeleport/isKeepAlive utils in stubs
1 parent 5b49e59 commit f69bc37

File tree

3 files changed

+55
-8
lines changed

3 files changed

+55
-8
lines changed

src/vnodeTransformers/stubComponentsTransformer.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { VTUVNodeTypeTransformer } from './util'
1+
import { isKeepAlive, isTeleport, VTUVNodeTypeTransformer } from './util'
22
import {
33
Transition,
44
TransitionGroup,
@@ -115,7 +115,7 @@ export function createStubComponentsTransformer({
115115
}: CreateStubComponentsTransformerConfig): VTUVNodeTypeTransformer {
116116
return function componentsTransformer(type, instance) {
117117
// stub teleport by default via config.global.stubs
118-
if ((type as any) === Teleport && 'teleport' in stubs) {
118+
if (isTeleport(type) && 'teleport' in stubs) {
119119
if (stubs.teleport === false) return type
120120

121121
return createStub({
@@ -125,9 +125,10 @@ export function createStubComponentsTransformer({
125125
})
126126
}
127127

128-
// stub keep-alive by default via config.global.stubs
129-
if ((type as any) === KeepAlive && 'keep-alive' in stubs) {
130-
if (stubs['keep-alive'] === false) return type
128+
// stub keep-alive/KeepAlive by default via config.global.stubs
129+
if (isKeepAlive(type) && ('keep-alive' in stubs || 'KeepAlive' in stubs)) {
130+
if ('keep-alive' in stubs && stubs['keep-alive'] === false) return type
131+
if ('KeepAlive' in stubs && stubs['KeepAlive'] === false) return type
131132

132133
return createStub({
133134
name: 'keep-alive',

src/vnodeTransformers/util.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ export type VTUVNodeTypeTransformer = (
2020
instance: InstanceArgsType
2121
) => VNodeTransformerInputComponentType
2222

23-
const isTeleport = (type: any): boolean => type.__isTeleport
24-
const isKeepAlive = (type: any): boolean => type.__isKeepAlive
23+
export const isTeleport = (type: any): boolean => type.__isTeleport
24+
export const isKeepAlive = (type: any): boolean => type.__isKeepAlive
2525

2626
export const createVNodeTransformer = ({
2727
transformers

tests/mountingOptions/global.stubs.spec.ts

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ describe('mounting options: stubs', () => {
563563
expect(wrapper.html()).toBe('<div id="content"></div>')
564564
})
565565

566-
it('opts in to stubbing keep-alive ', () => {
566+
it('opts in to stubbing keep-alive with keep-alive: true', () => {
567567
const spy = vi.spyOn(console, 'warn')
568568
const Comp = {
569569
template: `<keep-alive><div id="content" /></keep-alive>`
@@ -586,6 +586,52 @@ describe('mounting options: stubs', () => {
586586
expect(spy).not.toHaveBeenCalled()
587587
})
588588

589+
it('opts in to stubbing KeepAlive with KeepAlive: true', () => {
590+
const spy = vi.spyOn(console, 'warn')
591+
const Comp = {
592+
template: `<KeepAlive><div id="content" /></KeepAlive>`
593+
}
594+
const wrapper = mount(Comp, {
595+
global: {
596+
stubs: {
597+
KeepAlive: true
598+
}
599+
}
600+
})
601+
602+
expect(wrapper.html()).toBe(
603+
'<keep-alive-stub>\n' +
604+
' <div id="content"></div>\n' +
605+
'</keep-alive-stub>'
606+
)
607+
// Make sure that we don't have a warning when stubbing keep-alive
608+
// https://github.com/vuejs/test-utils/issues/1888
609+
expect(spy).not.toHaveBeenCalled()
610+
})
611+
612+
it('opts in to stubbing keep-alive with KeepAlive: true', () => {
613+
const spy = vi.spyOn(console, 'warn')
614+
const Comp = {
615+
template: `<keep-alive><div id="content" /></keep-alive>`
616+
}
617+
const wrapper = mount(Comp, {
618+
global: {
619+
stubs: {
620+
KeepAlive: true
621+
}
622+
}
623+
})
624+
625+
expect(wrapper.html()).toBe(
626+
'<keep-alive-stub>\n' +
627+
' <div id="content"></div>\n' +
628+
'</keep-alive-stub>'
629+
)
630+
// Make sure that we don't have a warning when stubbing keep-alive
631+
// https://github.com/vuejs/test-utils/issues/1888
632+
expect(spy).not.toHaveBeenCalled()
633+
})
634+
589635
it('does not stub keep-alive with shallow', () => {
590636
const Comp = {
591637
template: `<keep-alive><div id="content" /></keep-alive>`

0 commit comments

Comments
 (0)