Skip to content

Commit 4ef369d

Browse files
authored
feat: adding stubbing for keep-alive (#1889)
1 parent d9e62d3 commit 4ef369d

File tree

3 files changed

+70
-7
lines changed

3 files changed

+70
-7
lines changed

src/vnodeTransformers/stubComponentsTransformer.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
TransitionGroup,
55
BaseTransition,
66
Teleport,
7+
KeepAlive,
78
h,
89
defineComponent,
910
VNodeTypes,
@@ -31,7 +32,7 @@ export type CustomCreateStub = (params: {
3132

3233
interface StubOptions {
3334
name: string
34-
type?: VNodeTypes | typeof Teleport
35+
type?: VNodeTypes | typeof Teleport | typeof KeepAlive
3536
renderStubDefaultSlot?: boolean
3637
}
3738

@@ -124,6 +125,17 @@ export function createStubComponentsTransformer({
124125
})
125126
}
126127

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
131+
132+
return createStub({
133+
name: 'keep-alive',
134+
type,
135+
renderStubDefaultSlot: true
136+
})
137+
}
138+
127139
// stub transition by default via config.global.stubs
128140
if (
129141
(type === Transition || (type as any) === BaseTransition) &&

src/vnodeTransformers/util.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export type VTUVNodeTypeTransformer = (
2121
) => VNodeTransformerInputComponentType
2222

2323
const isTeleport = (type: any): boolean => type.__isTeleport
24+
const isKeepAlive = (type: any): boolean => type.__isKeepAlive
2425

2526
export const createVNodeTransformer = ({
2627
transformers
@@ -41,9 +42,9 @@ export const createVNodeTransformer = ({
4142

4243
const cachedTransformation = transformationCache.get(originalType)
4344
if (cachedTransformation) {
44-
// https://github.com/vuejs/test-utils/issues/1829
45-
// Teleport should return child nodes as a function
46-
if (isTeleport(originalType)) {
45+
// https://github.com/vuejs/test-utils/issues/1829 & https://github.com/vuejs/test-utils/issues/1888
46+
// Teleport/KeepAlive should return child nodes as a function
47+
if (isTeleport(originalType) || isKeepAlive(originalType)) {
4748
return [cachedTransformation, props, () => children, ...restVNodeArgs]
4849
}
4950
return [cachedTransformation, props, children, ...restVNodeArgs]
@@ -60,9 +61,9 @@ export const createVNodeTransformer = ({
6061
transformationCache.set(originalType, transformedType)
6162

6263
registerStub({ source: originalType, stub: transformedType })
63-
// https://github.com/vuejs/test-utils/issues/1829
64-
// Teleport should return child nodes as a function
65-
if (isTeleport(originalType)) {
64+
// https://github.com/vuejs/test-utils/issues/1829 & https://github.com/vuejs/test-utils/issues/1888
65+
// Teleport/KeepAlive should return child nodes as a function
66+
if (isTeleport(originalType) || isKeepAlive(originalType)) {
6667
return [transformedType, props, () => children, ...restVNodeArgs]
6768
}
6869
}

tests/mountingOptions/global.stubs.spec.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,56 @@ describe('mounting options: stubs', () => {
553553
})
554554
})
555555

556+
describe('keep-alive', () => {
557+
it('will omit the keep-alive tag by default', () => {
558+
const Comp = {
559+
template: `<keep-alive><div id="content" /></keep-alive>`
560+
}
561+
const wrapper = mount(Comp)
562+
563+
expect(wrapper.html()).toBe('<div id="content"></div>')
564+
})
565+
566+
it('opts in to stubbing keep-alive ', () => {
567+
const spy = vi.spyOn(console, 'warn')
568+
const Comp = {
569+
template: `<keep-alive><div id="content" /></keep-alive>`
570+
}
571+
const wrapper = mount(Comp, {
572+
global: {
573+
stubs: {
574+
'keep-alive': true
575+
}
576+
}
577+
})
578+
579+
expect(wrapper.html()).toBe(
580+
'<keep-alive-stub>\n' +
581+
' <div id="content"></div>\n' +
582+
'</keep-alive-stub>'
583+
)
584+
// Make sure that we don't have a warning when stubbing keep-alive
585+
// https://github.com/vuejs/test-utils/issues/1888
586+
expect(spy).not.toHaveBeenCalled()
587+
})
588+
589+
it('does not stub keep-alive with shallow', () => {
590+
const Comp = {
591+
template: `<keep-alive><div id="content" /></keep-alive>`
592+
}
593+
const wrapper = mount(Comp, {
594+
shallow: true,
595+
global: {
596+
stubs: {
597+
'keep-alive': false
598+
}
599+
}
600+
})
601+
602+
expect(wrapper.html()).toBe('<div id="content"></div>')
603+
})
604+
})
605+
556606
it('stubs component by key prior before name', () => {
557607
const MyComponent = defineComponent({
558608
name: 'MyComponent',

0 commit comments

Comments
 (0)