Skip to content

Commit

Permalink
test: more test cases for $slot usage detection
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jan 10, 2019
1 parent 1e5174d commit d855027
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/compiler/parser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ function childrenHas$Slot (el): boolean {
return el.children ? el.children.some(nodeHas$Slot) : false
}

const $slotRE = /\$slot/
const $slotRE = /(^|[^\w_$])\$slot($|[^\w_$])/
function nodeHas$Slot (node): boolean {
// caching
if (hasOwn(node, 'has$Slot')) {
Expand Down
27 changes: 25 additions & 2 deletions test/unit/features/component/component-scoped-slot.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -690,10 +690,33 @@ describe('Component scoped slot', () => {
expect(vm.$el.innerHTML).toBe(`default<div>default</div><div>static</div>`)
})

// testing $slot detection: bracket access, using $slot alone, passing as arguments...
it('should work for alternative $slot usage', () => {
const vm = new Vue({
template: `<foo>{{ $slot['foo'] }}<div slot="foo">{{ $slot }}</div><div>{{ pick($slot) }}</div></foo>`,
methods: { pick: s => s.foo },
components: { foo: { template: `<div><slot foo="default"/><slot name="foo"/></div>` }}
}).$mount()
expect(vm.$el.innerHTML).toBe(`default<div>default</div><div>{}</div>`)
})

// should not consider detection if $slot is inside longer valid identifier
it('should not break when template expression uses $slots', () => {
const vm = new Vue({
template: ``
})
data: { some$slot: 123 },
template: `<foo>{{ some$slot }}<div slot="foo">{{ $slots }}</div></foo>`,
components: {
foo: {
render(h) {
// should be compiled as normal slots
expect(this.$slots.default).toBeTruthy()
expect(this.$slots.foo).toBeTruthy()
return h('div', [this.$slots.default, this.$slots.foo])
}
}
}
}).$mount()
expect(vm.$el.innerHTML).toBe(`123<div>{}</div>`)
})
})
})

0 comments on commit d855027

Please sign in to comment.