1
- import { type InjectionKey , type Ref , defineComponent , h , inject , ref } from 'vue-demi'
2
- import { createInjectionState } from '@vueuse/shared'
1
+ import { type InjectionKey , type Ref , defineComponent , h , inject , nextTick , ref } from 'vue-demi'
2
+ import { createInjectionState , injectLocal } from '@vueuse/shared'
3
3
import { describe , expect , it } from 'vitest'
4
- import { mount } from '../../.test'
4
+ import { mount , useSetup } from '../../.test'
5
5
6
6
describe ( 'createInjectionState' , ( ) => {
7
- it ( 'should work 1' , ( ) => {
7
+ it ( 'should work for simple nested component' , async ( ) => {
8
8
const [ useProvideCountState , useCountState ] = createInjectionState ( ( initialValue : number ) => {
9
9
const count = ref ( initialValue )
10
10
return count
11
11
} )
12
12
13
+ let count : Ref < number > | undefined
14
+
13
15
const ChildComponent = defineComponent ( {
14
16
setup ( ) {
15
- const count = useCountState ( )
16
- expect ( count ?. value ) . toBe ( 0 )
17
+ count = useCountState ( )
17
18
18
19
return ( ) => h ( 'div' )
19
20
} ,
20
21
} )
21
22
22
23
const RootComponent = defineComponent ( {
23
24
setup ( ) {
24
- useProvideCountState ( 0 )
25
+ useProvideCountState ( 114514 )
25
26
26
27
return ( ) => h ( ChildComponent )
27
28
} ,
28
29
} )
29
30
30
- mount ( RootComponent )
31
+ const vm = mount ( RootComponent )
32
+ await nextTick ( )
33
+
34
+ expect ( count ?. value ) . toBe ( 114514 )
35
+ vm . unmount ( )
31
36
} )
32
37
33
- it ( 'should work ( custom key)' , ( ) => {
38
+ it ( 'should work for custom key' , async ( ) => {
34
39
const KEY : InjectionKey < Ref < number > > = Symbol ( 'count-state' )
35
40
36
41
const [ useProvideCountState , useCountState ] = createInjectionState ( ( initialValue : number ) => {
37
42
const count = ref ( initialValue )
38
43
return count
39
44
} , { injectionKey : KEY } )
40
45
46
+ let count : Ref < number > | undefined
47
+ let count2 : Ref < number > | undefined
41
48
const ChildComponent = defineComponent ( {
42
49
setup ( ) {
43
- const count = useCountState ( )
44
- expect ( count ?. value ) . toBe ( 0 )
45
- const count2 = inject ( KEY )
46
- expect ( count2 ?. value ) . toBe ( 0 )
50
+ count = useCountState ( )
51
+ count2 = inject ( KEY )
47
52
48
53
return ( ) => h ( 'div' )
49
54
} ,
@@ -57,25 +62,43 @@ describe('createInjectionState', () => {
57
62
} ,
58
63
} )
59
64
60
- mount ( RootComponent )
65
+ const vm = mount ( RootComponent )
66
+ await nextTick ( )
67
+ expect ( count ?. value ) . toBe ( 0 )
68
+ expect ( count2 ?. value ) . toBe ( 0 )
69
+ vm . unmount ( )
61
70
} )
62
71
63
- it ( 'allow call provideLocal and injectLocal in same component' , ( ) => {
72
+ it ( 'allow call useProvidingState and useInjectedState in same component' , async ( ) => {
64
73
const [ useProvideCountState , useCountState ] = createInjectionState ( ( initialValue : number ) => {
65
74
const count = ref ( initialValue )
66
75
return count
67
76
} )
77
+ const vm = useSetup ( ( ) => {
78
+ useProvideCountState ( 114514 )
79
+ const count = useCountState ( ) !
68
80
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
- } ,
81
+ return { count }
77
82
} )
83
+ await nextTick ( )
84
+ expect ( vm . count ) . toBe ( 114514 )
85
+ vm . unmount ( )
86
+ } )
78
87
79
- mount ( CanProvidingStateAndInjectedStateInSameComponent )
88
+ it ( 'allow call useProvidingState and injectLocal in same component' , async ( ) => {
89
+ const KEY : InjectionKey < Ref < number > > | string = Symbol ( 'count-state' )
90
+ const [ useProvideCountState ] = createInjectionState ( ( initialValue : number ) => {
91
+ const count = ref ( initialValue )
92
+ return count
93
+ } , { injectionKey : KEY } )
94
+ const vm = useSetup ( ( ) => {
95
+ useProvideCountState ( 114514 )
96
+ const count = injectLocal ( KEY ) !
97
+
98
+ return { count }
99
+ } )
100
+ await nextTick ( )
101
+ expect ( vm . count ) . toBe ( 114514 )
102
+ vm . unmount ( )
80
103
} )
81
104
} )
0 commit comments