Skip to content

Commit 4c81246

Browse files
committed
fix: allow to use KeepAlive or keep-alive in stubs
1 parent 5b49e59 commit 4c81246

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

src/vnodeTransformers/stubComponentsTransformer.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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 ((type as any) === KeepAlive && ('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',

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)