Skip to content

Commit efa6d46

Browse files
gitstart-calcomgitstartshivamklr
authored
refactor: removed redundant test (#10785)
Co-authored-by: gitstart-calcom <[email protected]> Co-authored-by: Shivam Kalra <[email protected]>
1 parent 811262d commit efa6d46

File tree

2 files changed

+152
-4
lines changed

2 files changed

+152
-4
lines changed

packages/ui/components/form/wizard/WizardForm.tsx

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,23 @@ function WizardForm<T extends DefaultStep>(props: {
4343
}, [currentStep]);
4444

4545
return (
46-
<div className="mx-auto mt-4 print:w-full">
46+
<div className="mx-auto mt-4 print:w-full" data-testid="wizard-form">
4747
<div className={classNames("overflow-hidden md:mb-2 md:w-[700px]", props.containerClassname)}>
4848
<div className="px-6 py-5 sm:px-14">
49-
<h1 className="font-cal text-emphasis text-2xl">{currentStep.title}</h1>
50-
<p className="text-subtle text-sm">{currentStep.description}</p>
49+
<h1 className="font-cal text-emphasis text-2xl" data-testid="step-title">
50+
{currentStep.title}
51+
</h1>
52+
<p className="text-subtle text-sm" data-testid="step-description">
53+
{currentStep.description}
54+
</p>
5155
{!props.disableNavigation && (
52-
<Steps maxSteps={steps.length} currentStep={step} navigateToStep={noop} stepLabel={stepLabel} />
56+
<Steps
57+
maxSteps={steps.length}
58+
currentStep={step}
59+
navigateToStep={noop}
60+
stepLabel={stepLabel}
61+
data-testid="wizard-step-component"
62+
/>
5363
)}
5464
</div>
5565
</div>
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/* eslint-disable playwright/missing-playwright-await */
2+
import { render, waitFor } from "@testing-library/react";
3+
import { vi } from "vitest";
4+
5+
import WizardForm from "./WizardForm";
6+
7+
vi.mock("next/navigation", () => ({
8+
useRouter() {
9+
return { replace: vi.fn() };
10+
},
11+
useSearchParams() {
12+
return { get: vi.fn().mockReturnValue(currentStepNavigation) };
13+
},
14+
}));
15+
16+
const steps = [
17+
{
18+
title: "Step 1",
19+
description: "Description 1",
20+
content: <p data-testid="content-1">Step 1</p>,
21+
isEnabled: false,
22+
},
23+
{
24+
title: "Step 2",
25+
description: "Description 2",
26+
content: (setIsLoading: (value: boolean) => void) => (
27+
<button data-testid="content-2" onClick={() => setIsLoading(true)}>
28+
Test
29+
</button>
30+
),
31+
isEnabled: true,
32+
},
33+
{ title: "Step 3", description: "Description 3", content: <p data-testid="content-3">Step 3</p> },
34+
];
35+
36+
const props = {
37+
href: "/test/mock",
38+
steps: steps,
39+
nextLabel: "Next step",
40+
prevLabel: "Previous step",
41+
finishLabel: "Finish",
42+
};
43+
44+
let currentStepNavigation: number;
45+
46+
const renderComponent = (extraProps?: { disableNavigation: boolean }) =>
47+
render(<WizardForm {...props} {...extraProps} />);
48+
49+
describe("Tests for WizardForm component", () => {
50+
test("Should handle all the steps correctly", async () => {
51+
currentStepNavigation = 1;
52+
const { queryByTestId, queryByText, rerender } = renderComponent();
53+
const { prevLabel, nextLabel, finishLabel } = props;
54+
const stepInfo = {
55+
title: queryByTestId("step-title"),
56+
description: queryByTestId("step-description"),
57+
};
58+
59+
await waitFor(() => {
60+
steps.forEach((step, index) => {
61+
rerender(<WizardForm {...props} />);
62+
63+
const { title, description } = step;
64+
const buttons = {
65+
prev: queryByText(prevLabel),
66+
next: queryByText(nextLabel),
67+
finish: queryByText(finishLabel),
68+
};
69+
70+
expect(stepInfo.title).toHaveTextContent(title);
71+
expect(stepInfo.description).toHaveTextContent(description);
72+
73+
if (index === 0) {
74+
// case of first step
75+
expect(buttons.prev && buttons.finish).not.toBeInTheDocument();
76+
expect(buttons.next).toBeInTheDocument();
77+
} else if (index === steps.length - 1) {
78+
// case of last step
79+
expect(buttons.prev && buttons.finish).toBeInTheDocument();
80+
expect(buttons.next).not.toBeInTheDocument();
81+
} else {
82+
// case of in-between steps
83+
expect(buttons.prev && buttons.next).toBeInTheDocument();
84+
expect(buttons.finish).not.toBeInTheDocument();
85+
}
86+
87+
currentStepNavigation++;
88+
});
89+
});
90+
});
91+
92+
describe("Should handle the visibility of the content", async () => {
93+
test("Should render JSX content correctly", async () => {
94+
currentStepNavigation = 1;
95+
const { getByTestId, getByText } = renderComponent();
96+
const currentStep = steps[0];
97+
98+
expect(getByTestId("content-1")).toBeInTheDocument();
99+
expect(getByText(currentStep.title && currentStep.description)).toBeInTheDocument();
100+
});
101+
102+
test("Should render function content correctly", async () => {
103+
currentStepNavigation = 2;
104+
const { getByTestId, getByText } = renderComponent();
105+
const currentStep = steps[1];
106+
107+
expect(getByTestId("content-2")).toBeInTheDocument();
108+
expect(getByText(currentStep.title && currentStep.description)).toBeInTheDocument();
109+
});
110+
});
111+
112+
test("Should disable 'Next step' button if current step navigation is not enabled", async () => {
113+
currentStepNavigation = 1;
114+
const { nextLabel } = props;
115+
const { getByText } = renderComponent();
116+
117+
expect(getByText(nextLabel)).toBeDisabled();
118+
});
119+
120+
test("Should handle when navigation is disabled", async () => {
121+
const { queryByText, queryByTestId } = renderComponent({ disableNavigation: true });
122+
const { prevLabel, nextLabel, finishLabel } = props;
123+
const stepComponent = queryByTestId("wizard-step-component");
124+
const stepInfo = {
125+
title: queryByTestId("step-title"),
126+
description: queryByTestId("step-description"),
127+
};
128+
const buttons = {
129+
prev: queryByText(prevLabel),
130+
next: queryByText(nextLabel),
131+
finish: queryByText(finishLabel),
132+
};
133+
134+
expect(stepInfo.title && stepInfo.description).toBeInTheDocument();
135+
expect(stepComponent).not.toBeInTheDocument();
136+
expect(buttons.prev && buttons.next && buttons.finish).not.toBeInTheDocument();
137+
});
138+
});

0 commit comments

Comments
 (0)