Skip to content

Commit 790a949

Browse files
38elementseddyerburgh
authored andcommitted
feat(slots): enable passing text to slots (#233)
* Passing text to slots * Add VNode * Fix message * Use vueVersion * Change vueVersion * Change vueVersion * Add vueVersion * Improve test
1 parent 5da6821 commit 790a949

File tree

4 files changed

+48
-16
lines changed

4 files changed

+48
-16
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,7 @@ _book
2121
.tmp
2222
tmp*
2323

24+
# Vim
25+
*.sw[po]
26+
2427
yarn.lock

src/lib/add-slots.js

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// @flow
22

3+
import Vue from 'vue'
34
import { compileToFunctions } from 'vue-template-compiler'
45
import { throwError } from './util'
56

@@ -8,24 +9,28 @@ function isValidSlot (slot: any): boolean {
89
}
910

1011
function addSlotToVm (vm: Component, slotName: string, slotValue: Component | string | Array<Component> | Array<string>): void {
11-
if (Array.isArray(vm.$slots[slotName])) {
12-
if (typeof slotValue === 'string') {
13-
if (!compileToFunctions) {
14-
throwError('vueTemplateCompiler is undefined, you must pass components explicitly if vue-template-compiler is undefined')
15-
}
16-
vm.$slots[slotName].push(vm.$createElement(compileToFunctions(slotValue)))
17-
} else {
18-
vm.$slots[slotName].push(vm.$createElement(slotValue))
12+
let elem
13+
const vueVersion = Number(`${Vue.version.split('.')[0]}.${Vue.version.split('.')[1]}`)
14+
if (typeof slotValue === 'string') {
15+
if (!compileToFunctions) {
16+
throwError('vueTemplateCompiler is undefined, you must pass components explicitly if vue-template-compiler is undefined')
1917
}
20-
} else {
21-
if (typeof slotValue === 'string') {
22-
if (!compileToFunctions) {
23-
throwError('vueTemplateCompiler is undefined, you must pass components explicitly if vue-template-compiler is undefined')
24-
}
25-
vm.$slots[slotName] = [vm.$createElement(compileToFunctions(slotValue))]
18+
if (slotValue.trim()[0] === '<') {
19+
elem = vm.$createElement(compileToFunctions(slotValue))
2620
} else {
27-
vm.$slots[slotName] = [vm.$createElement(slotValue)] // eslint-disable-line no-param-reassign
21+
if (vueVersion >= 2.2) {
22+
elem = vm._v(slotValue)
23+
} else {
24+
throwError('vue-test-utils support for passing text to slots at [email protected]+')
25+
}
2826
}
27+
} else {
28+
elem = vm.$createElement(slotValue)
29+
}
30+
if (Array.isArray(vm.$slots[slotName])) {
31+
vm.$slots[slotName].push(elem)
32+
} else {
33+
vm.$slots[slotName] = [elem]
2934
}
3035
}
3136

test/unit/specs/mount.spec.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Vue from 'vue'
22
import { compileToFunctions } from 'vue-template-compiler'
33
import mount from '~src/mount'
4+
import Component from '~resources/components/component.vue'
45
import ComponentWithProps from '~resources/components/component-with-props.vue'
56
import ComponentWithMixin from '~resources/components/component-with-mixin.vue'
67
import createLocalVue from '~src/create-local-vue'
@@ -101,7 +102,7 @@ describe('mount', () => {
101102
'prop': 'val'
102103
},
103104
slots: {
104-
'prop': 'val'
105+
'prop': Component
105106
},
106107
localVue: createLocalVue(),
107108
stubs: {

test/unit/specs/mount/options/slots.spec.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { compileToFunctions } from 'vue-template-compiler'
22
import mount from '~src/mount'
33
import Component from '~resources/components/component.vue'
44
import ComponentWithSlots from '~resources/components/component-with-slots.vue'
5+
import { vueVersion } from '~resources/test-utils'
56

67
describe('mount.slots', () => {
78
it('mounts component with default slot if passed component in slot object', () => {
@@ -25,6 +26,17 @@ describe('mount.slots', () => {
2526
expect(wrapper.contains('span')).to.equal(true)
2627
})
2728

29+
it('mounts component with default slot if passed string in slot object', () => {
30+
if (vueVersion >= 2.2) {
31+
const wrapper = mount(ComponentWithSlots, { slots: { default: 'foo' }})
32+
expect(wrapper.find('main').text()).to.equal('foo')
33+
} else {
34+
const message = '[vue-test-utils]: vue-test-utils support for passing text to slots at [email protected]+'
35+
const fn = () => mount(ComponentWithSlots, { slots: { default: 'foo' }})
36+
expect(fn).to.throw().with.property('message', message)
37+
}
38+
})
39+
2840
it('throws error if passed string in default slot object and vue-template-compiler is undefined', () => {
2941
const compilerSave = require.cache[require.resolve('vue-template-compiler')].exports.compileToFunctions
3042
require.cache[require.resolve('vue-template-compiler')].exports.compileToFunctions = undefined
@@ -46,6 +58,17 @@ describe('mount.slots', () => {
4658
expect(wrapper.contains('span')).to.equal(true)
4759
})
4860

61+
it('mounts component with default slot if passed string in slot text array object', () => {
62+
if (vueVersion >= 2.2) {
63+
const wrapper = mount(ComponentWithSlots, { slots: { default: ['foo', 'bar'] }})
64+
expect(wrapper.find('main').text()).to.equal('foobar')
65+
} else {
66+
const message = '[vue-test-utils]: vue-test-utils support for passing text to slots at [email protected]+'
67+
const fn = () => mount(ComponentWithSlots, { slots: { default: ['foo', 'bar'] }})
68+
expect(fn).to.throw().with.property('message', message)
69+
}
70+
})
71+
4972
it('throws error if passed string in default slot array vue-template-compiler is undefined', () => {
5073
const compilerSave = require.cache[require.resolve('vue-template-compiler')].exports.compileToFunctions
5174
require.cache[require.resolve('vue-template-compiler')].exports.compileToFunctions = undefined

0 commit comments

Comments
 (0)