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

#1888: adding stubbing for keep-alive #1889

Merged
merged 1 commit into from Dec 2, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/vnodeTransformers/stubComponentsTransformer.ts
Expand Up @@ -4,6 +4,7 @@ import {
TransitionGroup,
BaseTransition,
Teleport,
KeepAlive,
h,
defineComponent,
VNodeTypes,
Expand Down Expand Up @@ -31,7 +32,7 @@ export type CustomCreateStub = (params: {

interface StubOptions {
name: string
type?: VNodeTypes | typeof Teleport
type?: VNodeTypes | typeof Teleport | typeof KeepAlive
renderStubDefaultSlot?: boolean
}

Expand Down Expand Up @@ -124,6 +125,17 @@ export function createStubComponentsTransformer({
})
}

// stub keep-alive by default via config.global.stubs
if ((type as any) === KeepAlive && 'keep-alive' in stubs) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we should support KeepAlive: true as well?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: we could also use isKeepAlive instead of (type as any) === KeepAlive (and do the same for Teleport)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we just push this commit on behalf of the original author and ship this?

if (stubs['keep-alive'] === false) return type

return createStub({
name: 'keep-alive',
type,
renderStubDefaultSlot: true
})
}

// stub transition by default via config.global.stubs
if (
(type === Transition || (type as any) === BaseTransition) &&
Expand Down
13 changes: 7 additions & 6 deletions src/vnodeTransformers/util.ts
Expand Up @@ -21,6 +21,7 @@ export type VTUVNodeTypeTransformer = (
) => VNodeTransformerInputComponentType

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

export const createVNodeTransformer = ({
transformers
Expand All @@ -41,9 +42,9 @@ export const createVNodeTransformer = ({

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

registerStub({ source: originalType, stub: transformedType })
// https://github.com/vuejs/test-utils/issues/1829
// Teleport should return child nodes as a function
if (isTeleport(originalType)) {
// https://github.com/vuejs/test-utils/issues/1829 & https://github.com/vuejs/test-utils/issues/1888
// Teleport/KeepAlive should return child nodes as a function
if (isTeleport(originalType) || isKeepAlive(originalType)) {
return [transformedType, props, () => children, ...restVNodeArgs]
}
}
Expand Down
50 changes: 50 additions & 0 deletions tests/mountingOptions/global.stubs.spec.ts
Expand Up @@ -553,6 +553,56 @@ describe('mounting options: stubs', () => {
})
})

describe('keep-alive', () => {
it('will omit the keep-alive tag by default', () => {
const Comp = {
template: `<keep-alive><div id="content" /></keep-alive>`
}
const wrapper = mount(Comp)

expect(wrapper.html()).toBe('<div id="content"></div>')
})

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

expect(wrapper.html()).toBe('<div id="content"></div>')
})
})

it('stubs component by key prior before name', () => {
const MyComponent = defineComponent({
name: 'MyComponent',
Expand Down