import React from "react"; import { fireEvent, render, screen } from "@testing-library/react-native"; import { InputPressable } from "."; import type { InputFieldWrapperProps } from "../InputFieldWrapper"; const MockInputFieldWrapper = jest.fn(); jest.mock("../InputFieldWrapper", () => ({ ...jest.requireActual("../InputFieldWrapper"), InputFieldWrapper: function Mock(props: InputFieldWrapperProps) { MockInputFieldWrapper(props); return jest.requireActual("../InputFieldWrapper").InputFieldWrapper(props); }, })); describe("InputPressable", () => { describe("InputFieldWrapper gets the expected props", () => { it("renders an invalid InputPressable", () => { const props = { invalid: true }; render(); expect(MockInputFieldWrapper).toHaveBeenCalledWith( expect.objectContaining(props), ); }); it("renders an invalid InputPressable with text", () => { const props = { invalid: "this test is invalid" }; render(); expect(MockInputFieldWrapper).toHaveBeenCalledWith( expect.objectContaining(props), ); }); it("renders a valid InputText with empty string", () => { const props = { invalid: "" }; render(); expect(MockInputFieldWrapper).toHaveBeenCalledWith( expect.objectContaining(props), ); }); it("renders a disabled InputPressable", () => { const props = { disabled: true }; render(); expect(MockInputFieldWrapper).toHaveBeenCalledWith( expect.objectContaining(props), ); }); it("renders an InputPressable with a placeholder", () => { const props = { placeholder: "test placeholder" }; render(); expect(MockInputFieldWrapper).toHaveBeenCalledWith( expect.objectContaining(props), ); }); }); it("renders an InputPressable with a value", () => { const value = "test value"; const { getByText } = render(); expect(getByText(value, { includeHiddenElements: true })).toBeTruthy(); }); it("renders a prefix label when specified", () => { const label = "test label"; const { getByText } = render( , ); expect(getByText(label)).toBeDefined(); }); it("renders a prefix icon when specified", () => { const { getByTestId } = render( , ); expect(getByTestId("calendar")).toBeDefined(); }); it("renders a suffix icon when specified", () => { const { getByTestId } = render( , ); expect(getByTestId("calendar")).toBeDefined(); }); it("calls onPress when pressed", () => { const onPressMock = jest.fn(); const a11yLabel = "test InputPressable"; const { getByLabelText } = render( , ); fireEvent.press(getByLabelText(a11yLabel)); expect(onPressMock).toHaveBeenCalledTimes(1); }); describe("when given a value", () => { const value = "A value"; it("renders an InputPressable with the value", () => { const { getByText } = render(); expect(getByText(value, { includeHiddenElements: true })).toBeDefined(); }); it("renders a prefix label when specified", () => { const { getByText } = render( , ); expect(getByText(value, { includeHiddenElements: true })).toBeDefined(); }); it("renders a suffix label when specified", () => { const { getByText } = render( , ); expect(getByText(value, { includeHiddenElements: true })).toBeDefined(); }); }); describe("showMiniLabel", () => { it("defaults to true", () => { const props = { placeholder: "placeholder", value: "value" }; render(); expect( screen.getByText("placeholder", { includeHiddenElements: true }), ).toBeDefined(); expect(MockInputFieldWrapper).toHaveBeenCalledWith( expect.objectContaining({ placeholderMode: "mini", }), ); }); describe("when true", () => { it("renders the placeholder in its normal position when the input has no value", () => { const props = { showMiniLabel: true, placeholder: "placeholder" }; render(); expect( screen.getByText("placeholder", { includeHiddenElements: true }), ).toBeDefined(); expect(MockInputFieldWrapper).toHaveBeenCalledWith( expect.objectContaining({ placeholderMode: "normal", }), ); }); it("renders the placeholder as a mini label when the input has a value", () => { const props = { showMiniLabel: true, placeholder: "placeholder", value: "value", }; render(); expect( screen.getByText("placeholder", { includeHiddenElements: true }), ).toBeDefined(); expect(MockInputFieldWrapper).toHaveBeenCalledWith( expect.objectContaining({ placeholderMode: "mini", }), ); }); }); describe("when false", () => { it("renders the placeholder in its normal position when the input has no value", () => { const props = { showMiniLabel: false, placeholder: "placeholder" }; render(); expect( screen.getByText("placeholder", { includeHiddenElements: true }), ).toBeDefined(); expect(MockInputFieldWrapper).toHaveBeenCalledWith( expect.objectContaining({ placeholderMode: "normal", }), ); }); it("does not render the placeholder when the input has a value", () => { const props = { showMiniLabel: false, placeholder: "placeholder", value: "value", }; render(); expect( screen.queryByText("placeholder", { includeHiddenElements: true }), ).toBeNull(); }); }); }); }); describe("accessibilityLabel", () => { it("uses accessibilityLabel if specified", () => { const { getByLabelText } = render( , ); expect(getByLabelText("accessibilityLabel")).toBeTruthy(); }); it("uses placeholder if unspecified", () => { const { getByLabelText } = render( , ); expect(getByLabelText("placeholder")).toBeTruthy(); }); });