-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: vue 2 support for provideLocal and injectLocal (#3464)
- Loading branch information
1 parent
90d3400
commit cf75702
Showing
5 changed files
with
112 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { type InjectionKey, defineComponent, getCurrentInstance, h, nextTick } from 'vue-demi' | ||
import { injectLocal, provideLocal } from '@vueuse/shared' | ||
import { describe, expect, it } from 'vitest' | ||
import { mount, useSetup } from '../../.test' | ||
|
||
describe('provideLocal injectLocal deps', () => { | ||
it('depends on getCurrentInstance().proxy === getCurrentInstance().proxy', async () => { | ||
const vm = useSetup(() => { | ||
const instance1 = getCurrentInstance() | ||
const instance2 = getCurrentInstance() | ||
const instanceProxyStable = instance1?.proxy === instance2?.proxy | ||
return { instance1, instance2, instanceProxyStable } | ||
}) | ||
await nextTick() | ||
expect(vm.instance1).not.toBeNull() | ||
expect(vm.instance2).not.toBeNull() | ||
expect(vm.instance1).toBeTypeOf('object') | ||
expect(vm.instance2).toBeTypeOf('object') | ||
expect(vm.instanceProxyStable).toBe(true) | ||
vm.unmount() | ||
}) | ||
}) | ||
|
||
describe('provideLocal injectLocal', () => { | ||
it('should work for nested component', async () => { | ||
const CountKey: InjectionKey<number> | string = Symbol('count') | ||
let count: number | undefined | ||
const ChildComponent = defineComponent({ | ||
setup() { | ||
count = injectLocal(CountKey) | ||
|
||
return () => h('div') | ||
}, | ||
}) | ||
|
||
const RootComponent = defineComponent({ | ||
setup() { | ||
provideLocal(CountKey, 2333) | ||
|
||
return () => h(ChildComponent) | ||
}, | ||
}) | ||
const vm = mount(RootComponent) | ||
await nextTick() | ||
|
||
expect(count).toBe(2333) | ||
vm.unmount() | ||
}) | ||
|
||
it('should work for same component', async () => { | ||
const CountKey: InjectionKey<number> | string = Symbol('count') | ||
const vm = useSetup(() => { | ||
provideLocal(CountKey, 2333) | ||
const count = injectLocal(CountKey)! | ||
return { count } | ||
}) | ||
await nextTick() | ||
expect(vm.count).toBe(2333) | ||
vm.unmount() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
import type { getCurrentInstance } from 'vue-demi' | ||
|
||
export const localProvidedStateMap = new WeakMap<NonNullable<ReturnType<typeof getCurrentInstance>>, Record<string | symbol, any>>() | ||
export const localProvidedStateMap = new WeakMap<NonNullable<NonNullable<ReturnType<typeof getCurrentInstance>>['proxy']>, Record<string | symbol, any>>() |