diff --git a/packages/ui/components/form/step/Steps.tsx b/packages/ui/components/form/step/Steps.tsx index 4fd883c1e2..3c52946628 100644 --- a/packages/ui/components/form/step/Steps.tsx +++ b/packages/ui/components/form/step/Steps.tsx @@ -17,7 +17,7 @@ const Steps = (props: ISteps) => { return (

{stepLabel(currentStep, maxSteps)}

-
+
{new Array(maxSteps).fill(0).map((_s, index) => { return index <= currentStep - 1 ? (
{ "bg-inverted h-1 w-full rounded-[1px]", index < currentStep - 1 ? "cursor-pointer" : "" )} + data-testid={`step-indicator-${index}`} /> ) : ( -
+
); })}
diff --git a/packages/ui/components/form/step/steps.test.tsx b/packages/ui/components/form/step/steps.test.tsx new file mode 100644 index 0000000000..57af482922 --- /dev/null +++ b/packages/ui/components/form/step/steps.test.tsx @@ -0,0 +1,62 @@ +/* eslint-disable playwright/no-conditional-in-test */ + +/* eslint-disable playwright/missing-playwright-await */ +import { render, fireEvent } from "@testing-library/react"; +import { vi } from "vitest"; + +import { Steps } from "./Steps"; + +const MAX_STEPS = 10; +const CURRENT_STEP = 5; +const mockNavigateToStep = vi.fn(); + +const Props = { + maxSteps: MAX_STEPS, + currentStep: CURRENT_STEP, + navigateToStep: mockNavigateToStep, + stepLabel: (currentStep: number, totalSteps: number) => `Test Step ${currentStep} of ${totalSteps}`, +}; + +describe("Tests for Steps Component", () => { + test("Should render the correct number of steps", () => { + const { queryByTestId } = render(); + + const stepIndicatorDivs = queryByTestId("step-indicator-container"); + const childDivs = stepIndicatorDivs?.querySelectorAll("div"); + + const count = childDivs?.length; + expect(stepIndicatorDivs).toBeInTheDocument(); + + expect(count).toBe(MAX_STEPS); + + for (let i = 0; i < MAX_STEPS; i++) { + const step = queryByTestId(`step-indicator-${i}`); + if (i < CURRENT_STEP - 1) { + expect(step).toHaveClass("cursor-pointer"); + } else { + expect(step).not.toHaveClass("cursor-pointer"); + } + } + }); + + test("Should render correctly the label of the steps", () => { + const { getByText } = render(); + + expect(getByText(`Test Step ${CURRENT_STEP} of ${MAX_STEPS}`)).toBeInTheDocument(); + }); + + test("Should navigate to the correct step when clicked", async () => { + const { getByTestId } = render(); + + for (let i = 0; i < MAX_STEPS; i++) { + const stepIndicator = getByTestId(`step-indicator-${i}`); + if (i < CURRENT_STEP - 1) { + fireEvent.click(stepIndicator); + expect(mockNavigateToStep).toHaveBeenCalledWith(i); + mockNavigateToStep.mockClear(); + } else { + expect(mockNavigateToStep).not.toHaveBeenCalled(); + } + } + }); +});