Skip to content

Colota - Android GPS Location tracker

2.6 KB
Raw Blame
import React from "react"
import { render, fireEvent } from "@testing-library/react-native"
jest.mock("../../../hooks/useTheme", () => ({
  useTheme: () => ({ colors: require("@colota/shared").lightColors })
}))

import { NumericInput } from "../NumericInput"

describe("NumericInput", () => {
  const mockOnChange = jest.fn()
  const mockOnBlur = jest.fn()

  beforeEach(() => {
    jest.clearAllMocks()
  })

  function renderInput(overrides: Partial<React.ComponentProps<typeof NumericInput>> = {}) {
    return render(
      <NumericInput
        label="Test label"
        value="10"
        onChange={mockOnChange}
        onBlur={mockOnBlur}
        unit="seconds"
        {...overrides}
      />
    )
  }

  it("renders label, value, and unit", () => {
    const { getByText, getByDisplayValue } = renderInput()

    expect(getByText("Test label")).toBeTruthy()
    expect(getByDisplayValue("10")).toBeTruthy()
    expect(getByText("seconds")).toBeTruthy()
  })

  it("renders hint when provided", () => {
    const { getByText } = renderInput({ hint: "Enter a number" })

    expect(getByText("Enter a number")).toBeTruthy()
  })

  it("does not render hint when not provided", () => {
    const { queryByText } = renderInput()

    expect(queryByText("Enter a number")).toBeNull()
  })

  it("calls onChange when text changes", () => {
    const { getByDisplayValue } = renderInput()

    fireEvent.changeText(getByDisplayValue("10"), "25")

    expect(mockOnChange).toHaveBeenCalledWith("25")
  })

  it("calls onBlur when input loses focus", () => {
    const { getByDisplayValue } = renderInput()

    fireEvent(getByDisplayValue("10"), "blur")

    expect(mockOnBlur).toHaveBeenCalled()
  })

  it("uses numeric keyboard type", () => {
    const { getByDisplayValue } = renderInput()

    const input = getByDisplayValue("10")
    expect(input.props.keyboardType).toBe("numeric")
  })

  it("displays placeholder text", () => {
    const { getByPlaceholderText } = renderInput({
      value: "",
      placeholder: "50"
    })

    expect(getByPlaceholderText("50")).toBeTruthy()
  })

  it("renders different units correctly", () => {
    const { getByText } = renderInput({ unit: "meters" })

    expect(getByText("meters")).toBeTruthy()
  })

  it("hands an error to the box and a message under it, so a clamp and a rejection read in place", () => {
    const withError = renderInput({ error: "A whole number" })
    expect(withError.getByText("A whole number")).toBeTruthy()
    expect(withError.getByLabelText("Test label, A whole number")).toBeTruthy()

    const withMessage = renderInput({ message: "Set to 1 s" })
    expect(withMessage.getByText("Set to 1 s")).toBeTruthy()
  })
})