Skip to content

Commit ce71525

Browse files
committed
timeout improve
1 parent 58dc4dc commit ce71525

1 file changed

Lines changed: 69 additions & 42 deletions

File tree

src/visualBuilder/components/__test__/fieldLabelWrapper.test.tsx

Lines changed: 69 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -596,13 +596,23 @@ describe("FieldLabelWrapperComponent", () => {
596596
/>
597597
);
598598

599-
// Use findByTestId to wait for variant indicator - faster and more reliable
600-
// No need to check button since findByTestId ensures component is loaded
601-
const variantIndicator = await findByTestId(
602-
container as HTMLElement,
603-
"variant-indicator",
604-
{},
605-
{ timeout: 1000 }
599+
// Wait for component to finish loading (button enabled = dataLoading false)
600+
// This ensures all async operations complete before checking variant indicator
601+
await waitFor(
602+
() => {
603+
const button = container.querySelector("button");
604+
if (!button || button.hasAttribute("disabled")) {
605+
throw new Error(
606+
"Button still disabled - component not loaded"
607+
);
608+
}
609+
},
610+
{ timeout: 5000, interval: 10 }
611+
);
612+
613+
// Now check for variant indicator - component is fully loaded
614+
const variantIndicator = container.querySelector(
615+
"[data-testid='variant-indicator']"
606616
);
607617
expect(variantIndicator).toBeInTheDocument();
608618
});
@@ -617,17 +627,21 @@ describe("FieldLabelWrapperComponent", () => {
617627
/>
618628
);
619629

620-
// findByTestId handles act() internally, so we don't need a separate act() call
621-
// This eliminates the redundant act() bottleneck
622-
// Wait for component to load and check variant indicator
623-
const fieldLabel = await findByTestId(
624-
container as HTMLElement,
625-
"visual-builder__focused-toolbar__field-label-wrapper",
626-
{},
627-
{ timeout: 1000 }
630+
// Wait for component to finish loading (button enabled = dataLoading false)
631+
// This ensures all async operations complete before checking variant indicator
632+
await waitFor(
633+
() => {
634+
const button = container.querySelector("button");
635+
if (!button || button.hasAttribute("disabled")) {
636+
throw new Error(
637+
"Button still disabled - component not loaded"
638+
);
639+
}
640+
},
641+
{ timeout: 5000, interval: 10 }
628642
);
629-
expect(fieldLabel).toBeInTheDocument();
630643

644+
// Now check that variant indicator is NOT present - component is fully loaded
631645
const variantIndicator = container.querySelector(
632646
"[data-testid='variant-indicator']"
633647
);
@@ -649,23 +663,27 @@ describe("FieldLabelWrapperComponent", () => {
649663
/>
650664
);
651665

652-
// Use findByTestId to wait for field label wrapper - faster and more reliable
653-
// Then check for variant class - no need to wait for button
654-
const fieldLabelWrapper = (await findByTestId(
655-
container as HTMLElement,
656-
"visual-builder__focused-toolbar__field-label-wrapper",
657-
{},
658-
{ timeout: 1000 }
659-
)) as HTMLElement;
660-
661-
// Wait for variant class to be applied (may take a microtask)
666+
// Wait for component to finish loading (button enabled = dataLoading false)
667+
// This ensures all async operations complete before checking variant class
662668
await waitFor(
663669
() => {
664-
expect(fieldLabelWrapper).toHaveClass(
665-
"visual-builder__focused-toolbar--variant"
666-
);
670+
const button = container.querySelector("button");
671+
if (!button || button.hasAttribute("disabled")) {
672+
throw new Error(
673+
"Button still disabled - component not loaded"
674+
);
675+
}
667676
},
668-
{ timeout: 1000, interval: 5 }
677+
{ timeout: 5000, interval: 10 }
678+
);
679+
680+
// Now check for variant class - component is fully loaded
681+
const fieldLabelWrapper = container.querySelector(
682+
"[data-testid='visual-builder__focused-toolbar__field-label-wrapper']"
683+
) as HTMLElement;
684+
expect(fieldLabelWrapper).toBeInTheDocument();
685+
expect(fieldLabelWrapper).toHaveClass(
686+
"visual-builder__focused-toolbar--variant"
669687
);
670688
});
671689

@@ -679,18 +697,27 @@ describe("FieldLabelWrapperComponent", () => {
679697
/>
680698
);
681699

682-
// Use findByTestId which is optimized and handles act() internally
683-
// This is much faster than waitFor and avoids timeout issues
684-
const fieldLabelWrapper = (await findByTestId(
685-
container as HTMLElement,
686-
"visual-builder__focused-toolbar__field-label-wrapper",
687-
{},
688-
{ timeout: 1000 }
689-
)) as HTMLElement;
690-
691-
// Direct assertion - no need to wait for button since component is loaded
692-
expect(fieldLabelWrapper).not.toHaveClass(
693-
"visual-builder__focused-toolbar--variant"
700+
// Wait for component to finish loading - check both field label wrapper and button
701+
// This ensures all async operations complete before checking variant class
702+
await waitFor(
703+
() => {
704+
const fieldLabelWrapper = container.querySelector(
705+
"[data-testid='visual-builder__focused-toolbar__field-label-wrapper']"
706+
);
707+
const button = container.querySelector("button");
708+
if (
709+
!fieldLabelWrapper ||
710+
!button ||
711+
button.hasAttribute("disabled")
712+
) {
713+
throw new Error("Component not fully loaded");
714+
}
715+
// Component is loaded - check variant class is NOT present
716+
expect(fieldLabelWrapper).not.toHaveClass(
717+
"visual-builder__focused-toolbar--variant"
718+
);
719+
},
720+
{ timeout: 5000, interval: 10 }
694721
);
695722
});
696723
});

0 commit comments

Comments
 (0)