Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow to use KeepAlive or keep-alive in stubs #1892

Merged
merged 2 commits into from
Dec 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/vnodeTransformers/stubComponentsTransformer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { VTUVNodeTypeTransformer } from './util'
import { isKeepAlive, isTeleport, VTUVNodeTypeTransformer } from './util'
import {
Transition,
TransitionGroup,
Expand Down Expand Up @@ -115,7 +115,7 @@ export function createStubComponentsTransformer({
}: CreateStubComponentsTransformerConfig): VTUVNodeTypeTransformer {
return function componentsTransformer(type, instance) {
// stub teleport by default via config.global.stubs
if ((type as any) === Teleport && 'teleport' in stubs) {
if (isTeleport(type) && 'teleport' in stubs) {
if (stubs.teleport === false) return type

return createStub({
Expand All @@ -125,9 +125,10 @@ export function createStubComponentsTransformer({
})
}

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

return createStub({
name: 'keep-alive',
Expand Down
4 changes: 2 additions & 2 deletions src/vnodeTransformers/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ export type VTUVNodeTypeTransformer = (
instance: InstanceArgsType
) => VNodeTransformerInputComponentType

const isTeleport = (type: any): boolean => type.__isTeleport
const isKeepAlive = (type: any): boolean => type.__isKeepAlive
export const isTeleport = (type: any): boolean => type.__isTeleport
export const isKeepAlive = (type: any): boolean => type.__isKeepAlive

export const createVNodeTransformer = ({
transformers
Expand Down
48 changes: 47 additions & 1 deletion tests/mountingOptions/global.stubs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ describe('mounting options: stubs', () => {
expect(wrapper.html()).toBe('<div id="content"></div>')
})

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

it('opts in to stubbing KeepAlive with KeepAlive: true', () => {
const spy = vi.spyOn(console, 'warn')
const Comp = {
template: `<KeepAlive><div id="content" /></KeepAlive>`
}
const wrapper = mount(Comp, {
global: {
stubs: {
KeepAlive: true
}
}
})

expect(wrapper.html()).toBe(
'<keep-alive-stub>\n' +
' <div id="content"></div>\n' +
'</keep-alive-stub>'
)
// Make sure that we don't have a warning when stubbing keep-alive
// https://github.com/vuejs/test-utils/issues/1888
expect(spy).not.toHaveBeenCalled()
})

it('opts in to stubbing keep-alive with KeepAlive: true', () => {
const spy = vi.spyOn(console, 'warn')
const Comp = {
template: `<keep-alive><div id="content" /></keep-alive>`
}
const wrapper = mount(Comp, {
global: {
stubs: {
KeepAlive: true
}
}
})

expect(wrapper.html()).toBe(
'<keep-alive-stub>\n' +
' <div id="content"></div>\n' +
'</keep-alive-stub>'
)
// Make sure that we don't have a warning when stubbing keep-alive
// https://github.com/vuejs/test-utils/issues/1888
expect(spy).not.toHaveBeenCalled()
})

it('does not stub keep-alive with shallow', () => {
const Comp = {
template: `<keep-alive><div id="content" /></keep-alive>`
Expand Down