Skip to content

feat: add support for JSX and strings without a slot-scope attribute #871

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 8 commits into from
Jul 29, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"presets": ["env", "stage-2", "flow-vue"],
"plugins": ["transform-decorators-legacy"],
"plugins": ["transform-decorators-legacy", "transform-vue-jsx"],
"comments": false
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"babel-eslint": "^8.2.2",
"babel-loader": "^7.1.3",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-plugin-transform-vue-jsx": "^3.7.0",
"babel-polyfill": "^6.23.0",
"babel-preset-env": "^1.6.0",
"babel-preset-flow-vue": "^1.0.0",
Expand Down
53 changes: 22 additions & 31 deletions packages/create-instance/create-scoped-slots.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,54 +32,45 @@ function getVueTemplateCompilerHelpers (): { [name: string]: Function } {
names.forEach(name => {
helpers[name] = vue._renderProxy[name]
})
helpers.$createElement = vue._renderProxy.$createElement
return helpers
}

function validateEnvironment (): void {
if (window.navigator.userAgent.match(/PhantomJS/i)) {
throwError(
`the scopedSlots option does not support PhantomJS. ` +
`Please use Puppeteer, or pass a component.`
)
}
if (vueVersion < 2.5) {
throwError(`the scopedSlots option is only supported in ` + `[email protected]+.`)
if (vueVersion < 2.1) {
throwError(`the scopedSlots option is only supported in ` + `[email protected]+.`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the scopedSlots option is only supported in + [email protected]+.

the scopedSlots option is only supported in [email protected]+.

}
}

function validateTempldate (template: string): void {
if (template.trim().substr(0, 9) === '<template') {
throwError(
`the scopedSlots option does not support a template ` +
`tag as the root element.`
)
}
}
const scopedSlotRe = /<[^>]+ slot-scope=\"(.+)\"/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO, I think slotScopeRe is better.


export default function createScopedSlots (
scopedSlotsOption: ?{ [slotName: string]: string }
scopedSlotsOption: ?{ [slotName: string]: string | Function }
): { [slotName: string]: (props: Object) => VNode | Array<VNode>} {
const scopedSlots = {}
if (!scopedSlotsOption) {
return scopedSlots
}
validateEnvironment()
const helpers = getVueTemplateCompilerHelpers()
for (const name in scopedSlotsOption) {
const template = scopedSlotsOption[name]
validateTempldate(template)
const render = compileToFunctions(template).render
const domParser = new window.DOMParser()
const _document = domParser.parseFromString(template, 'text/html')
const slotScope = _document.body.firstChild.getAttribute(
'slot-scope'
)
const isDestructuring = isDestructuringSlotScope(slotScope)
scopedSlots[name] = function (props) {
if (isDestructuring) {
return render.call({ ...helpers, ...props })
for (const s in scopedSlotsOption) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think slotName is better then s.

const slot = scopedSlotsOption[s]
const isFn = typeof slot === 'function'
const renderFn = isFn
? slot
: compileToFunctions(slot).renderFn

const hasSlotScopeAttr = !isFn && slot.match(scopedSlotRe)
// // $FlowIgnore
const slotScope = hasSlotScopeAttr && hasSlotScopeAttr[1]

scopedSlots[s] = function (props) {
if (isFn) {
return renderFn.call({ ...helpers }, props)
} else if (slotScope && !isDestructuringSlotScope(slotScope)) {
return renderFn.call({ ...helpers, [slotScope]: props })
} else {
return render.call({ ...helpers, [slotScope]: props })
return renderFn.call({ ...helpers, ...props })
}
}
}
Expand Down
83 changes: 35 additions & 48 deletions test/specs/mounting-options/scopedSlots.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
describeWithShallowAndMount,
vueVersion,
isRunningPhantomJS
vueVersion
} from '~resources/utils'
import ComponentWithScopedSlots from '~resources/components/component-with-scoped-slots.vue'
import { itDoNotRunIf } from 'conditional-specs'
Expand All @@ -14,7 +13,7 @@ describeWithShallowAndMount('scopedSlots', mountingMethod => {
})

itDoNotRunIf(
vueVersion < 2.5 || isRunningPhantomJS,
vueVersion < 2.5,
'mounts component scoped slots in render function',
() => {
const destructuringWrapper = mountingMethod(
Expand All @@ -38,15 +37,15 @@ describeWithShallowAndMount('scopedSlots', mountingMethod => {
const notDestructuringWrapper = mountingMethod(
{
render: function () {
return this.$scopedSlots.default({
return this.$scopedSlots.named({
index: 1,
item: 'foo'
})
}
},
{
scopedSlots: {
default:
named:
'<p slot-scope="props">{{props.index}},{{props.item}}</p>'
}
}
Expand All @@ -56,15 +55,15 @@ describeWithShallowAndMount('scopedSlots', mountingMethod => {
)

itDoNotRunIf(
vueVersion < 2.5 || isRunningPhantomJS,
vueVersion < 2.5,
'mounts component scoped slots',
() => {
const wrapper = mountingMethod(ComponentWithScopedSlots, {
slots: { default: '<span>123</span>' },
scopedSlots: {
destructuring:
'<p slot-scope="{ index, item }">{{index}},{{item}}</p>',
list: '<p slot-scope="foo">{{foo.index}},{{foo.text}}</p>',
list: '<template slot-scope="foo"><p>{{foo.index}},{{foo.text}}</p></template>',
single: '<p slot-scope="bar">{{bar.text}}</p>',
noProps: '<p slot-scope="baz">baz</p>'
}
Expand Down Expand Up @@ -105,52 +104,40 @@ describeWithShallowAndMount('scopedSlots', mountingMethod => {
}
)

itDoNotRunIf(
vueVersion < 2.5 || isRunningPhantomJS,
'throws exception when it is seted to a template tag at top',
() => {
const fn = () => {
mountingMethod(ComponentWithScopedSlots, {
scopedSlots: {
single: '<template></template>'
}
})
it('handles JSX', () => {
const wrapper = mountingMethod({
template: '<div><slot name="single" :text="foo"></slot></div>',
data: () => ({
foo: 'bar'
})
}, {
scopedSlots: {
single ({ text }) {
return <p>{{ text }}</p>
}
}
const message =
'[vue-test-utils]: the scopedSlots option does not support a template tag as the root element.'
expect(fn)
.to.throw()
.with.property('message', message)
}
)
})
expect(wrapper.html()).to.equal('<div><p>bar</p></div>')
})

itDoNotRunIf(
vueVersion >= 2.5 || isRunningPhantomJS,
'throws exception when vue version < 2.5',
() => {
const fn = () => {
mountingMethod(ComponentWithScopedSlots, {
scopedSlots: {
list: '<p slot-scope="foo">{{foo.index}},{{foo.text}}</p>'
}
})
it('handles no slot-scope', () => {
const wrapper = mountingMethod({
template: '<div><slot name="single" :text="foo" :i="123"></slot></div>',
data: () => ({
foo: 'bar'
})
}, {
scopedSlots: {
single: '<p>{{text}},{{i}}</p>'
}
const message =
'[vue-test-utils]: the scopedSlots option is only supported in [email protected]+.'
expect(fn)
.to.throw()
.with.property('message', message)
}
)
})
expect(wrapper.html()).to.equal('<div><p>bar,123</p></div>')
})

itDoNotRunIf(
vueVersion < 2.5,
'throws exception when using PhantomJS',
vueVersion > 2.0,
'throws exception when vue version < 2.1',
() => {
if (window.navigator.userAgent.match(/Chrome|PhantomJS/i)) {
return
}
window = { navigator: { userAgent: 'PhantomJS' }} // eslint-disable-line no-native-reassign
const fn = () => {
mountingMethod(ComponentWithScopedSlots, {
scopedSlots: {
Expand All @@ -159,7 +146,7 @@ describeWithShallowAndMount('scopedSlots', mountingMethod => {
})
}
const message =
'[vue-test-utils]: the scopedSlots option does not support PhantomJS. Please use Puppeteer, or pass a component.'
'[vue-test-utils]: the scopedSlots option is only supported in [email protected]+.'
expect(fn)
.to.throw()
.with.property('message', message)
Expand Down
6 changes: 6 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1790,6 +1790,12 @@ babel-plugin-transform-strict-mode@^6.24.1:
babel-runtime "^6.22.0"
babel-types "^6.24.1"

babel-plugin-transform-vue-jsx@^3.7.0:
version "3.7.0"
resolved "https://registry.yarnpkg.com/babel-plugin-transform-vue-jsx/-/babel-plugin-transform-vue-jsx-3.7.0.tgz#d40492e6692a36b594f7e9a1928f43e969740960"
dependencies:
esutils "^2.0.2"

babel-plugin-transform-vue-jsx@^4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/babel-plugin-transform-vue-jsx/-/babel-plugin-transform-vue-jsx-4.0.1.tgz#2c8bddce87a6ef09eaa59869ff1bfbeeafc5f88d"
Expand Down