import Vue from 'vue'
import { isNative } from 'core/util/env'
describe('Options provide/inject', () => {
let injected
const injectedComp = {
inject: ['foo', 'bar'],
render () {},
created () {
injected = [this.foo, this.bar]
}
}
beforeEach(() => {
injected = null
})
it('should work', () => {
new Vue({
template: ``,
provide: {
foo: 1,
bar: false
},
components: {
child: {
template: ``,
components: {
injectedComp
}
}
}
}).$mount()
expect(injected).toEqual([1, false])
})
it('should use closest parent', () => {
new Vue({
template: ``,
provide: {
foo: 1,
bar: null
},
components: {
child: {
provide: {
foo: 3
},
template: ``,
components: {
injectedComp
}
}
}
}).$mount()
expect(injected).toEqual([3, null])
})
it('provide function', () => {
new Vue({
template: ``,
data: {
a: 1,
b: false
},
provide () {
return {
foo: this.a,
bar: this.b
}
},
components: {
child: {
template: ``,
components: {
injectedComp
}
}
}
}).$mount()
expect(injected).toEqual([1, false])
})
it('inject with alias', () => {
const injectAlias = {
inject: {
baz: 'foo',
qux: 'bar'
},
render () {},
created () {
injected = [this.baz, this.qux]
}
}
new Vue({
template: ``,
provide: {
foo: false,
bar: 2
},
components: {
child: {
template: ``,
components: {
injectAlias
}
}
}
}).$mount()
expect(injected).toEqual([false, 2])
})
it('self-inject', () => {
const vm = new Vue({
provide: {
foo: 1
},
inject: ['foo']
})
expect(vm.foo).toBe(1)
})
if (typeof Reflect !== 'undefined' && isNative(Reflect.ownKeys)) {
it('with Symbol keys', () => {
const s = Symbol()
const vm = new Vue({
template: ``,
provide: {
[s]: 123
},
components: {
child: {
inject: { s },
template: `
{{ s }}
`
}
}
}).$mount()
expect(vm.$el.textContent).toBe('123')
})
}
})