@@ -108,9 +108,6 @@ describe('FormGroup', () => {
108108 expect ( renderProps ! . id ) . toHaveLength ( 11 ) ;
109109 expect ( renderProps ! . id ) . toContain ( 'input' ) ;
110110
111- expect ( container . querySelector ( 'input' ) ?. getAttribute ( 'aria-labelledby' ) ) . toBe (
112- `${ renderProps ! . id } --label` ,
113- ) ;
114111 expect ( container . querySelector ( '.nhsuk-label' ) ?. getAttribute ( 'id' ) ) . toBe (
115112 `${ renderProps ! . id } --label` ,
116113 ) ;
@@ -133,7 +130,6 @@ describe('FormGroup', () => {
133130 expect ( renderProps ) . not . toBe ( null ) ;
134131 expect ( renderProps ! . id ) . toBe ( 'testID' ) ;
135132
136- expect ( container . querySelector ( 'input' ) ?. getAttribute ( 'aria-labelledby' ) ) . toBe ( 'testID--label' ) ;
137133 expect ( container . querySelector ( '.nhsuk-label' ) ?. getAttribute ( 'id' ) ) . toBe ( 'testID--label' ) ;
138134 expect ( container . querySelector ( '.nhsuk-label' ) ?. getAttribute ( 'for' ) ) . toBe ( 'testID' ) ;
139135 expect ( container . querySelector ( '.nhsuk-label' ) ?. textContent ) . toBe ( 'This is a test label' ) ;
@@ -155,6 +151,7 @@ describe('FormGroup', () => {
155151 expect ( renderProps ) . not . toBe ( null ) ;
156152 expect ( renderProps ! . id ) . toHaveLength ( 11 ) ;
157153 expect ( renderProps ! . id ) . toContain ( 'input' ) ;
154+ expect ( renderProps ! [ 'aria-describedby' ] ) . toBe ( `${ renderProps ! . id } --error-message` ) ;
158155
159156 expect ( container . querySelector ( '.nhsuk-error-message' ) ?. getAttribute ( 'id' ) ) . toBe (
160157 `${ renderProps ! . id } --error-message` ,
@@ -182,6 +179,8 @@ describe('FormGroup', () => {
182179
183180 expect ( renderProps ) . not . toBe ( null ) ;
184181 expect ( renderProps ! . id ) . toBe ( 'testID' ) ;
182+ expect ( renderProps ! [ 'aria-describedby' ] ) . toBe ( `testID--error-message` ) ;
183+
185184
186185 expect ( container . querySelector ( '.nhsuk-error-message' ) ?. getAttribute ( 'id' ) ) . toBe (
187186 'testID--error-message' ,
@@ -240,4 +239,32 @@ describe('FormGroup', () => {
240239
241240 expect ( await axe ( html ) ) . toHaveNoViolations ( ) ;
242241 } ) ;
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+ } ) ;
243270} ) ;
0 commit comments