1- import { fireEvent , render , screen } from '@testing-library/react' ;
1+ import { fireEvent , render , screen , waitFor } from '@testing-library/react' ;
22import userEvent from '@testing-library/user-event' ;
33import { describe , expect , it } from 'vitest' ;
44
@@ -139,7 +139,9 @@ describe('PlainInput', () => {
139139
140140 await userEvent . click ( getByRole ( 'button' , { name : / s e t e r r o r / i } ) ) ;
141141
142- await screen . findByText ( / S o m e E r r o r / i, { selector : '[id^="error-"]' } ) ;
142+ await waitFor ( ( ) => {
143+ expect ( container . querySelector ( '#error-firstname' ) ) . toBeInTheDocument ( ) ;
144+ } ) ;
143145
144146 const input = getByLabelText ( / s o m e l a b e l / i) ;
145147 expect ( input ) . toHaveAttribute ( 'aria-invalid' , 'true' ) ;
@@ -160,10 +162,12 @@ describe('PlainInput', () => {
160162 infoText : 'some info' ,
161163 } ) ;
162164
163- const { findByLabelText } = render ( < Field actionLabel = { 'take action' } /> , { wrapper } ) ;
165+ const { findByLabelText, container } = render ( < Field actionLabel = { 'take action' } /> , { wrapper } ) ;
164166
165167 fireEvent . focus ( await findByLabelText ( / s o m e l a b e l / i) ) ;
166- await screen . findByText ( / s o m e i n f o / i, { selector : '[id$="-info-feedback"]' } ) ;
168+ await waitFor ( ( ) => {
169+ expect ( container . querySelector ( '#firstname-info-feedback' ) ) . toBeInTheDocument ( ) ;
170+ } ) ;
167171 } ) ;
168172
169173 it ( 'with success feedback and aria-describedby' , async ( ) => {
@@ -178,7 +182,9 @@ describe('PlainInput', () => {
178182
179183 await userEvent . click ( getByRole ( 'button' , { name : / s e t s u c c e s s / i } ) ) ;
180184
181- await screen . findByText ( / S o m e S u c c e s s / i, { selector : '[id$="-success-feedback"]' } ) ;
185+ await waitFor ( ( ) => {
186+ expect ( container . querySelector ( '#firstname-success-feedback' ) ) . toBeInTheDocument ( ) ;
187+ } ) ;
182188
183189 const input = getByLabelText ( / s o m e l a b e l / i) ;
184190 expect ( input ) . toHaveAttribute ( 'aria-invalid' , 'false' ) ;
@@ -202,15 +208,19 @@ describe('PlainInput', () => {
202208
203209 // Start with error
204210 await userEvent . click ( getByRole ( 'button' , { name : / s e t e r r o r / i } ) ) ;
205- await screen . findByText ( / S o m e E r r o r / i, { selector : '[id^="error-"]' } ) ;
211+ await waitFor ( ( ) => {
212+ expect ( container . querySelector ( '#error-firstname' ) ) . toBeInTheDocument ( ) ;
213+ } ) ;
206214
207215 let input = getByLabelText ( / s o m e l a b e l / i) ;
208216 expect ( input ) . toHaveAttribute ( 'aria-invalid' , 'true' ) ;
209217 expect ( input ) . toHaveAttribute ( 'aria-describedby' , 'error-firstname' ) ;
210218
211219 // Transition to success
212220 await userEvent . click ( getByRole ( 'button' , { name : / s e t s u c c e s s / i } ) ) ;
213- await screen . findByText ( / S o m e S u c c e s s / i, { selector : '[id$="-success-feedback"]' } ) ;
221+ await waitFor ( ( ) => {
222+ expect ( container . querySelector ( '#firstname-success-feedback' ) ) . toBeInTheDocument ( ) ;
223+ } ) ;
214224
215225 input = getByLabelText ( / s o m e l a b e l / i) ;
216226 expect ( input ) . toHaveAttribute ( 'aria-invalid' , 'false' ) ;
@@ -249,7 +259,9 @@ describe('PlainInput', () => {
249259
250260 // Set error feedback
251261 await userEvent . click ( getByRole ( 'button' , { name : / s e t e r r o r / i } ) ) ;
252- await screen . findByText ( / S o m e E r r o r / i, { selector : '[id^="error-"]' } ) ;
262+ await waitFor ( ( ) => {
263+ expect ( container . querySelector ( '#error-firstname' ) ) . toBeInTheDocument ( ) ;
264+ } ) ;
253265
254266 // Verify there's a screen-reader-only aria-live region with the error content
255267 const ariaLiveRegions = container . querySelectorAll ( '[aria-live="polite"]' ) ;
@@ -265,7 +277,9 @@ describe('PlainInput', () => {
265277
266278 // Transition to success
267279 await userEvent . click ( getByRole ( 'button' , { name : / s e t s u c c e s s / i } ) ) ;
268- await screen . findByText ( / S o m e S u c c e s s / i, { selector : '[id$="-success-feedback"]' } ) ;
280+ await waitFor ( ( ) => {
281+ expect ( container . querySelector ( '#firstname-success-feedback' ) ) . toBeInTheDocument ( ) ;
282+ } ) ;
269283
270284 // Verify the screen reader only region updated its content
271285 expect ( srOnlyRegion ) . toHaveTextContent ( / S o m e S u c c e s s / i) ;
0 commit comments