import React from "react"; import { fireEvent, render, waitFor } from "@testing-library/react-native"; import { FormProvider, useForm } from "react-hook-form"; import { Keyboard } from "react-native"; import { InputDate } from "./InputDate"; import { Button } from "../Button"; import * as atlantisContext from "../AtlantisContext/AtlantisContext"; const keyboardDismissSpy = jest.spyOn(Keyboard, "dismiss"); describe("InputDate", () => { describe("Visuals", () => { const placeholder = "Start time"; const expectedDate = "May 30, 2022"; const value = new Date(2022, 4, 30); const handleChange = jest.fn(); const setup = () => render( , ); it("should show a calendar prefix icon", () => { const screen = setup(); const calendarIcon = screen.getByTestId("calendar"); expect(calendarIcon).toBeDefined(); expect(calendarIcon.type).toBe("RNSVGSvgView"); }); it("should show a formatted date", () => { const screen = setup(); expect( screen.getByText(expectedDate, { includeHiddenElements: true }), ).toBeDefined(); }); it("should be clearable when there's a value", () => { const screen = setup(); const clearAction = screen.getByLabelText("Clear input"); expect(clearAction).toBeDefined(); fireEvent.press(clearAction); expect(handleChange).toHaveBeenCalledWith(undefined); }); }); describe("String value", () => { it("should show a formatted date", () => { const expectedDate = "May 31, 2022"; const value = new Date(2022, 4, 31).toISOString(); const screen = render(); expect( screen.getByText(expectedDate, { includeHiddenElements: true }), ).toBeDefined(); }); }); describe("With emptyValueLabel", () => { it("should show the emptyValueLabel when there's no value", () => { const label = "Unscheduled"; const screen = render(); expect( screen.getByText(label, { includeHiddenElements: true }), ).toBeDefined(); expect(screen.queryByLabelText("Clear input")).toBeNull(); }); it("should not show the emptyValueLabel when there's a value", () => { const label = "Unscheduled"; const screen = render( , ); expect(screen.queryByText(label)).toBeNull(); expect(screen.getByLabelText("Clear input")).toBeDefined(); }); }); describe("with a defaultValue", () => { it("renders the supplied value", () => { const { getByText } = render( , ); expect( getByText("Jan 17, 2022", { includeHiddenElements: true }), ).toBeDefined(); }); }); describe("Date picker", () => { const placeholder = "Tap me"; const handleChange = jest.fn(); function renderDatePicker() { const screen = render( , ); fireEvent.press(screen.getByLabelText(placeholder)); expect(screen.getByTestId("inputDate-datePicker")).toBeDefined(); return screen; } it("should not show a date picker", () => { const screen = render(); expect(screen.queryByTestId("inputDate-datePicker")).toBeNull(); }); it("should fire the onChange with the current value after canceling a date selection", () => { const screen = renderDatePicker(); fireEvent.press(screen.getByLabelText("Cancel")); expect(handleChange).toHaveBeenCalledWith(undefined); }); it("should fire the onChange after confirming a date selection", () => { const screen = renderDatePicker(); fireEvent.press(screen.getByLabelText("Confirm")); expect(handleChange).toHaveBeenCalledWith(expect.any(Date)); }); it("should dismiss the keyboard when the date picker is opened", () => { renderDatePicker(); expect(keyboardDismissSpy).toHaveBeenCalled(); }); }); const mockOnSubmit = jest.fn(); const saveButtonText = "Submit"; const requiredError = "This is required"; function SimpleFormWithProvider({ children, defaultValues, }: { readonly children: React.ReactNode; readonly defaultValues?: Record; }) { const formMethods = useForm({ reValidateMode: "onChange", defaultValues, mode: "onTouched", }); return ( {children}