Skip to content

Commit e74e2bf

Browse files
authored
feat: support array class binding in stubbed functional components (#1744)
Co-authored-by: palpich <[email protected]>
1 parent 62dec1c commit e74e2bf

File tree

3 files changed

+26
-37
lines changed

3 files changed

+26
-37
lines changed

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

+9-36
Original file line numberDiff line numberDiff line change
@@ -61,28 +61,6 @@ function getCoreProperties(componentOptions: Component): Object {
6161
}
6262
}
6363

64-
function createClassString(staticClass, dynamicClass) {
65-
// :class="someComputedObject" can return a string, object or undefined
66-
// if it is a string, we don't need to do anything special.
67-
let evaluatedDynamicClass = dynamicClass
68-
69-
// if it is an object, eg { 'foo': true }, we need to evaluate it.
70-
// see https://github.com/vuejs/vue-test-utils/issues/1474 for more context.
71-
if (typeof dynamicClass === 'object') {
72-
evaluatedDynamicClass = Object.keys(dynamicClass).reduce((acc, key) => {
73-
if (dynamicClass[key]) {
74-
return acc + ' ' + key
75-
}
76-
return acc
77-
}, '')
78-
}
79-
80-
if (staticClass && evaluatedDynamicClass) {
81-
return staticClass + ' ' + evaluatedDynamicClass
82-
}
83-
return staticClass || evaluatedDynamicClass
84-
}
85-
8664
function resolveOptions(component, _Vue) {
8765
if (isDynamicComponent(component)) {
8866
return {}
@@ -134,24 +112,19 @@ export function createStubFromComponent(
134112
render(h, context) {
135113
return h(
136114
tagName,
137-
{
138-
ref: componentOptions.functional ? context.data.ref : undefined,
139-
domProps: componentOptions.functional
140-
? context.data.domProps
141-
: undefined,
142-
attrs: componentOptions.functional
143-
? {
115+
componentOptions.functional
116+
? {
117+
...context.data,
118+
attrs: {
144119
...context.props,
145-
...context.data.attrs,
146-
class: createClassString(
147-
context.data.staticClass,
148-
context.data.class
149-
)
120+
...context.data.attrs
150121
}
151-
: {
122+
}
123+
: {
124+
attrs: {
152125
...this.$props
153126
}
154-
},
127+
},
155128
context
156129
? context.children
157130
: this.$options._renderChildren ||

Diff for: test/resources/components/component-with-functional-child.vue

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<template>
22
<functional-component
3-
:class="{ bar: a + b === 2, foo: a === 1, qux: a === 2 }"
3+
:class="['baz', { bar: a + b === 2, foo: a === 1, qux: a === 2 }]"
44
v-text="something"
5+
@click="handleFunctionalComponentClick"
56
/>
67
</template>
78

@@ -19,6 +20,12 @@ export default {
1920
b: 1,
2021
something: 'value'
2122
}
23+
},
24+
25+
methods: {
26+
handleFunctionalComponentClick() {
27+
this.something = 'newValue'
28+
}
2229
}
2330
}
2431
</script>

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

+9
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ describeRunIf(process.env.TEST_ENV !== 'node', 'shallowMount', () => {
3333
it('renders dynamic class of functional child', () => {
3434
const wrapper = shallowMount(ComponentWithFunctionalChild)
3535
expect(wrapper.find('functional-component-stub').classes()).toContain(
36+
'baz',
3637
'foo',
3738
'bar'
3839
)
@@ -46,6 +47,14 @@ describeRunIf(process.env.TEST_ENV !== 'node', 'shallowMount', () => {
4647
expect(wrapper.find('functional-component-stub').text()).toBe('value')
4748
})
4849

50+
it('trigger click must change content of functional child', async () => {
51+
const wrapper = shallowMount(ComponentWithFunctionalChild)
52+
53+
await wrapper.trigger('click')
54+
55+
expect(wrapper.find('functional-component-stub').text()).toBe('newValue')
56+
})
57+
4958
it('returns new VueWrapper of Vue localVue if no options are passed', () => {
5059
const compiled = compileToFunctions('<div><input /></div>')
5160
const wrapper = shallowMount(compiled)

0 commit comments

Comments
 (0)