@@ -5,7 +5,12 @@ import {
5
5
render ,
6
6
SetupContext
7
7
} from '@vue/runtime-test'
8
- import { defineEmits , defineProps , useContext } from '../src/apiSetupHelpers'
8
+ import {
9
+ defineEmits ,
10
+ defineProps ,
11
+ useAttrs ,
12
+ useSlots
13
+ } from '../src/apiSetupHelpers'
9
14
10
15
describe ( 'SFC <script setup> helpers' , ( ) => {
11
16
test ( 'should warn runtime usage' , ( ) => {
@@ -16,34 +21,41 @@ describe('SFC <script setup> helpers', () => {
16
21
expect ( `defineEmits() is a compiler-hint` ) . toHaveBeenWarned ( )
17
22
} )
18
23
19
- test ( 'useContext (no args)' , ( ) => {
20
- let ctx : SetupContext | undefined
24
+ test ( 'useSlots / useAttrs (no args)' , ( ) => {
25
+ let slots : SetupContext [ 'slots' ] | undefined
26
+ let attrs : SetupContext [ 'attrs' ] | undefined
21
27
const Comp = {
22
28
setup ( ) {
23
- ctx = useContext ( )
29
+ slots = useSlots ( )
30
+ attrs = useAttrs ( )
24
31
return ( ) => { }
25
32
}
26
33
}
27
- render ( h ( Comp ) , nodeOps . createElement ( 'div' ) )
28
- expect ( ctx ) . toMatchObject ( {
29
- attrs : { } ,
30
- slots : { }
31
- } )
32
- expect ( typeof ctx ! . emit ) . toBe ( 'function' )
34
+ const passedAttrs = { id : 'foo' }
35
+ const passedSlots = {
36
+ default : ( ) => { } ,
37
+ x : ( ) => { }
38
+ }
39
+ render ( h ( Comp , passedAttrs , passedSlots ) , nodeOps . createElement ( 'div' ) )
40
+ expect ( typeof slots ! . default ) . toBe ( 'function' )
41
+ expect ( typeof slots ! . x ) . toBe ( 'function' )
42
+ expect ( attrs ) . toMatchObject ( passedAttrs )
33
43
} )
34
44
35
- test ( 'useContext (with args)' , ( ) => {
45
+ test ( 'useSlots / useAttrs (with args)' , ( ) => {
46
+ let slots : SetupContext [ 'slots' ] | undefined
47
+ let attrs : SetupContext [ 'attrs' ] | undefined
36
48
let ctx : SetupContext | undefined
37
- let ctxArg : SetupContext | undefined
38
49
const Comp = defineComponent ( {
39
- setup ( _ , _ctxArg ) {
40
- ctx = useContext ( )
41
- ctxArg = _ctxArg
50
+ setup ( _ , _ctx ) {
51
+ slots = useSlots ( )
52
+ attrs = useAttrs ( )
53
+ ctx = _ctx
42
54
return ( ) => { }
43
55
}
44
56
} )
45
57
render ( h ( Comp ) , nodeOps . createElement ( 'div' ) )
46
- expect ( ctx ) . toBeDefined ( )
47
- expect ( ctx ) . toBe ( ctxArg )
58
+ expect ( slots ) . toBe ( ctx ! . slots )
59
+ expect ( attrs ) . toBe ( ctx ! . attrs )
48
60
} )
49
61
} )
0 commit comments