1
- import { defineComponent , h , nextTick } from 'vue'
1
+ import { defineComponent , h , nextTick , ref } from 'vue'
2
2
import prettier from 'prettier'
3
3
4
4
import { render } from '../../test-utils/vue-testing-library'
5
5
import { Description , useDescriptions } from './description'
6
6
7
7
import { html } from '../../test-utils/html'
8
+ import { click } from '../../test-utils/interactions'
9
+ import { getByText } from '../../test-utils/accessibility-assertions'
8
10
9
11
function format ( input : Element | string ) {
10
12
let contents = ( typeof input === 'string' ? input : ( input as HTMLElement ) . outerHTML ) . trim ( )
@@ -36,18 +38,14 @@ function renderTemplate(input: string | Partial<Parameters<typeof defineComponen
36
38
)
37
39
}
38
40
39
- it ( 'should be possible to use a DescriptionProvider without using a Description' , async ( ) => {
41
+ it ( 'should be possible to use useDescriptions without using a Description' , async ( ) => {
40
42
let { container } = renderTemplate ( {
41
43
render ( ) {
42
- return h ( 'div' , [
43
- h ( this . DescriptionProvider , ( ) => [
44
- h ( 'div' , { 'aria-describedby' : this . describedby } , [ 'No description' ] ) ,
45
- ] ) ,
46
- ] )
44
+ return h ( 'div' , [ h ( 'div' , { 'aria-describedby' : this . describedby } , [ 'No description' ] ) ] )
47
45
} ,
48
46
setup ( ) {
49
- let [ describedby , DescriptionProvider ] = useDescriptions ( )
50
- return { describedby, DescriptionProvider }
47
+ let describedby = useDescriptions ( )
48
+ return { describedby }
51
49
} ,
52
50
} )
53
51
@@ -60,21 +58,19 @@ it('should be possible to use a DescriptionProvider without using a Description'
60
58
)
61
59
} )
62
60
63
- it ( 'should be possible to use a DescriptionProvider and a single Description, and have them linked' , async ( ) => {
61
+ it ( 'should be possible to use useDescriptions and a single Description, and have them linked' , async ( ) => {
64
62
let { container } = renderTemplate ( {
65
63
render ( ) {
66
64
return h ( 'div' , [
67
- h ( this . DescriptionProvider , ( ) => [
68
- h ( 'div' , { 'aria-describedby' : this . describedby } , [
69
- h ( Description , ( ) => 'I am a description' ) ,
70
- h ( 'span' , 'Contents' ) ,
71
- ] ) ,
65
+ h ( 'div' , { 'aria-describedby' : this . describedby } , [
66
+ h ( Description , ( ) => 'I am a description' ) ,
67
+ h ( 'span' , 'Contents' ) ,
72
68
] ) ,
73
69
] )
74
70
} ,
75
71
setup ( ) {
76
- let [ describedby , DescriptionProvider ] = useDescriptions ( )
77
- return { describedby, DescriptionProvider }
72
+ let describedby = useDescriptions ( )
73
+ return { describedby }
78
74
} ,
79
75
} )
80
76
@@ -94,22 +90,20 @@ it('should be possible to use a DescriptionProvider and a single Description, an
94
90
)
95
91
} )
96
92
97
- it ( 'should be possible to use a DescriptionProvider and multiple Description components, and have them linked' , async ( ) => {
93
+ it ( 'should be possible to use useDescriptions and multiple Description components, and have them linked' , async ( ) => {
98
94
let { container } = renderTemplate ( {
99
95
render ( ) {
100
96
return h ( 'div' , [
101
- h ( this . DescriptionProvider , ( ) => [
102
- h ( 'div' , { 'aria-describedby' : this . describedby } , [
103
- h ( Description , ( ) => 'I am a description' ) ,
104
- h ( 'span' , 'Contents' ) ,
105
- h ( Description , ( ) => 'I am also a description' ) ,
106
- ] ) ,
97
+ h ( 'div' , { 'aria-describedby' : this . describedby } , [
98
+ h ( Description , ( ) => 'I am a description' ) ,
99
+ h ( 'span' , 'Contents' ) ,
100
+ h ( Description , ( ) => 'I am also a description' ) ,
107
101
] ) ,
108
102
] )
109
103
} ,
110
104
setup ( ) {
111
- let [ describedby , DescriptionProvider ] = useDescriptions ( )
112
- return { describedby, DescriptionProvider }
105
+ let describedby = useDescriptions ( )
106
+ return { describedby }
113
107
} ,
114
108
} )
115
109
@@ -131,3 +125,47 @@ it('should be possible to use a DescriptionProvider and multiple Description com
131
125
` )
132
126
)
133
127
} )
128
+
129
+ it ( 'should be possible to update a prop from the parent and it should reflect in the Description component' , async ( ) => {
130
+ let { container } = renderTemplate ( {
131
+ render ( ) {
132
+ return h ( 'div' , [
133
+ h ( 'div' , { 'aria-describedby' : this . describedby } , [
134
+ h ( Description , ( ) => 'I am a description' ) ,
135
+ h ( 'button' , { onClick : ( ) => this . count ++ } , '+1' ) ,
136
+ ] ) ,
137
+ ] )
138
+ } ,
139
+ setup ( ) {
140
+ let count = ref ( 0 )
141
+ let describedby = useDescriptions ( { props : { 'data-count' : count } } )
142
+ return { count, describedby }
143
+ } ,
144
+ } )
145
+
146
+ await new Promise < void > ( nextTick )
147
+
148
+ expect ( format ( container . firstElementChild ) ) . toEqual (
149
+ format ( html `
150
+ < div aria-describedby ="headlessui-description-1 ">
151
+ < p data-count ="0 " id ="headlessui-description-1 ">
152
+ I am a description
153
+ </ p >
154
+ < button > +1</ button >
155
+ </ div >
156
+ ` )
157
+ )
158
+
159
+ await click ( getByText ( '+1' ) )
160
+
161
+ expect ( format ( container . firstElementChild ) ) . toEqual (
162
+ format ( html `
163
+ < div aria-describedby ="headlessui-description-1 ">
164
+ < p data-count ="1 " id ="headlessui-description-1 ">
165
+ I am a description
166
+ </ p >
167
+ < button > +1</ button >
168
+ </ div >
169
+ ` )
170
+ )
171
+ } )
0 commit comments