@@ -139,7 +139,7 @@ describe('PlainInput', () => {
139139
140140 await userEvent . click ( getByRole ( 'button' , { name : / s e t e r r o r / i } ) ) ;
141141
142- expect ( await findByText ( / S o m e E r r o r / i) ) . toBeInTheDocument ( ) ;
142+ expect ( await findByText ( / S o m e E r r o r / i, { selector : '#error-firstname' } ) ) . toBeInTheDocument ( ) ;
143143
144144 const input = getByLabelText ( / s o m e l a b e l / i) ;
145145 expect ( input ) . toHaveAttribute ( 'aria-invalid' , 'true' ) ;
@@ -163,7 +163,7 @@ describe('PlainInput', () => {
163163 const { findByLabelText, findByText } = render ( < Field actionLabel = { 'take action' } /> , { wrapper } ) ;
164164
165165 fireEvent . focus ( await findByLabelText ( / s o m e l a b e l / i) ) ;
166- expect ( await findByText ( / s o m e i n f o / i) ) . toBeInTheDocument ( ) ;
166+ expect ( await findByText ( / s o m e i n f o / i, { selector : '#firstname-info-feedback' } ) ) . toBeInTheDocument ( ) ;
167167 } ) ;
168168
169169 it ( 'with success feedback and aria-describedby' , async ( ) => {
@@ -178,7 +178,7 @@ describe('PlainInput', () => {
178178
179179 await userEvent . click ( getByRole ( 'button' , { name : / s e t s u c c e s s / i } ) ) ;
180180
181- expect ( await findByText ( / S o m e S u c c e s s / i) ) . toBeInTheDocument ( ) ;
181+ expect ( await findByText ( / S o m e S u c c e s s / i, { selector : '#firstname-success-feedback' } ) ) . toBeInTheDocument ( ) ;
182182
183183 const input = getByLabelText ( / s o m e l a b e l / i) ;
184184 expect ( input ) . toHaveAttribute ( 'aria-invalid' , 'false' ) ;
@@ -202,15 +202,15 @@ describe('PlainInput', () => {
202202
203203 // Start with error
204204 await userEvent . click ( getByRole ( 'button' , { name : / s e t e r r o r / i } ) ) ;
205- expect ( await findByText ( / S o m e E r r o r / i) ) . toBeInTheDocument ( ) ;
205+ expect ( await findByText ( / S o m e E r r o r / i, { selector : '#error-firstname' } ) ) . toBeInTheDocument ( ) ;
206206
207207 let input = getByLabelText ( / s o m e l a b e l / i) ;
208208 expect ( input ) . toHaveAttribute ( 'aria-invalid' , 'true' ) ;
209209 expect ( input ) . toHaveAttribute ( 'aria-describedby' , 'error-firstname' ) ;
210210
211211 // Transition to success
212212 await userEvent . click ( getByRole ( 'button' , { name : / s e t s u c c e s s / i } ) ) ;
213- expect ( await findByText ( / S o m e S u c c e s s / i) ) . toBeInTheDocument ( ) ;
213+ expect ( await findByText ( / S o m e S u c c e s s / i, { selector : '#firstname-success-feedback' } ) ) . toBeInTheDocument ( ) ;
214214
215215 input = getByLabelText ( / s o m e l a b e l / i) ;
216216 expect ( input ) . toHaveAttribute ( 'aria-invalid' , 'false' ) ;
@@ -232,9 +232,24 @@ describe('PlainInput', () => {
232232
233233 const { getByRole, findByText, container } = render ( < Field /> , { wrapper } ) ;
234234
235+ // Pre-state: aria-live region exists and is empty
236+ const preRegions = container . querySelectorAll ( '[aria-live="polite"]' ) ;
237+ expect ( preRegions . length ) . toBeGreaterThanOrEqual ( 1 ) ;
238+ const preSrOnly = Array . from ( preRegions ) . find ( el => {
239+ const style = window . getComputedStyle ( el ) ;
240+ return style . position === 'absolute' && style . width === '1px' ;
241+ } ) ;
242+ expect ( preSrOnly ) . toBeDefined ( ) ;
243+ expect ( preSrOnly ?. textContent ?? '' ) . toMatch ( / ^ \s * $ / ) ;
244+
245+ // Input is not in error and not described yet
246+ const inputEl = container . querySelector ( 'input#firstname-field' ) ;
247+ expect ( inputEl ) . toHaveAttribute ( 'aria-invalid' , 'false' ) ;
248+ expect ( inputEl ) . not . toHaveAttribute ( 'aria-describedby' ) ;
249+
235250 // Set error feedback
236251 await userEvent . click ( getByRole ( 'button' , { name : / s e t e r r o r / i } ) ) ;
237- expect ( await findByText ( / S o m e E r r o r / i) ) . toBeInTheDocument ( ) ;
252+ expect ( await findByText ( / S o m e E r r o r / i, { selector : '#error-firstname' } ) ) . toBeInTheDocument ( ) ;
238253
239254 // Verify there's a screen-reader-only aria-live region with the error content
240255 const ariaLiveRegions = container . querySelectorAll ( '[aria-live="polite"]' ) ;
@@ -250,7 +265,7 @@ describe('PlainInput', () => {
250265
251266 // Transition to success
252267 await userEvent . click ( getByRole ( 'button' , { name : / s e t s u c c e s s / i } ) ) ;
253- expect ( await findByText ( / S o m e S u c c e s s / i) ) . toBeInTheDocument ( ) ;
268+ expect ( await findByText ( / S o m e S u c c e s s / i, { selector : '#firstname-success-feedback' } ) ) . toBeInTheDocument ( ) ;
254269
255270 // Verify the screen reader only region updated its content
256271 expect ( srOnlyRegion ) . toHaveTextContent ( / S o m e S u c c e s s / i) ;
0 commit comments