From 81c11f61dc942a6feba7d586cf97a0a101bb3644 Mon Sep 17 00:00:00 2001 From: "GitStart-Cal.com" <121884634+gitstart-calcom@users.noreply.github.com> Date: Tue, 1 Aug 2023 17:19:54 +0100 Subject: [PATCH] test: Create unit tests for react components in packages/ui/components/form/input (#10506) Co-authored-by: gitstart-calcom --- packages/ui/components/form/inputs/Input.tsx | 2 +- .../ui/components/form/inputs/input.test.tsx | 188 ++++++++++++++++++ 2 files changed, 189 insertions(+), 1 deletion(-) create mode 100644 packages/ui/components/form/inputs/input.test.tsx diff --git a/packages/ui/components/form/inputs/Input.tsx b/packages/ui/components/form/inputs/Input.tsx index af66eb1ff7..5a5619df18 100644 --- a/packages/ui/components/form/inputs/Input.tsx +++ b/packages/ui/components/form/inputs/Input.tsx @@ -425,7 +425,7 @@ export const FilterSearchField = forwardRef(f dir="ltr" className="focus-within:ring-brand-default group relative mx-3 mb-1 mt-2.5 flex items-center rounded-md focus-within:outline-none focus-within:ring-2">
- +
{ + test("Should render correctly with label and placeholder", () => { + const { getByLabelText, getByPlaceholderText } = render( + + ); + + expect(getByLabelText("Test Label")).toBeInTheDocument(); + expect(getByPlaceholderText("Test Placeholder")).toBeInTheDocument(); + }); + + test("Should handle input correctly", () => { + const { getByRole } = render(); + const inputElement = getByRole("textbox") as HTMLInputElement; + + fireEvent.change(inputElement, { target: { value: "Hello" } }); + expect(onChangeMock).toHaveBeenCalledTimes(1); + expect(inputElement.value).toBe("Hello"); + }); + + it("should render with addOnLeading prop", () => { + const { getByText } = render(Leading} />); + + const addOnLeadingElement = getByText("Leading"); + expect(addOnLeadingElement).toBeInTheDocument(); + }); + + it("should render with addOnSuffix prop", () => { + const { getByText } = render(Suffix} />); + + const addOnSuffixElement = getByText("Suffix"); + expect(addOnSuffixElement).toBeInTheDocument(); + }); + + it("should display both addOnLeading and addOnSuffix", () => { + const { getByText } = render( + Leading} addOnSuffix={Suffix} /> + ); + + const addOnLeadingElement = getByText("Leading"); + const addOnSuffixElement = getByText("Suffix"); + + expect(addOnLeadingElement).toBeInTheDocument(); + expect(addOnSuffixElement).toBeInTheDocument(); + }); + + it("Should display error message when error prop is provided", () => { + const errorMessage = "This field is required"; + const { getByRole } = render(); + + const errorElement = getByRole("textbox"); + expect(errorElement).toHaveAttribute("error", errorMessage); + }); +}); + +describe("Tests for PasswordField Component", () => { + test("Should toggle password visibility correctly", () => { + const { getByLabelText, getByText } = render( + + + + ); + const passwordInput = getByLabelText("password") as HTMLInputElement; + const toggleButton = getByText("show_password"); + + expect(passwordInput.type).toBe("password"); + + fireEvent.click(toggleButton); + expect(passwordInput.type).toBe("text"); + + fireEvent.click(toggleButton); + expect(passwordInput.type).toBe("password"); + }); +}); + +describe("Tests for EmailField Component", () => { + test("Should render correctly with email-related attributes", () => { + const { getByRole } = render(); + const emailInput = getByRole("textbox"); + expect(emailInput).toHaveAttribute("type", "email"); + expect(emailInput).toHaveAttribute("autoCapitalize", "none"); + expect(emailInput).toHaveAttribute("autoComplete", "email"); + expect(emailInput).toHaveAttribute("autoCorrect", "off"); + expect(emailInput).toHaveAttribute("inputMode", "email"); + }); +}); + +describe("Tests for TextAreaField Component", () => { + test("Should render correctly with label and placeholder", () => { + const { getByText, getByPlaceholderText, getByRole } = render( + + ); + + expect(getByText("Test Label")).toBeInTheDocument(); + expect(getByPlaceholderText("Test Placeholder")).toBeInTheDocument(); + expect(getByRole("textbox")).toBeInTheDocument(); + }); + + test("Should handle input correctly", () => { + const { getByRole } = render(); + const textareaElement = getByRole("textbox") as HTMLInputElement; + + fireEvent.change(textareaElement, { target: { value: "Hello" } }); + expect(onChangeMock).toHaveBeenCalled(); + expect(textareaElement.value).toBe("Hello"); + }); +}); + +describe("Tests for InputFieldWithSelect Component", () => { + test("Should render correctly with InputField and UnstyledSelect", () => { + const onChangeMock = vi.fn(); + + const selectProps = { + value: null, + onChange: onChangeMock, + name: "testSelect", + options: [ + { value: "Option 1", label: "Option 1" }, + { value: "Option 2", label: "Option 2" }, + { value: "Option 3", label: "Option 3" }, + ], + } as unknown as typeof UnstyledSelect; + + const { getByText } = render(); + + const inputElement = getByText("Select..."); + fireEvent.mouseDown(inputElement); + + const optionElement = getByText("Option 1"); + expect(optionElement).toBeInTheDocument(); + }); +}); + +describe("Tests for NumberInput Component", () => { + test("Should render correctly with input type number", () => { + const { getByRole } = render(); + const numberInput = getByRole("spinbutton"); + + expect(numberInput).toBeInTheDocument(); + expect(numberInput).toHaveAttribute("type", "number"); + }); + + test("Should handle input correctly", () => { + const { getByRole } = render(); + const numberInput = getByRole("spinbutton") as HTMLInputElement; + + fireEvent.change(numberInput, { target: { value: "42" } }); + expect(onChangeMock).toHaveBeenCalled(); + expect(numberInput.value).toBe("42"); + }); +}); + +describe("Tests for FilterSearchField Component", () => { + test("Should render correctly with Search icon and input", () => { + const { getByRole, getByTestId } = render(); + const searchInput = getByRole("textbox"); + const searchIcon = getByTestId("search-icon"); + + expect(searchInput).toBeInTheDocument(); + expect(searchIcon).toBeInTheDocument(); + }); + + test("Should handle input correctly", () => { + const { getByRole } = render(); + const searchInput = getByRole("textbox") as HTMLInputElement; + + fireEvent.change(searchInput, { target: { value: "Test search" } }); + expect(onChangeMock).toHaveBeenCalled(); + expect(searchInput.value).toBe("Test search"); + }); +});