Skip to content

Commit

Permalink
fix: do not stub slots content when using shallow mount
Browse files Browse the repository at this point in the history
  • Loading branch information
xanf committed Jun 27, 2021
1 parent 015c765 commit 002204d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 15 deletions.
10 changes: 10 additions & 0 deletions src/stubs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ interface StubOptions {
renderStubDefaultSlot?: boolean
}

const doNotStubComponents: WeakSet<ComponentOptions> = new WeakSet()

const shouldNotStub = (type: ComponentOptions) => doNotStubComponents.has(type)
export const addToDoNotStubComponents = (type: ComponentOptions) =>
doNotStubComponents.add(type)

export const createStub = ({
name,
propsDeclaration,
Expand Down Expand Up @@ -154,6 +160,10 @@ export function stubComponents(
}

if (isComponent(type) || isFunctionalComponent(type)) {
if (shouldNotStub(type)) {
return args
}

const registeredName = getComponentRegisteredName(instance, type)
const componentName = type['name'] || type['displayName']

Expand Down
36 changes: 21 additions & 15 deletions src/utils/compileSlots.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
import { compile } from '@vue/compiler-dom'
import * as vue from 'vue'
import type { SetupContext } from 'vue'
import { addToDoNotStubComponents } from '../stubs'

const SlotWrapper = {
inheritAttrs: false,
setup(_: Record<string, any>, ctx: SetupContext) {
return () => {
const names = Object.keys(ctx.slots)
if (names.length === 0) {
return []
} else {
const slotName = names[0]
return ctx.slots[slotName]!(ctx.attrs)
}
}
}
}
addToDoNotStubComponents(SlotWrapper)

export function processSlot(source = '', Vue = vue) {
let template = source.trim()
Expand All @@ -23,24 +40,13 @@ export function processSlot(source = '', Vue = vue) {
__BROWSER__ ? `'use strict';\n${code}` : code
)

return {
const Component = {
inheritAttrs: false,
render: createRenderFunction(Vue),
components: {
SlotWrapper: {
inheritAttrs: false,
setup(_: Record<string, any>, ctx: SetupContext) {
return () => {
const names = Object.keys(ctx.slots)
if (names.length === 0) {
return []
} else {
const slotName = names[0]
return ctx.slots[slotName]!(ctx.attrs)
}
}
}
}
SlotWrapper
}
}
addToDoNotStubComponents(Component)
return Component
}
13 changes: 13 additions & 0 deletions tests/shallowMount.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,19 @@ describe('shallowMount', () => {
)
})

it('correctly renders slot content', () => {
const ComponentWithSlot = defineComponent({
template: '<div><slot></slot></label>'
})

const wrapper = shallowMount(ComponentWithSlot, {
slots: {
default: '<span class="slot-content">test</span>'
}
})
expect(wrapper.find('.slot-content').exists()).toBe(true)
})

it('stubs all components, but allows providing custom stub', () => {
const wrapper = mount(ComponentWithChildren, {
shallow: true,
Expand Down

0 comments on commit 002204d

Please sign in to comment.