Skip to content

Commit 97228ce

Browse files
committed
chore: rebasing create-component-stubs
1 parent a7fdcbd commit 97228ce

File tree

3 files changed

+67
-727
lines changed

3 files changed

+67
-727
lines changed

packages/create-instance/create-component-stubs.js

+57-34
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import {
55
throwError,
66
camelize,
77
capitalize,
8-
hyphenate,
9-
keys
8+
hyphenate
9+
// keys
1010
} from '../shared/util'
1111
import {
1212
componentNeedsCompiling,
@@ -80,25 +80,42 @@ function resolveOptions(component, _Vue) {
8080
return options
8181
}
8282

83-
function getScopedSlotRenderFunctions(ctx: any): Array<string> {
84-
// In Vue 2.6+ a new v-slot syntax was introduced
85-
// scopedSlots are now saved in parent._vnode.data.scopedSlots
86-
// We filter out the _normalized and $stable key
87-
if (
88-
ctx &&
89-
ctx.$options &&
90-
ctx.$options.parent &&
91-
ctx.$options.parent._vnode &&
92-
ctx.$options.parent._vnode.data &&
93-
ctx.$options.parent._vnode.data.scopedSlots
94-
) {
95-
const slotKeys: Array<string> = ctx.$options.parent._vnode.data.scopedSlots
96-
return keys(slotKeys).filter(x => x !== '_normalized' && x !== '$stable')
97-
}
98-
99-
return []
83+
function getAttributes(
84+
functional: boolean,
85+
functionalCtx: any,
86+
componentCtx
87+
): Object {
88+
return functional
89+
? {
90+
...functionalCtx.props,
91+
...functionalCtx.data.attrs,
92+
class: createClassString(
93+
functionalCtx.data.staticClass,
94+
functionalCtx.data.class
95+
)
96+
}
97+
: componentCtx.$props
10098
}
10199

100+
// function getScopedSlotRenderFunctions(ctx: any): Array<string> {
101+
// // In Vue 2.6+ a new v-slot syntax was introduced
102+
// // scopedSlots are now saved in parent._vnode.data.scopedSlots
103+
// // We filter out the _normalized and $stable key
104+
// if (
105+
// ctx &&
106+
// ctx.$options &&
107+
// ctx.$options.parent &&
108+
// ctx.$options.parent._vnode &&
109+
// ctx.$options.parent._vnode.data &&
110+
// ctx.$options.parent._vnode.data.scopedSlots
111+
// ) {
112+
// const slotKeys: Array<string> = ctx.$options.parent._vnode.data.scopedSlots
113+
// return keys(slotKeys).filter(x => x !== '_normalized' && x !== '$stable')
114+
// }
115+
//
116+
// return []
117+
// }
118+
102119
export function createStubFromComponent(
103120
originalComponent: Component,
104121
name: string,
@@ -120,19 +137,7 @@ export function createStubFromComponent(
120137
$_vueTestUtils_original: originalComponent,
121138
$_doNotStubChildren: true,
122139
render(h, context) {
123-
const attrs = componentOptions.functional
124-
? {
125-
...context.props,
126-
...context.data.attrs,
127-
class: createClassString(
128-
context.data.staticClass,
129-
context.data.class
130-
)
131-
}
132-
: {
133-
...this.$props
134-
}
135-
140+
const attrs = getAttributes(componentOptions.functional, context, this)
136141
const slots = context ? context.slots() : this._renderProxy.$slots
137142

138143
// ensure consistent ordering of slots (default first, then alphabetical)
@@ -145,13 +150,31 @@ export function createStubFromComponent(
145150
: slotNameA.localeCompare(slotNameB)
146151
)
147152

148-
const children = sortedSlotEntries.map(([slotName, slotChildren]) =>
153+
const sortedSlots = sortedSlotEntries.map(([slotName, slotChildren]) =>
149154
slotName === 'default'
150155
? slotChildren
151156
: h('template-stub', { attrs: { slot: slotName } }, slotChildren)
152157
)
153158

154-
return h(tagName, { attrs }, children)
159+
// let children
160+
// if (context) {
161+
// children = sortedSlots
162+
// } else if (this.$options._renderChildren) {
163+
// children = this.$options._renderChildren
164+
// } else {
165+
// children = getScopedSlotRenderFunctions(this).map(x =>
166+
// this.$options.parent._vnode.data.scopedSlots[x]()
167+
// )
168+
// }
169+
170+
return h(
171+
tagName,
172+
{
173+
attrs,
174+
ref: componentOptions.functional ? context.data.ref : undefined
175+
},
176+
sortedSlots
177+
)
155178
}
156179
}
157180
}

test/specs/shallow-mount.spec.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { compileToFunctions } from 'vue-template-compiler'
22
import Vue from 'vue'
3-
import { mount, shallowMount, createLocalVue } from '~vue/test-utils'
3+
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'
66
import ComponentWithNestedChildren from '~resources/components/component-with-nested-children.vue'
@@ -109,7 +109,9 @@ describeRunIf(process.env.TEST_ENV !== 'node', 'shallowMount', () => {
109109
expect(wrapper.find('.new-example').exists()).to.equal(true)
110110
expect(wrapper.html()).to.equal(
111111
'<componentwithvslot-stub>\n' +
112-
' <p class="new-example">new slot syntax</p>\n' +
112+
' <template-stub slot="newSyntax">\n' +
113+
' <p class="new-example">new slot syntax</p>\n' +
114+
' </template-stub>\n' +
113115
'</componentwithvslot-stub>'
114116
)
115117
})
@@ -134,7 +136,11 @@ describeRunIf(process.env.TEST_ENV !== 'node', 'shallowMount', () => {
134136
expect(wrapper.find({ name: 'Foo' }).exists()).to.equal(true)
135137
expect(wrapper.find('.new-example').exists()).to.equal(true)
136138
expect(wrapper.html()).to.equal(
137-
'<foo-stub>\n' + ' <p class="new-example">text</p>\n' + '</foo-stub>'
139+
'<foo-stub>\n' +
140+
' <template-stub slot="newSyntax">\n' +
141+
' <p class="new-example">text</p>\n' +
142+
' </template-stub>\n' +
143+
'</foo-stub>'
138144
)
139145
})
140146

0 commit comments

Comments
 (0)