|
1 |
| -import { defineComponent, h, ref } from 'vue-demi' |
| 1 | +import { type InjectionKey, type Ref, defineComponent, h, inject, ref } from 'vue-demi' |
2 | 2 | import { createInjectionState } from '@vueuse/shared'
|
3 | 3 | import { describe, expect, it } from 'vitest'
|
4 | 4 | import { mount } from '../../.test'
|
5 | 5 |
|
6 |
| -const [useProvideCountState, useCountState] = createInjectionState((initialValue: number) => { |
7 |
| - const count = ref(initialValue) |
8 |
| - return count |
9 |
| -}) |
| 6 | +describe('createInjectionState', () => { |
| 7 | + it('should work 1', () => { |
| 8 | + const [useProvideCountState, useCountState] = createInjectionState((initialValue: number) => { |
| 9 | + const count = ref(initialValue) |
| 10 | + return count |
| 11 | + }) |
10 | 12 |
|
11 |
| -const ChildComponent = defineComponent({ |
12 |
| - setup() { |
13 |
| - const count = useCountState() |
14 |
| - expect(count?.value).toBe(0) |
| 13 | + const ChildComponent = defineComponent({ |
| 14 | + setup() { |
| 15 | + const count = useCountState() |
| 16 | + expect(count?.value).toBe(0) |
15 | 17 |
|
16 |
| - return () => h('div') |
17<
E7F5
/code> |
| - }, |
18 |
| -}) |
| 18 | + return () => h('div') |
| 19 | + }, |
| 20 | + }) |
19 | 21 |
|
20 |
| -const RootComponent = defineComponent({ |
21 |
| - setup() { |
22 |
| - useProvideCountState(0) |
| 22 | + const RootComponent = defineComponent({ |
| 23 | + setup() { |
| 24 | + useProvideCountState(0) |
23 | 25 |
|
24 |
| - return () => h(ChildComponent) |
25 |
| - }, |
26 |
| -}) |
| 26 | + return () => h(ChildComponent) |
| 27 | + }, |
| 28 | + }) |
27 | 29 |
|
28 |
| -describe('createInjectionState simple example', () => { |
29 |
| - it('should work', () => { |
30 | 30 | mount(RootComponent)
|
31 | 31 | })
|
32 |
| -}) |
33 | 32 |
|
34 |
| -const CanProvidingStateAndInjectedStateInSameComponent = defineComponent({ |
35 |
| - setup() { |
36 |
| - useProvideCountState(114514) |
37 |
| - const count = useCountState()! |
38 |
| - expect(count.value).toBe(114514) |
| 33 | + it('should work (custom key)', () => { |
| 34 | + const KEY: InjectionKey<Ref<number>> = Symbol('count-state') |
39 | 35 |
|
40 |
| - return () => h('div') |
41 |
| - }, |
42 |
| -}) |
| 36 | + const [useProvideCountState, useCountState] = createInjectionState((initialValue: number) => { |
| 37 | + const count = ref(initialValue) |
| 38 | + return count |
| 39 | + }, { injectionKey: KEY }) |
| 40 | + |
| 41 | + const ChildComponent = defineComponent({ |
| 42 | + setup() { |
| 43 | + const count = useCountState() |
| 44 | + expect(count?.value).toBe(0) |
| 45 | + const count2 = inject(KEY) |
| 46 | + expect(count2?.value).toBe(0) |
| 47 | + |
| 48 | + return () => h('div') |
| 49 | + }, |
| 50 | + }) |
| 51 | + |
| 52 | + const RootComponent = defineComponent({ |
| 53 | + setup() { |
| 54 | + useProvideCountState(0) |
| 55 | + |
| 56 | + return () => h(ChildComponent) |
| 57 | + }, |
| 58 | + }) |
| 59 | + |
| 60 | + mount(RootComponent) |
| 61 | + }) |
| 62 | + |
| 63 | + it('allow call provideLocal and injectLocal in same component', () => { |
| 64 | + const [useProvideCountState, useCountState] = createInjectionState((initialValue: number) => { |
| 65 | + const count = ref(initialValue) |
| 66 | + return count |
| 67 | + }) |
| 68 | + |
| 69 | + const CanProvidingStateAndInjectedStateInSameComponent = defineComponent({ |
| 70 | + setup() { |
| 71 | + useProvideCountState(114514) |
| 72 | + const count = useCountState()! |
| 73 | + expect(count.value).toBe(114514) |
| 74 | + |
| 75 | + return () => h('div') |
| 76 | + }, |
| 77 | + }) |
43 | 78 |
|
44 |
| -describe('allow call useProvidingState and useInjectedState in same component', () => { |
45 |
| - it('should work', () => { |
46 | 79 | mount(CanProvidingStateAndInjectedStateInSameComponent)
|
47 | 80 | })
|
48 | 81 | })
|
0 commit comments