11import {
22 render ,
33 waitFor ,
4+ act ,
5+ findByTestId ,
46} from "@testing-library/preact" ;
57import FieldLabelWrapperComponent from "../fieldLabelWrapper" ;
68import { CslpData } from "../../../cslp/types/cslp.types" ;
@@ -313,7 +315,12 @@ describe("FieldLabelWrapperComponent", () => {
313315 />
314316 ) ;
315317
316- // Use waitFor with querySelector for faster detection
318+ // Use act() to ensure React processes all state updates
319+ await act ( async ( ) => {
320+ await new Promise ( resolve => setTimeout ( resolve , 0 ) ) ;
321+ } ) ;
322+
323+ // Use waitFor with shorter timeout since mocks resolve immediately
317324 await waitFor (
318325 ( ) => {
319326 const text = Array . from ( container . querySelectorAll ( "*" ) ) . find (
@@ -322,7 +329,7 @@ describe("FieldLabelWrapperComponent", () => {
322329 if ( ! text ) throw new Error ( "Text not found" ) ;
323330 expect ( text ) . toBeInTheDocument ( ) ;
324331 } ,
325- { timeout : 2000 , interval : 10 } // Reduced timeout from 5s to 2s
332+ { timeout : 1000 , interval : 10 }
326333 ) ;
327334 } ) ;
328335
@@ -336,17 +343,19 @@ describe("FieldLabelWrapperComponent", () => {
336343 />
337344 ) ;
338345
339- // Use waitFor with querySelector for faster detection
340- await waitFor (
341- ( ) => {
342- const icon = container . querySelector (
343- '[data-testid="visual-builder__field-icon"]'
344- ) ;
345- if ( ! icon ) throw new Error ( "Icon not found" ) ;
346- expect ( icon ) . toBeInTheDocument ( ) ;
347- } ,
348- { timeout : 2000 , interval : 10 } // Reduced timeout from 5s to 2s
346+ // Use act() to ensure React processes all state updates
347+ await act ( async ( ) => {
348+ await new Promise ( resolve => setTimeout ( resolve , 0 ) ) ;
349+ } ) ;
350+
351+ // Use findByTestId which is optimized for async queries
352+ const icon = await findByTestId (
353+ container ,
354+ "visual-builder__field-icon" ,
355+ { } ,
356+ { timeout : 1000 }
349357 ) ;
358+ expect ( icon ) . toBeInTheDocument ( ) ;
350359 } ) ;
351360
352361 test ( "renders with correct class when field is disabled" , async ( ) => {
@@ -363,18 +372,20 @@ describe("FieldLabelWrapperComponent", () => {
363372 />
364373 ) ;
365374
366- // Use waitFor to find element and check class in one go
367- await waitFor (
368- ( ) => {
369- const fieldLabel = container . querySelector (
370- '[data-testid="visual-builder__focused-toolbar__field-label-wrapper"]'
371- ) as HTMLElement ;
372- if ( ! fieldLabel ) throw new Error ( "Field label not found" ) ;
373- expect ( fieldLabel ) . toHaveClass (
374- "visual-builder__focused-toolbar--field-disabled"
375- ) ;
376- } ,
377- { timeout : 2000 , interval : 10 } // Reduced timeout from 5s+1s to 2s total
375+ // Use act() to ensure React processes all state updates
376+ await act ( async ( ) => {
377+ await new Promise ( resolve => setTimeout ( resolve , 0 ) ) ;
378+ } ) ;
379+
380+ // Use findByTestId which is optimized for async queries
381+ const fieldLabel = await findByTestId (
382+ container ,
383+ "visual-builder__focused-toolbar__field-label-wrapper" ,
384+ { } ,
385+ { timeout : 1000 }
386+ ) as HTMLElement ;
387+ expect ( fieldLabel ) . toHaveClass (
388+ "visual-builder__focused-toolbar--field-disabled"
378389 ) ;
379390 } ) ;
380391
@@ -388,7 +399,12 @@ describe("FieldLabelWrapperComponent", () => {
388399 />
389400 ) ;
390401
391- // Wait for component to mount and isFieldDisabled to be called in one go
402+ // Use act() to ensure React processes all state updates
403+ await act ( async ( ) => {
404+ await new Promise ( resolve => setTimeout ( resolve , 0 ) ) ;
405+ } ) ;
406+
407+ // Wait for component to mount and isFieldDisabled to be called
392408 await waitFor (
393409 ( ) => {
394410 const fieldLabel = container . querySelector (
@@ -397,7 +413,7 @@ describe("FieldLabelWrapperComponent", () => {
397413 if ( ! fieldLabel ) throw new Error ( "Field label not found" ) ;
398414 expect ( isFieldDisabled ) . toHaveBeenCalled ( ) ;
399415 } ,
400- { timeout : 2000 , interval : 10 } // Reduced timeout from 5s+1s to 2s total
416+ { timeout : 1000 , interval : 10 }
401417 ) ;
402418
403419 expect ( isFieldDisabled ) . toHaveBeenCalledWith (
@@ -463,6 +479,11 @@ describe("FieldLabelWrapperComponent", () => {
463479 />
464480 ) ;
465481
482+ // Use act() to ensure React processes all state updates
483+ await act ( async ( ) => {
484+ await new Promise ( resolve => setTimeout ( resolve , 0 ) ) ;
485+ } ) ;
486+
466487 // When loading, component returns LoadingIcon, not the main structure
467488 // ContentTypeIcon only renders when dataLoading is false, which won't happen here
468489 // So we should see LoadingIcon and NOT see ContentTypeIcon
@@ -474,7 +495,7 @@ describe("FieldLabelWrapperComponent", () => {
474495 ) ;
475496 expect ( contentTypeIcon ) . not . toBeInTheDocument ( ) ;
476497 } ,
477- { timeout : 3000 , interval : 5 } // Reduced timeout from 5s to 3s with faster polling
498+ { timeout : 1000 , interval : 10 } // Reduced timeout - mocks resolve immediately
478499 ) ;
479500 } ) ;
480501
@@ -518,21 +539,24 @@ describe("FieldLabelWrapperComponent", () => {
518539 />
519540 ) ;
520541
521- // Wait for component to load and check variant indicator in one go
522- await waitFor (
523- ( ) => {
524- const fieldLabel = container . querySelector (
525- '[data-testid="visual-builder__focused-toolbar__field-label-wrapper"]'
526- ) ;
527- if ( ! fieldLabel ) throw new Error ( "Field label not found" ) ;
528-
529- const variantIndicator = container . querySelector (
530- "[data-testid='variant-indicator']"
531- ) ;
532- expect ( variantIndicator ) . not . toBeInTheDocument ( ) ;
533- } ,
534- { timeout : 2000 , interval : 10 } // Reduced timeout from 5s to 2s
542+ // Use act() to ensure React processes all state updates
543+ await act ( async ( ) => {
544+ await new Promise ( resolve => setTimeout ( resolve , 0 ) ) ;
545+ } ) ;
546+
547+ // Wait for component to load and check variant indicator
548+ const fieldLabel = await findByTestId (
549+ container ,
550+ "visual-builder__focused-toolbar__field-label-wrapper" ,
551+ { } ,
552+ { timeout : 1000 }
535553 ) ;
554+ expect ( fieldLabel ) . toBeInTheDocument ( ) ;
555+
556+ const variantIndicator = container . querySelector (
557+ "[data-testid='variant-indicator']"
558+ ) ;
559+ expect ( variantIndicator ) . not . toBeInTheDocument ( ) ;
536560 } ) ;
537561
538562 test . skip ( "applies variant CSS classes when field has variant" , async ( ) => {
@@ -585,18 +609,20 @@ describe("FieldLabelWrapperComponent", () => {
585609 />
586610 ) ;
587611
588- // Wait for component to render and check class in one go
589- await waitFor (
590- ( ) => {
591- const fieldLabelWrapper = container . querySelector (
592- '[data-testid="visual-builder__focused-toolbar__field-label-wrapper"]'
593- ) as HTMLElement ;
594- if ( ! fieldLabelWrapper ) throw new Error ( "Field label not found" ) ;
595- expect ( fieldLabelWrapper ) . not . toHaveClass (
596- "visual-builder__focused-toolbar--variant"
597- ) ;
598- } ,
599- { timeout : 2000 , interval : 10 } // Reduced timeout from 5s to 2s
612+ // Use act() to ensure React processes all state updates
613+ await act ( async ( ) => {
614+ await new Promise ( resolve => setTimeout ( resolve , 0 ) ) ;
615+ } ) ;
616+
617+ // Use findByTestId which is optimized for async queries
618+ const fieldLabelWrapper = await findByTestId (
619+ container ,
620+ "visual-builder__focused-toolbar__field-label-wrapper" ,
621+ { } ,
622+ { timeout : 1000 }
623+ ) as HTMLElement ;
624+ expect ( fieldLabelWrapper ) . not . toHaveClass (
625+ "visual-builder__focused-toolbar--variant"
600626 ) ;
601627 } ) ;
602628} ) ;
0 commit comments