|
| 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