2017-02-21 05:58:24 +08:00
|
|
|
import Vue from 'vue'
|
2017-02-26 06:13:41 +08:00
|
|
|
import { isNative } from 'core/util/env'
|
2017-02-21 05:58:24 +08:00
|
|
|
|
|
|
|
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: `<child/>`,
|
|
|
|
provide: {
|
|
|
|
foo: 1,
|
2017-03-02 00:01:24 +08:00
|
|
|
bar: false
|
2017-02-21 05:58:24 +08:00
|
|
|
},
|
|
|
|
components: {
|
|
|
|
child: {
|
|
|
|
template: `<injected-comp/>`,
|
|
|
|
components: {
|
|
|
|
injectedComp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).$mount()
|
|
|
|
|
2017-03-02 00:01:24 +08:00
|
|
|
expect(injected).toEqual([1, false])
|
2017-02-21 05:58:24 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should use closest parent', () => {
|
|
|
|
new Vue({
|
|
|
|
template: `<child/>`,
|
|
|
|
provide: {
|
|
|
|
foo: 1,
|
2017-03-02 00:01:24 +08:00
|
|
|
bar: null
|
2017-02-21 05:58:24 +08:00
|
|
|
},
|
|
|
|
components: {
|
|
|
|
child: {
|
|
|
|
provide: {
|
|
|
|
foo: 3
|
|
|
|
},
|
|
|
|
template: `<injected-comp/>`,
|
|
|
|
components: {
|
|
|
|
injectedComp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).$mount()
|
|
|
|
|
2017-03-02 00:01:24 +08:00
|
|
|
expect(injected).toEqual([3, null])
|
2017-02-21 05:58:24 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it('provide function', () => {
|
|
|
|
new Vue({
|
|
|
|
template: `<child/>`,
|
|
|
|
data: {
|
|
|
|
a: 1,
|
2017-03-02 00:01:24 +08:00
|
|
|
b: false
|
2017-02-21 05:58:24 +08:00
|
|
|
},
|
|
|
|
provide () {
|
|
|
|
return {
|
|
|
|
foo: this.a,
|
|
|
|
bar: this.b
|
|
|
|
}
|
|
|
|
},
|
|
|
|
components: {
|
|
|
|
child: {
|
|
|
|
template: `<injected-comp/>`,
|
|
|
|
components: {
|
|
|
|
injectedComp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).$mount()
|
|
|
|
|
2017-03-02 00:01:24 +08:00
|
|
|
expect(injected).toEqual([1, false])
|
2017-02-21 05:58:24 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it('inject with alias', () => {
|
|
|
|
const injectAlias = {
|
|
|
|
inject: {
|
|
|
|
baz: 'foo',
|
|
|
|
qux: 'bar'
|
|
|
|
},
|
|
|
|
render () {},
|
|
|
|
created () {
|
|
|
|
injected = [this.baz, this.qux]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
new Vue({
|
|
|
|
template: `<child/>`,
|
|
|
|
provide: {
|
2017-03-02 00:01:24 +08:00
|
|
|
foo: false,
|
2017-02-21 05:58:24 +08:00
|
|
|
bar: 2
|
|
|
|
},
|
|
|
|
components: {
|
|
|
|
child: {
|
|
|
|
template: `<inject-alias/>`,
|
|
|
|
components: {
|
|
|
|
injectAlias
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).$mount()
|
|
|
|
|
2017-03-02 00:01:24 +08:00
|
|
|
expect(injected).toEqual([false, 2])
|
2017-02-21 05:58:24 +08:00
|
|
|
})
|
|
|
|
|
2017-03-03 11:49:10 +08:00
|
|
|
it('inject before resolving data/props', () => {
|
2017-02-21 05:58:24 +08:00
|
|
|
const vm = new Vue({
|
|
|
|
provide: {
|
|
|
|
foo: 1
|
2017-03-03 11:49:10 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const child = new Vue({
|
|
|
|
parent: vm,
|
|
|
|
inject: ['foo'],
|
|
|
|
data () {
|
|
|
|
return {
|
|
|
|
bar: this.foo + 1
|
|
|
|
}
|
2017-02-21 05:58:24 +08:00
|
|
|
},
|
2017-03-03 11:49:10 +08:00
|
|
|
props: {
|
|
|
|
baz: {
|
|
|
|
default () {
|
|
|
|
return this.foo + 2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-02-21 05:58:24 +08:00
|
|
|
})
|
|
|
|
|
2017-03-03 11:49:10 +08:00
|
|
|
expect(child.foo).toBe(1)
|
|
|
|
expect(child.bar).toBe(2)
|
|
|
|
expect(child.baz).toBe(3)
|
2017-02-21 05:58:24 +08:00
|
|
|
})
|
2017-02-26 06:13:41 +08:00
|
|
|
|
2017-04-05 14:36:15 +08:00
|
|
|
// Github issue #5194
|
|
|
|
it('should work with functional', () => {
|
|
|
|
new Vue({
|
|
|
|
template: `<child/>`,
|
|
|
|
provide: {
|
|
|
|
foo: 1,
|
|
|
|
bar: false
|
|
|
|
},
|
|
|
|
components: {
|
|
|
|
child: {
|
|
|
|
functional: true,
|
|
|
|
inject: ['foo', 'bar'],
|
|
|
|
render (h, context) {
|
|
|
|
const { injections } = context
|
|
|
|
injected = [injections.foo, injections.bar]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).$mount()
|
|
|
|
|
|
|
|
expect(injected).toEqual([1, false])
|
|
|
|
})
|
|
|
|
|
2017-02-26 06:13:41 +08:00
|
|
|
if (typeof Reflect !== 'undefined' && isNative(Reflect.ownKeys)) {
|
|
|
|
it('with Symbol keys', () => {
|
|
|
|
const s = Symbol()
|
|
|
|
const vm = new Vue({
|
|
|
|
template: `<child/>`,
|
|
|
|
provide: {
|
|
|
|
[s]: 123
|
|
|
|
},
|
|
|
|
components: {
|
|
|
|
child: {
|
|
|
|
inject: { s },
|
|
|
|
template: `<div>{{ s }}</div>`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).$mount()
|
|
|
|
expect(vm.$el.textContent).toBe('123')
|
|
|
|
})
|
|
|
|
}
|
2017-03-21 15:22:32 +08:00
|
|
|
|
|
|
|
// Github issue #5223
|
|
|
|
it('should work with reactive array', done => {
|
|
|
|
const vm = new Vue({
|
|
|
|
template: `<div><child></child></div>`,
|
|
|
|
data () {
|
|
|
|
return {
|
|
|
|
foo: []
|
|
|
|
}
|
|
|
|
},
|
|
|
|
provide () {
|
|
|
|
return {
|
|
|
|
foo: this.foo
|
|
|
|
}
|
|
|
|
},
|
|
|
|
components: {
|
|
|
|
child: {
|
|
|
|
inject: ['foo'],
|
|
|
|
template: `<span>{{foo.length}}</span>`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).$mount()
|
|
|
|
|
|
|
|
expect(vm.$el.innerHTML).toEqual(`<span>0</span>`)
|
|
|
|
vm.foo.push(vm.foo.length)
|
|
|
|
vm.$nextTick(() => {
|
|
|
|
expect(vm.$el.innerHTML).toEqual(`<span>1</span>`)
|
|
|
|
vm.foo.pop()
|
|
|
|
vm.$nextTick(() => {
|
|
|
|
expect(vm.$el.innerHTML).toEqual(`<span>0</span>`)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should warn when injections has been modified', () => {
|
|
|
|
const key = 'foo'
|
|
|
|
const vm = new Vue({
|
|
|
|
provide: {
|
|
|
|
foo: 1
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const child = new Vue({
|
|
|
|
parent: vm,
|
|
|
|
inject: ['foo']
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(child.foo).toBe(1)
|
|
|
|
child.foo = 2
|
|
|
|
expect(
|
2017-03-21 20:26:22 +08:00
|
|
|
`Avoid mutating an injected value directly since the changes will be ` +
|
2017-03-21 15:22:32 +08:00
|
|
|
`overwritten whenever the provided component re-renders. ` +
|
2017-03-21 20:26:22 +08:00
|
|
|
`injection being mutated: "${key}"`).toHaveBeenWarned()
|
2017-03-21 15:22:32 +08:00
|
|
|
})
|
2017-02-21 05:58:24 +08:00
|
|
|
})
|