Skip to content

Commit

Permalink
fix: render classes of functional component stubs (#898)
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyerburgh authored Aug 4, 2018
1 parent 1bc37f1 commit 11cfee2
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
13 changes: 12 additions & 1 deletion packages/shared/stub-components.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ function createStubFromString (
}
}

function createClassString (staticClass, dynamicClass) {
if (staticClass && dynamicClass) {
return staticClass + ' ' + dynamicClass
}
return staticClass || dynamicClass
}

function createBlankStub (
originalComponent: Component,
name: string
Expand All @@ -105,7 +112,11 @@ function createBlankStub (
{
attrs: componentOptions.functional ? {
...context.props,
...context.data.attrs
...context.data.attrs,
class: createClassString(
context.data.staticClass,
context.data.class
)
} : {
...this.$props
}
Expand Down
51 changes: 51 additions & 0 deletions test/specs/shallow-mount.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,22 @@ describeRunIf(process.env.TEST_ENV !== 'node', 'shallowMount', () => {
expect(wrapper.html()).to.contain('<child-stub prop="a" attr="hello"')
})

itDoNotRunIf(
vueVersion < 2.2, // $props does not exist in Vue < 2.2
'renders stubs classes', () => {
const TestComponent = {
template: `<child :class="classA" class="b" />`,
data: () => ({
'classA': 'a'
}),
components: {
child: { template: '<div />' }
}
}
const wrapper = shallowMount(TestComponent)
expect(wrapper.html()).to.contain('<child-stub class="b a"')
})

it('renders stubs props for functional components', () => {
const TestComponent = {
template: `<child :prop="propA" attr="hello" />`,
Expand All @@ -173,6 +189,41 @@ describeRunIf(process.env.TEST_ENV !== 'node', 'shallowMount', () => {
expect(wrapper.html()).to.contain('<child-stub prop="a" attr="hello"')
})

it('renders classes for functional components', () => {
const components = {
Child: {
functional: true
}
}
const TestComponent = {
template: `<child :class="classA" class="b" />`,
data: () => ({
'classA': 'a'
}),
components
}
const wrapper = shallowMount(TestComponent)
expect(wrapper.html()).to.contain('<child-stub class="b a"')
const TestComponent2 = {
template: `<child :class="classA"/>`,
data: () => ({
'classA': 'a'
}),
components
}
const wrapper2 = shallowMount(TestComponent2)
expect(wrapper2.html()).to.contain('<child-stub class="a"')
const TestComponent3 = {
template: `<child class="b" />`,
data: () => ({
'classA': 'a'
}),
components
}
const wrapper3 = shallowMount(TestComponent3)
expect(wrapper3.html()).to.contain('<child-stub class="b"')
})

itDoNotRunIf(vueVersion < 2.1, 'handles recursive components', () => {
const TestComponent = {
template: `
Expand Down

0 comments on commit 11cfee2

Please sign in to comment.