| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- 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: `<child/>`,
- provide: {
- foo: 1,
- bar: false
- },
- components: {
- child: {
- template: `<injected-comp/>`,
- components: {
- injectedComp
- }
- }
- }
- }).$mount()
- expect(injected).toEqual([1, false])
- })
- it('should use closest parent', () => {
- new Vue({
- template: `<child/>`,
- provide: {
- foo: 1,
- bar: null
- },
- components: {
- child: {
- provide: {
- foo: 3
- },
- template: `<injected-comp/>`,
- components: {
- injectedComp
- }
- }
- }
- }).$mount()
- expect(injected).toEqual([3, null])
- })
- it('provide function', () => {
- new Vue({
- template: `<child/>`,
- data: {
- a: 1,
- b: false
- },
- provide () {
- return {
- foo: this.a,
- bar: this.b
- }
- },
- components: {
- child: {
- template: `<injected-comp/>`,
- 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: `<child/>`,
- provide: {
- foo: false,
- bar: 2
- },
- components: {
- child: {
- template: `<inject-alias/>`,
- components: {
- injectAlias
- }
- }
- }
- }).$mount()
- expect(injected).toEqual([false, 2])
- })
- it('inject before resolving data/props', () => {
- const vm = new Vue({
- provide: {
- foo: 1
- }
- })
- const child = new Vue({
- parent: vm,
- inject: ['foo'],
- data () {
- return {
- bar: this.foo + 1
- }
- },
- props: {
- baz: {
- default () {
- return this.foo + 2
- }
- }
- }
- })
- expect(child.foo).toBe(1)
- expect(child.bar).toBe(2)
- expect(child.baz).toBe(3)
- })
- 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')
- })
- }
- // 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(
- `Avoid mutating a injections directly since the value will be ` +
- `overwritten whenever the provided component re-renders. ` +
- `injections being mutated: "${key}"`).toHaveBeenWarned()
- })
- })
|