Skip to content

Commit 11cfee2

Browse files
authored
fix: render classes of functional component stubs (#898)
1 parent 1bc37f1 commit 11cfee2

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

Diff for: packages/shared/stub-components.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ function createStubFromString (
8383
}
8484
}
8585

86+
function createClassString (staticClass, dynamicClass) {
87+
if (staticClass && dynamicClass) {
88+
return staticClass + ' ' + dynamicClass
89+
}
90+
return staticClass || dynamicClass
91+
}
92+
8693
function createBlankStub (
8794
originalComponent: Component,
8895
name: string
@@ -105,7 +112,11 @@ function createBlankStub (
105112
{
106113
attrs: componentOptions.functional ? {
107114
...context.props,
108-
...context.data.attrs
115+
...context.data.attrs,
116+
class: createClassString(
117+
context.data.staticClass,
118+
context.data.class
119+
)
109120
} : {
110121
...this.$props
111122
}

Diff for: test/specs/shallow-mount.spec.js

+51
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,22 @@ describeRunIf(process.env.TEST_ENV !== 'node', 'shallowMount', () => {
156156
expect(wrapper.html()).to.contain('<child-stub prop="a" attr="hello"')
157157
})
158158

159+
itDoNotRunIf(
160+
vueVersion < 2.2, // $props does not exist in Vue < 2.2
161+
'renders stubs classes', () => {
162+
const TestComponent = {
163+
template: `<child :class="classA" class="b" />`,
164+
data: () => ({
165+
'classA': 'a'
166+
}),
167+
components: {
168+
child: { template: '<div />' }
169+
}
170+
}
171+
const wrapper = shallowMount(TestComponent)
172+
expect(wrapper.html()).to.contain('<child-stub class="b a"')
173+
})
174+
159175
it('renders stubs props for functional components', () => {
160176
const TestComponent = {
161177
template: `<child :prop="propA" attr="hello" />`,
@@ -173,6 +189,41 @@ describeRunIf(process.env.TEST_ENV !== 'node', 'shallowMount', () => {
173189
expect(wrapper.html()).to.contain('<child-stub prop="a" attr="hello"')
174190
})
175191

192+
it('renders classes for functional components', () => {
193+
const components = {
194+
Child: {
195+
functional: true
196+
}
197+
}
198+
const TestComponent = {
199+
template: `<child :class="classA" class="b" />`,
200+
data: () => ({
201+
'classA': 'a'
202+
}),
203+
components
204+
}
205+
const wrapper = shallowMount(TestComponent)
206+
expect(wrapper.html()).to.contain('<child-stub class="b a"')
207+
const TestComponent2 = {
208+
template: `<child :class="classA"/>`,
209+
data: () => ({
210+
'classA': 'a'
211+
}),
212+
components
213+
}
214+
const wrapper2 = shallowMount(TestComponent2)
215+
expect(wrapper2.html()).to.contain('<child-stub class="a"')
216+
const TestComponent3 = {
217+
template: `<child class="b" />`,
218+
data: () => ({
219+
'classA': 'a'
220+
}),
221+
components
222+
}
223+
const wrapper3 = shallowMount(TestComponent3)
224+
expect(wrapper3.html()).to.contain('<child-stub class="b"')
225+
})
226+
176227
itDoNotRunIf(vueVersion < 2.1, 'handles recursive components', () => {
177228
const TestComponent = {
178229
template: `

0 commit comments

Comments
 (0)