Skip to content

Commit

Permalink
fix(provide/inject): merge provide properly from mixins
Browse files Browse the repository at this point in the history
close #6175
  • Loading branch information
yyx990803 committed Jul 21, 2017
1 parent fc3d7cd commit 3036551
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/core/util/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export function mergeDataOrFn (
return function mergedDataFn () {
return mergeData(
typeof childVal === 'function' ? childVal.call(this) : childVal,
parentVal.call(this)
typeof parentVal === 'function' ? parentVal.call(this) : parentVal
)
}
} else if (parentVal || childVal) {
Expand Down
41 changes: 41 additions & 0 deletions test/unit/features/options/inject.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ describe('Options provide/inject', () => {

expect(injected).toEqual(['foo', 'bar'])
})

it('should merge provide from mixins (functions)', () => {
const mixinA = { provide: () => ({ foo: 'foo' }) }
const mixinB = { provide: () => ({ bar: 'bar' }) }
Expand All @@ -401,6 +402,7 @@ describe('Options provide/inject', () => {

expect(injected).toEqual(['foo', 'bar'])
})

it('should merge provide from mixins (mix of objects and functions)', () => {
const mixinA = { provide: { foo: 'foo' }}
const mixinB = { provide: () => ({ bar: 'bar' }) }
Expand All @@ -422,6 +424,7 @@ describe('Options provide/inject', () => {

expect(injected).toEqual(['foo', 'bar', 'baz', 'bam'])
})

it('should merge provide from mixins and override existing keys', () => {
const mixinA = { provide: { foo: 'foo' }}
const mixinB = { provide: { foo: 'bar' }}
Expand All @@ -441,6 +444,7 @@ describe('Options provide/inject', () => {

expect(injected).toEqual(['bar'])
})

it('should merge provide when Vue.extend', () => {
const mixinA = { provide: () => ({ foo: 'foo' }) }
const child = {
Expand Down Expand Up @@ -504,4 +508,41 @@ describe('Options provide/inject', () => {
expect(isObserver(child.bar)).toBe(false)
expect(isObserver(child.baz)).toBe(false)
})

// #6175
it('merge provide properly from mixins', () => {
const ProvideFooMixin = {
provide: {
foo: 'foo injected'
}
}

const ProvideBarMixin = {
provide: {
bar: 'bar injected'
}
}

const Child = {
inject: ['foo', 'bar'],
render (h) {
return h('div', [`foo: ${this.foo}, `, `bar: ${this.bar}`])
}
}

const Parent = {
mixins: [ProvideFooMixin, ProvideBarMixin],
render (h) {
return h(Child)
}
}

const vm = new Vue({
render (h) {
return h(Parent)
}
}).$mount()

expect(vm.$el.textContent).toBe(`foo: foo injected, bar: bar injected`)
})
})

0 comments on commit 3036551

Please sign in to comment.