Skip to content

Commit

Permalink
chore: rebasing create-component-stubs
Browse files Browse the repository at this point in the history
  • Loading branch information
JessicaSachs committed Jan 21, 2020
1 parent a7fdcbd commit 97228ce
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 727 deletions.
91 changes: 57 additions & 34 deletions packages/create-instance/create-component-stubs.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {
throwError,
camelize,
capitalize,
hyphenate,
keys
hyphenate
// keys
} from '../shared/util'
import {
componentNeedsCompiling,
Expand Down Expand Up @@ -80,25 +80,42 @@ function resolveOptions(component, _Vue) {
return options
}

function getScopedSlotRenderFunctions(ctx: any): Array<string> {
// In Vue 2.6+ a new v-slot syntax was introduced
// scopedSlots are now saved in parent._vnode.data.scopedSlots
// We filter out the _normalized and $stable key
if (
ctx &&
ctx.$options &&
ctx.$options.parent &&
ctx.$options.parent._vnode &&
ctx.$options.parent._vnode.data &&
ctx.$options.parent._vnode.data.scopedSlots
) {
const slotKeys: Array<string> = ctx.$options.parent._vnode.data.scopedSlots
return keys(slotKeys).filter(x => x !== '_normalized' && x !== '$stable')
}

return []
function getAttributes(
functional: boolean,
functionalCtx: any,
componentCtx
): Object {
return functional
? {
...functionalCtx.props,
...functionalCtx.data.attrs,
class: createClassString(
functionalCtx.data.staticClass,
functionalCtx.data.class
)
}
: componentCtx.$props
}

// function getScopedSlotRenderFunctions(ctx: any): Array<string> {
// // In Vue 2.6+ a new v-slot syntax was introduced
// // scopedSlots are now saved in parent._vnode.data.scopedSlots
// // We filter out the _normalized and $stable key
// if (
// ctx &&
// ctx.$options &&
// ctx.$options.parent &&
// ctx.$options.parent._vnode &&
// ctx.$options.parent._vnode.data &&
// ctx.$options.parent._vnode.data.scopedSlots
// ) {
// const slotKeys: Array<string> = ctx.$options.parent._vnode.data.scopedSlots
// return keys(slotKeys).filter(x => x !== '_normalized' && x !== '$stable')
// }
//
// return []
// }

export function createStubFromComponent(
originalComponent: Component,
name: string,
Expand All @@ -120,19 +137,7 @@ export function createStubFromComponent(
$_vueTestUtils_original: originalComponent,
$_doNotStubChildren: true,
render(h, context) {
const attrs = componentOptions.functional
? {
...context.props,
...context.data.attrs,
class: createClassString(
context.data.staticClass,
context.data.class
)
}
: {
...this.$props
}

const attrs = getAttributes(componentOptions.functional, context, this)
const slots = context ? context.slots() : this._renderProxy.$slots

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

const children = sortedSlotEntries.map(([slotName, slotChildren]) =>
const sortedSlots = sortedSlotEntries.map(([slotName, slotChildren]) =>
slotName === 'default'
? slotChildren
: h('template-stub', { attrs: { slot: slotName } }, slotChildren)
)

return h(tagName, { attrs }, children)
// let children
// if (context) {
// children = sortedSlots
// } else if (this.$options._renderChildren) {
// children = this.$options._renderChildren
// } else {
// children = getScopedSlotRenderFunctions(this).map(x =>
// this.$options.parent._vnode.data.scopedSlots[x]()
// )
// }

return h(
tagName,
{
attrs,
ref: componentOptions.functional ? context.data.ref : undefined
},
sortedSlots
)
}
}
}
Expand Down
12 changes: 9 additions & 3 deletions test/specs/shallow-mount.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { compileToFunctions } from 'vue-template-compiler'
import Vue from 'vue'
import { mount, shallowMount, createLocalVue } from '~vue/test-utils'
import { mount, shallowMount, createLocalVue } from '@vue/test-utils'
import Component from '~resources/components/component.vue'
import ComponentWithChild from '~resources/components/component-with-child.vue'
import ComponentWithNestedChildren from '~resources/components/component-with-nested-children.vue'
Expand Down Expand Up @@ -109,7 +109,9 @@ describeRunIf(process.env.TEST_ENV !== 'node', 'shallowMount', () => {
expect(wrapper.find('.new-example').exists()).to.equal(true)
expect(wrapper.html()).to.equal(
'<componentwithvslot-stub>\n' +
' <p class="new-example">new slot syntax</p>\n' +
' <template-stub slot="newSyntax">\n' +
' <p class="new-example">new slot syntax</p>\n' +
' </template-stub>\n' +
'</componentwithvslot-stub>'
)
})
Expand All @@ -134,7 +136,11 @@ describeRunIf(process.env.TEST_ENV !== 'node', 'shallowMount', () => {
expect(wrapper.find({ name: 'Foo' }).exists()).to.equal(true)
expect(wrapper.find('.new-example').exists()).to.equal(true)
expect(wrapper.html()).to.equal(
'<foo-stub>\n' + ' <p class="new-example">text</p>\n' + '</foo-stub>'
'<foo-stub>\n' +
' <template-stub slot="newSyntax">\n' +
' <p class="new-example">text</p>\n' +
' </template-stub>\n' +
'</foo-stub>'
)
})

Expand Down

0 comments on commit 97228ce

Please sign in to comment.