Skip to content

Commit 55f7eac

Browse files
authored
feat: support object class binding in stubbed functional components (#1476)
* feat: support object class binding in stubbed functional components * fix: typo * fix: do not check for strict true
1 parent aff87f8 commit 55f7eac

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed

Diff for: packages/create-instance/create-component-stubs.js

+18-3
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,25 @@ function getCoreProperties(componentOptions: Component): Object {
6060
}
6161

6262
function createClassString(staticClass, dynamicClass) {
63-
if (staticClass && dynamicClass) {
64-
return staticClass + ' ' + dynamicClass
63+
// :class="someComputedObject" can return a string, object or undefined
64+
// if it is a string, we don't need to do anything special.
65+
let evaluatedDynamicClass = dynamicClass
66+
67+
// if it is an object, eg { 'foo': true }, we need to evaluate it.
68+
// see https://github.com/vuejs/vue-test-utils/issues/1474 for more context.
69+
if (typeof dynamicClass === 'object') {
70+
evaluatedDynamicClass = Object.keys(dynamicClass).reduce((acc, key) => {
71+
if (dynamicClass[key]) {
72+
return acc + ' ' + key
73+
}
74+
return acc
75+
}, '')
76+
}
77+
78+
if (staticClass && evaluatedDynamicClass) {
79+
return staticClass + ' ' + evaluatedDynamicClass
6580
}
66-
return staticClass || dynamicClass
81+
return staticClass || evaluatedDynamicClass
6782
}
6883

6984
function resolveOptions(component, _Vue) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<template>
2+
<functional-component
3+
:class="{ bar: a + b === 2, foo: a === 1, qux: a === 2 }"
4+
/>
5+
</template>
6+
7+
<script>
8+
import FunctionalComponent from './functional-component.vue'
9+
10+
export default {
11+
components: {
12+
FunctionalComponent
13+
},
14+
15+
data() {
16+
return {
17+
a: 1,
18+
b: 1
19+
}
20+
}
21+
}
22+
</script>

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

+12
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Vue from 'vue'
33
import { mount, shallowMount, createLocalVue } from '@vue/test-utils'
44
import Component from '~resources/components/component.vue'
55
import ComponentWithChild from '~resources/components/component-with-child.vue'
6+
import ComponentWithFunctionalChild from '~resources/components/component-with-functional-child.vue'
67
import ComponentWithNestedChildren from '~resources/components/component-with-nested-children.vue'
78
import ComponentWithLifecycleHooks from '~resources/components/component-with-lifecycle-hooks.vue'
89
import ComponentWithoutName from '~resources/components/component-without-name.vue'
@@ -25,6 +26,17 @@ describeRunIf(process.env.TEST_ENV !== 'node', 'shallowMount', () => {
2526
sandbox.restore()
2627
})
2728

29+
it('renders dynamic class of functional child', () => {
30+
const wrapper = shallowMount(ComponentWithFunctionalChild)
31+
expect(wrapper.find('functional-component-stub').classes()).to.contain(
32+
'foo',
33+
'bar'
34+
)
35+
expect(wrapper.find('functional-component-stub').classes()).not.to.contain(
36+
'qux'
37+
)
38+
})
39+
2840
it('returns new VueWrapper of Vue localVue if no options are passed', () => {
2941
const compiled = compileToFunctions('<div><input /></div>')
3042
const wrapper = shallowMount(compiled)

0 commit comments

Comments
 (0)