Skip to content

Commit 09236b6

Browse files
committed
fix: render props in stubs
1 parent 60f02fb commit 09236b6

File tree

4 files changed

+58
-9
lines changed

4 files changed

+58
-9
lines changed

src/stubs.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,31 @@ import { ComponentInternalInstance } from '@vue/runtime-core'
1818

1919
interface StubOptions {
2020
name?: string
21-
props: any
21+
props?: any
22+
propsDeclaration: any
2223
}
2324

2425
function getSlots(ctx: ComponentPublicInstance): Slots | undefined {
2526
return !config.renderStubDefaultSlot ? undefined : ctx.$slots
2627
}
2728

28-
export const createStub = ({ name, props }: StubOptions): ComponentOptions => {
29+
export const createStub = ({
30+
name,
31+
props,
32+
propsDeclaration
33+
}: StubOptions): ComponentOptions => {
2934
const anonName = 'anonymous-stub'
3035
const tag = name ? `${hyphenate(name)}-stub` : anonName
3136

3237
const render = (ctx: ComponentPublicInstance) => {
33-
return h(tag, {}, getSlots(ctx))
38+
return h(tag, props, getSlots(ctx))
3439
}
3540

36-
return defineComponent({ name: name || anonName, render, props })
41+
return defineComponent({
42+
name: name || anonName,
43+
render,
44+
props: propsDeclaration
45+
})
3746
}
3847

3948
const createTransitionStub = ({
@@ -95,7 +104,10 @@ export function stubComponents(
95104
// stub transition by default via config.global.stubs
96105
if (type === Transition && stubs['transition']) {
97106
return [
98-
createTransitionStub({ name: 'transition-stub', props: undefined }),
107+
createTransitionStub({
108+
name: 'transition-stub',
109+
propsDeclaration: undefined
110+
}),
99111
undefined,
100112
children
101113
]
@@ -106,7 +118,7 @@ export function stubComponents(
106118
return [
107119
createTransitionStub({
108120
name: 'transition-group-stub',
109-
props: undefined
121+
propsDeclaration: undefined
110122
}),
111123
undefined,
112124
children
@@ -158,7 +170,7 @@ export function stubComponents(
158170
if (stub === true || shallow) {
159171
const propsDeclaration = type?.props || {}
160172
return [
161-
createStub({ name, props: propsDeclaration }),
173+
createStub({ name, propsDeclaration, props }),
162174
props,
163175
children,
164176
patchFlag,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`shallowMount renders props for stubbed component in a snapshot 1`] = `<my-label-stub val="username"></my-label-stub>`;

tests/mountingOptions/stubs.global.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ describe('mounting options: stubs', () => {
116116
})
117117

118118
expect(wrapper.html()).toEqual(
119-
'<div><foo-stub class="bar" test-id="foo"></foo-stub></div>'
119+
'<div><foo-stub class="bar" test-id="foo" dynamic="[object Object]"></foo-stub></div>'
120120
)
121121
})
122122

tests/shallowMount.spec.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,41 @@
1-
import { mount, shallowMount } from '../src'
1+
import { defineComponent } from 'vue'
2+
import { mount, shallowMount, VueWrapper } from '../src'
23
import ComponentWithChildren from './components/ComponentWithChildren.vue'
34

45
describe('shallowMount', () => {
6+
it('renders props for stubbed component in a snapshot', () => {
7+
expect.addSnapshotSerializer({
8+
test(wrapper: VueWrapper<any>) {
9+
return '__app' in wrapper
10+
},
11+
serialize(wrapper: VueWrapper<any>) {
12+
return wrapper.html()
13+
}
14+
})
15+
16+
const MyLabel = defineComponent({
17+
props: ['val'],
18+
template: '<label :for="val">{{ val }}</label>'
19+
})
20+
21+
const Component = defineComponent({
22+
components: { MyLabel },
23+
template: '<MyLabel val="username" />',
24+
data() {
25+
return {
26+
foo: 'bar'
27+
}
28+
}
29+
})
30+
31+
const wrapper = shallowMount(Component)
32+
33+
expect(wrapper.html()).toBe(
34+
'<my-label-stub val="username"></my-label-stub>'
35+
)
36+
expect(wrapper).toMatchSnapshot()
37+
})
38+
539
it('stubs all components automatically using { shallow: true }', () => {
640
const wrapper = mount(ComponentWithChildren, { shallow: true })
741
expect(wrapper.html()).toEqual(

0 commit comments

Comments
 (0)