@@ -108,9 +108,6 @@ describe('FormGroup', () => {
108
108
expect ( renderProps ! . id ) . toHaveLength ( 11 ) ;
109
109
expect ( renderProps ! . id ) . toContain ( 'input' ) ;
110
110
111
- expect ( container . querySelector ( 'input' ) ?. getAttribute ( 'aria-labelledby' ) ) . toBe (
112
- `${ renderProps ! . id } --label` ,
113
- ) ;
114
111
expect ( container . querySelector ( '.nhsuk-label' ) ?. getAttribute ( 'id' ) ) . toBe (
115
112
`${ renderProps ! . id } --label` ,
116
113
) ;
@@ -133,7 +130,6 @@ describe('FormGroup', () => {
133
130
expect ( renderProps ) . not . toBe ( null ) ;
134
131
expect ( renderProps ! . id ) . toBe ( 'testID' ) ;
135
132
136
- expect ( container . querySelector ( 'input' ) ?. getAttribute ( 'aria-labelledby' ) ) . toBe ( 'testID--label' ) ;
137
133
expect ( container . querySelector ( '.nhsuk-label' ) ?. getAttribute ( 'id' ) ) . toBe ( 'testID--label' ) ;
138
134
expect ( container . querySelector ( '.nhsuk-label' ) ?. getAttribute ( 'for' ) ) . toBe ( 'testID' ) ;
139
135
expect ( container . querySelector ( '.nhsuk-label' ) ?. textContent ) . toBe ( 'This is a test label' ) ;
@@ -155,6 +151,7 @@ describe('FormGroup', () => {
155
151
expect ( renderProps ) . not . toBe ( null ) ;
156
152
expect ( renderProps ! . id ) . toHaveLength ( 11 ) ;
157
153
expect ( renderProps ! . id ) . toContain ( 'input' ) ;
154
+ expect ( renderProps ! [ 'aria-describedby' ] ) . toBe ( `${ renderProps ! . id } --error-message` ) ;
158
155
159
156
expect ( container . querySelector ( '.nhsuk-error-message' ) ?. getAttribute ( 'id' ) ) . toBe (
160
157
`${ renderProps ! . id } --error-message` ,
@@ -182,6 +179,8 @@ describe('FormGroup', () => {
182
179
183
180
expect ( renderProps ) . not . toBe ( null ) ;
184
181
expect ( renderProps ! . id ) . toBe ( 'testID' ) ;
182
+ expect ( renderProps ! [ 'aria-describedby' ] ) . toBe ( `testID--error-message` ) ;
183
+
185
184
186
185
expect ( container . querySelector ( '.nhsuk-error-message' ) ?. getAttribute ( 'id' ) ) . toBe (
187
186
'testID--error-message' ,
@@ -240,4 +239,32 @@ describe('FormGroup', () => {
240
239
241
240
expect ( await axe ( html ) ) . toHaveNoViolations ( ) ;
242
241
} ) ;
242
+
243
+ it ( 'should add hint ID and error ID to the aria-describedby of the input' , ( ) => {
244
+ const { container } = renderFormGroupComponent ( {
245
+ inputType : 'input' ,
246
+ id : 'error-and-hint' ,
247
+ error : 'This is an error' ,
248
+ hint : 'This is a hint' ,
249
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
250
+ children : ( { error, ...rest } ) => < input { ...rest } /> ,
251
+ } ) ;
252
+
253
+ const inputElement = container . querySelector ( 'input' ) ;
254
+ expect ( inputElement ) . not . toBeNull ( ) ;
255
+ expect ( inputElement ?. getAttribute ( 'aria-describedby' ) ) . toBe ( 'error-and-hint--hint error-and-hint--error-message' ) ;
256
+ } )
257
+
258
+ it ( 'should have no aria-describedby when there is no hint or label' , ( ) => {
259
+ const { container } = renderFormGroupComponent ( {
260
+ inputType : 'input' ,
261
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
262
+ children : ( { error, ...rest } ) => < input { ...rest } /> ,
263
+ } ) ;
264
+
265
+ const inputElement = container . querySelector ( 'input' ) ;
266
+ expect ( inputElement ) . not . toBeNull ( ) ;
267
+
268
+ expect ( inputElement ?. getAttribute ( 'aria-describedby' ) ) . toBe ( null ) ;
269
+ } ) ;
243
270
} ) ;
0 commit comments