| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336 |
- import Vue from 'vue'
- import {
- h,
- provide,
- inject,
- InjectionKey,
- ref,
- nextTick,
- Ref,
- readonly,
- reactive
- } from 'v3/index'
- // reference: https://vue-composition-api-rfc.netlify.com/api.html#provide-inject
- describe('api: provide/inject', () => {
- it('string keys', () => {
- const Provider = {
- setup() {
- provide('foo', 1)
- return () => h(Middle)
- }
- }
- const Middle = {
- render: () => h(Consumer)
- }
- const Consumer = {
- setup() {
- const foo = inject('foo')
- return () => h('div', foo)
- }
- }
- const vm = new Vue(Provider).$mount()
- expect(vm.$el.outerHTML).toBe(`<div>1</div>`)
- })
- it('symbol keys', () => {
- // also verifies InjectionKey type sync
- const key: InjectionKey<number> = Symbol()
- const Provider = {
- setup() {
- provide(key, 1)
- return () => h(Middle)
- }
- }
- const Middle = {
- render: () => h(Consumer)
- }
- const Consumer = {
- setup() {
- const foo = inject(key) || 1
- return () => h('div', foo + 1)
- }
- }
- const vm = new Vue(Provider).$mount()
- expect(vm.$el.outerHTML).toBe(`<div>2</div>`)
- })
- it('default values', () => {
- const Provider = {
- setup() {
- provide('foo', 'foo')
- return () => h(Middle)
- }
- }
- const Middle = {
- render: () => h(Consumer)
- }
- const Consumer = {
- setup() {
- // default value should be ignored if value is provided
- const foo = inject('foo', 'fooDefault')
- // default value should be used if value is not provided
- const bar = inject('bar', 'bar')
- return () => h('div', foo + bar)
- }
- }
- const vm = new Vue(Provider).$mount()
- expect(vm.$el.outerHTML).toBe(`<div>foobar</div>`)
- })
- it('bound to instance', () => {
- const Provider = {
- setup() {
- return () => h(Consumer)
- }
- }
- const Consumer = {
- name: 'Consumer',
- inject: {
- foo: {
- from: 'foo',
- default() {
- return this!.$options.name
- }
- }
- },
- render() {
- // @ts-ignore
- return h('div', this.foo)
- }
- }
- const vm = new Vue(Provider).$mount()
- expect(vm.$el.outerHTML).toBe(`<div>Consumer</div>`)
- })
- it('nested providers', () => {
- const ProviderOne = {
- setup() {
- provide('foo', 'foo')
- provide('bar', 'bar')
- return () => h(ProviderTwo)
- }
- }
- const ProviderTwo = {
- setup() {
- // override parent value
- provide('foo', 'fooOverride')
- provide('baz', 'baz')
- return () => h(Consumer)
- }
- }
- const Consumer = {
- setup() {
- const foo = inject('foo')
- const bar = inject('bar')
- const baz = inject('baz')
- return () => h('div', [foo, bar, baz].join(','))
- }
- }
- const vm = new Vue(ProviderOne).$mount()
- expect(vm.$el.outerHTML).toBe(`<div>fooOverride,bar,baz</div>`)
- })
- it('reactivity with refs', async () => {
- const count = ref(1)
- const Provider = {
- setup() {
- provide('count', count)
- return () => h(Middle)
- }
- }
- const Middle = {
- render: () => h(Consumer)
- }
- const Consumer = {
- setup() {
- const count = inject<Ref<number>>('count')!
- return () => h('div', count.value)
- }
- }
- const vm = new Vue(Provider).$mount()
- expect(vm.$el.outerHTML).toBe(`<div>1</div>`)
- count.value++
- await nextTick()
- expect(vm.$el.outerHTML).toBe(`<div>2</div>`)
- })
- it('reactivity with readonly refs', async () => {
- const count = ref(1)
- const Provider = {
- setup() {
- provide('count', readonly(count))
- return () => h(Middle)
- }
- }
- const Middle = {
- render: () => h(Consumer)
- }
- const Consumer = {
- setup() {
- const count = inject<Ref<number>>('count')!
- // should not work
- count.value++
- return () => h('div', count.value)
- }
- }
- const vm = new Vue(Provider).$mount()
- expect(vm.$el.outerHTML).toBe(`<div>1</div>`)
- expect(
- `Set operation on key "value" failed: target is readonly`
- ).toHaveBeenWarned()
- // source mutation should still work
- count.value++
- await nextTick()
- expect(vm.$el.outerHTML).toBe(`<div>2</div>`)
- })
- it('reactivity with objects', async () => {
- const rootState = reactive({ count: 1 })
- const Provider = {
- setup() {
- provide('state', rootState)
- return () => h(Middle)
- }
- }
- const Middle = {
- render: () => h(Consumer)
- }
- const Consumer = {
- setup() {
- const state = inject<typeof rootState>('state')!
- return () => h('div', state.count)
- }
- }
- const vm = new Vue(Provider).$mount()
- expect(vm.$el.outerHTML).toBe(`<div>1</div>`)
- rootState.count++
- await nextTick()
- expect(vm.$el.outerHTML).toBe(`<div>2</div>`)
- })
- it('reactivity with readonly objects', async () => {
- const rootState = reactive({ count: 1 })
- const Provider = {
- setup() {
- provide('state', readonly(rootState))
- return () => h(Middle)
- }
- }
- const Middle = {
- render: () => h(Consumer)
- }
- const Consumer = {
- setup() {
- const state = inject<typeof rootState>('state')!
- // should not work
- state.count++
- return () => h('div', state.count)
- }
- }
- const vm = new Vue(Provider).$mount()
- expect(vm.$el.outerHTML).toBe(`<div>1</div>`)
- expect(
- `Set operation on key "count" failed: target is readonly`
- ).toHaveBeenWarned()
- rootState.count++
- await nextTick()
- expect(vm.$el.outerHTML).toBe(`<div>2</div>`)
- })
- it('should warn unfound', () => {
- const Provider = {
- setup() {
- return () => h(Middle)
- }
- }
- const Middle = {
- render: () => h(Consumer)
- }
- const Consumer = {
- setup() {
- const foo = inject('foo')
- expect(foo).toBeUndefined()
- return () => h('div', foo)
- }
- }
- const vm = new Vue(Provider).$mount()
- expect(vm.$el.outerHTML).toBe(`<div></div>`)
- expect(`injection "foo" not found.`).toHaveBeenWarned()
- })
- it('should not warn when default value is undefined', () => {
- const Provider = {
- setup() {
- return () => h(Middle)
- }
- }
- const Middle = {
- render: () => h(Consumer)
- }
- const Consumer = {
- setup() {
- const foo = inject('foo', undefined)
- return () => h('div', foo)
- }
- }
- new Vue(Provider).$mount()
- expect(`injection "foo" not found.`).not.toHaveBeenWarned()
- })
- // #2400
- it('should not self-inject', () => {
- const Comp = {
- setup() {
- provide('foo', 'foo')
- const injection = inject('foo', null)
- return () => h('div', injection)
- }
- }
- expect(new Vue(Comp).$mount().$el.outerHTML).toBe(`<div></div>`)
- })
- })
|