-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
- Loading branch information
1 parent
87ea324
commit 4b34e4a
Showing
5 changed files
with
81 additions
and
33 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,51 @@ | ||
import { defineComponent, h, ref } from 'vue-demi' | ||
import { type Ref, defineComponent, h, nextTick, ref } from 'vue-demi' | ||
import { createInjectionState } from '@vueuse/shared' | ||
import { describe, expect, it } from 'vitest' | ||
import { mount } from '../../.test' | ||
import { mount, useSetup } from '../../.test' | ||
|
||
const [useProvideCountState, useCountState] = createInjectionState((initialValue: number) => { | ||
const count = ref(initialValue) | ||
return count | ||
}) | ||
|
||
const ChildComponent = defineComponent({ | ||
setup() { | ||
const count = useCountState() | ||
expect(count?.value).toBe(0) | ||
describe('createInjectionState simple example', () => { | ||
it('should work', async () => { | ||
let count: Ref<number> | undefined | ||
|
||
return () => h('div') | ||
}, | ||
}) | ||
const ChildComponent = defineComponent({ | ||
setup() { | ||
count = useCountState() | ||
|
||
const RootComponent = defineComponent({ | ||
setup() { | ||
useProvideCountState(0) | ||
return () => h('div') | ||
}, | ||
}) | ||
|
||
return () => h(ChildComponent) | ||
}, | ||
}) | ||
const RootComponent = defineComponent({ | ||
setup() { | ||
useProvideCountState(114514) | ||
|
||
describe('createInjectionState simple example', () => { | ||
it('should work', () => { | ||
mount(RootComponent) | ||
}) | ||
}) | ||
return () => h(ChildComponent) | ||
}, | ||
}) | ||
|
||
const CanProvidingStateAndInjectedStateInSameComponent = defineComponent({ | ||
setup() { | ||
useProvideCountState(114514) | ||
const count = useCountState()! | ||
expect(count.value).toBe(114514) | ||
const vm = mount(RootComponent) | ||
await nextTick() | ||
|
||
return () => h('div') | ||
}, | ||
expect(count?.value).toBe(114514) | ||
vm.unmount() | ||
}) | ||
}) | ||
|
||
describe('allow call useProvidingState and useInjectedState in same component', () => { | ||
it('should work', () => { | ||
mount(CanProvidingStateAndInjectedStateInSameComponent) | ||
it('should work', async () => { | ||
const vm = useSetup(() => { | ||
useProvideCountState(114514) | ||
const count = useCountState()! | ||
|
||
return { count } | ||
}) | ||
await nextTick() | ||
expect(vm.count).toBe(114514) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { type InjectionKey, defineComponent, 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 cross component example', () => { | ||
it('should work', 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() | ||
}) | ||
}) | ||
|
||
describe('provideLocal injectLocal same component example', () => { | ||
it('should work', 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>>() |