Browse Source

fix(provide/inject): merge provide properly from mixins

close #6175
dev
Evan You 7 years ago
parent
commit
303655116f
  1. 2
      src/core/util/options.js
  2. 41
      test/unit/features/options/inject.spec.js

2
src/core/util/options.js

@ -85,7 +85,7 @@ export function mergeDataOrFn (
return function mergedDataFn () { return function mergedDataFn () {
return mergeData( return mergeData(
typeof childVal === 'function' ? childVal.call(this) : childVal, typeof childVal === 'function' ? childVal.call(this) : childVal,
parentVal.call(this) typeof parentVal === 'function' ? parentVal.call(this) : parentVal
) )
} }
} else if (parentVal || childVal) { } else if (parentVal || childVal) {

41
test/unit/features/options/inject.spec.js

@ -382,6 +382,7 @@ describe('Options provide/inject', () => {
expect(injected).toEqual(['foo', 'bar']) expect(injected).toEqual(['foo', 'bar'])
}) })
it('should merge provide from mixins (functions)', () => { it('should merge provide from mixins (functions)', () => {
const mixinA = { provide: () => ({ foo: 'foo' }) } const mixinA = { provide: () => ({ foo: 'foo' }) }
const mixinB = { provide: () => ({ bar: 'bar' }) } const mixinB = { provide: () => ({ bar: 'bar' }) }
@ -401,6 +402,7 @@ describe('Options provide/inject', () => {
expect(injected).toEqual(['foo', 'bar']) expect(injected).toEqual(['foo', 'bar'])
}) })
it('should merge provide from mixins (mix of objects and functions)', () => { it('should merge provide from mixins (mix of objects and functions)', () => {
const mixinA = { provide: { foo: 'foo' }} const mixinA = { provide: { foo: 'foo' }}
const mixinB = { provide: () => ({ bar: 'bar' }) } const mixinB = { provide: () => ({ bar: 'bar' }) }
@ -422,6 +424,7 @@ describe('Options provide/inject', () => {
expect(injected).toEqual(['foo', 'bar', 'baz', 'bam']) expect(injected).toEqual(['foo', 'bar', 'baz', 'bam'])
}) })
it('should merge provide from mixins and override existing keys', () => { it('should merge provide from mixins and override existing keys', () => {
const mixinA = { provide: { foo: 'foo' }} const mixinA = { provide: { foo: 'foo' }}
const mixinB = { provide: { foo: 'bar' }} const mixinB = { provide: { foo: 'bar' }}
@ -441,6 +444,7 @@ describe('Options provide/inject', () => {
expect(injected).toEqual(['bar']) expect(injected).toEqual(['bar'])
}) })
it('should merge provide when Vue.extend', () => { it('should merge provide when Vue.extend', () => {
const mixinA = { provide: () => ({ foo: 'foo' }) } const mixinA = { provide: () => ({ foo: 'foo' }) }
const child = { const child = {
@ -504,4 +508,41 @@ describe('Options provide/inject', () => {
expect(isObserver(child.bar)).toBe(false) expect(isObserver(child.bar)).toBe(false)
expect(isObserver(child.baz)).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`)
})
}) })

Loading…
Cancel
Save