Skip to content

fix: render classes as attrs in functional component stubs #898

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

Merged
merged 1 commit into from
Aug 4, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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