Colota - Android GPS Location tracker
import React from "react"
import { render, fireEvent } from "@testing-library/react-native"
import { StyleSheet } from "react-native"
import { lightColors } from "@colota/shared"
jest.mock("../../../hooks/useTheme", () => ({
useTheme: () => ({ colors: require("@colota/shared").lightColors })
}))
import { TextField, SelectField } from "../TextField"
const flat = (el: any) => StyleSheet.flatten(el.props.style)
describe("TextField", () => {
it("keeps the text a constant distance from the edge as the border grows", () => {
// Focus takes the border from 1 to 2. Without the matching padding drop the caret and
// every character shift a pixel sideways the moment you tap in.
const { getByTestId } = render(<TextField label="Server address" testID="server-input" />)
const rest = flat(getByTestId("server-input-box"))
expect(rest.borderWidth + rest.paddingHorizontal).toBe(15)
fireEvent(getByTestId("server-input"), "focus")
const focused = flat(getByTestId("server-input-box"))
expect(focused.borderWidth).toBe(2)
expect(focused.borderWidth + focused.paddingHorizontal).toBe(15)
})
it("rests without a ring and grows one on focus and on error", () => {
// A border marks state, so the resting field is a fill. The ring's width is what the box
// gives back in padding, or the text would jump sideways as you tap in.
const { getByTestId, rerender } = render(<TextField label="Server address" testID="server-input" />)
expect(flat(getByTestId("server-input-box")).borderWidth).toBe(0)
fireEvent(getByTestId("server-input"), "focus")
expect(flat(getByTestId("server-input-box")).borderWidth).toBe(2)
expect(flat(getByTestId("server-input-box")).borderColor).toBe(lightColors.primary)
rerender(<TextField label="Server address" testID="server-input" error="Public hosts need https" />)
expect(flat(getByTestId("server-input-box")).borderColor).toBe(lightColors.error)
})
it("can be asked for a resting outline, which it does not draw otherwise", () => {
const { getByTestId } = render(<TextField label="Server address" testID="server-input" border />)
const style = flat(getByTestId("server-input-box"))
expect(style.borderWidth).toBe(1)
expect(style.borderColor).toBe(lightColors.border)
})
it("reads the error out with the field name, because the ring alone is colour", () => {
const { getByTestId, getByText } = render(
<TextField label="Password" testID="pw-input" error="At least 8 characters" />
)
expect(getByTestId("pw-input").props.accessibilityLabel).toBe("Password, At least 8 characters")
expect(getByText("At least 8 characters")).toBeTruthy()
})
it("stops taking input and drops its ink when disabled", () => {
const { getByTestId } = render(<TextField label="Server address" testID="server-input" disabled />)
const input = getByTestId("server-input")
expect(input.props.editable).toBe(false)
expect(flat(input).color).toBe(lightColors.textDisabled)
expect(flat(getByTestId("server-input-box")).borderWidth).toBe(0)
})
it("passes keyboard and autofill props through to the input", () => {
// The union type lets a field skip the visible label only when a row already names it.
const { getByTestId } = render(
<TextField accessibilityLabel="Port" testID="port-input" keyboardType="numeric" autoComplete="off" />
)
const input = getByTestId("port-input")
expect(input.props.keyboardType).toBe("numeric")
expect(input.props.autoComplete).toBe("off")
expect(input.props.accessibilityLabel).toBe("Port")
})
it("reveals a secure field only while the eye is held", () => {
// Hold, not toggle: the password cannot be left on screen by a stray tap.
const { getByTestId } = render(<TextField label="Password" testID="pw-input" secure />)
expect(getByTestId("pw-input").props.secureTextEntry).toBe(true)
fireEvent(getByTestId("pw-input-reveal"), "pressIn")
expect(getByTestId("pw-input").props.secureTextEntry).toBe(false)
fireEvent(getByTestId("pw-input-reveal"), "pressOut")
expect(getByTestId("pw-input").props.secureTextEntry).toBe(true)
})
it("lifts the label to primary on focus, so the field you are in is named", () => {
const { getByTestId, getByText } = render(<TextField label="Server address" testID="server-input" />)
expect(flat(getByText("Server address")).color).toBe(lightColors.textSecondary)
fireEvent(getByTestId("server-input"), "focus")
expect(flat(getByText("Server address")).color).toBe(lightColors.primary)
})
it("sets an endpoint in monospace so a typo in a URL is findable", () => {
const { getByTestId } = render(<TextField label="Server address" testID="server-input" mono />)
expect(flat(getByTestId("server-input")).fontFamily).toBe("monospace")
})
it("recesses into its card rather than punching through to the ground behind it", () => {
// The fill was colors.background, so a field cut a hole the colour of the screen behind
// its card and a card holding fields read as a different tone from one that did not.
const { getByTestId } = render(<TextField label="Server address" testID="server-input" />)
expect(flat(getByTestId("server-input-box")).backgroundColor).toBe(lightColors.well)
expect(flat(getByTestId("server-input-box")).backgroundColor).not.toBe(lightColors.background)
})
})
describe("SelectField", () => {
it("shows the picked value, or the placeholder until there is one, and opens the picker", () => {
const onPress = jest.fn()
const { getByText, getByTestId, rerender } = render(
<SelectField label="Device" placeholder="Pick a device" onPress={onPress} testID="device" />
)
expect(getByText("Pick a device")).toBeTruthy()
rerender(
<SelectField label="Device" value="My Car" placeholder="Pick a device" onPress={onPress} testID="device" />
)
expect(getByText("My Car")).toBeTruthy()
fireEvent.press(getByTestId("device"))
expect(onPress).toHaveBeenCalled()
})
// Same rule as TextField: the ring marks the error and the padding gives its width back.
it("grows the error ring without moving the value and names the error to a screen reader", () => {
const { getByTestId, getByText } = render(
<SelectField
label="Device"
placeholder="Pick a device"
error="Pick a device first"
onPress={() => {}}
testID="device"
/>
)
expect(flat(getByTestId("device")).paddingHorizontal + 2).toBe(15)
expect(getByText("Pick a device first")).toBeTruthy()
expect(getByTestId("device").props.accessibilityLabel).toBe("Device, Pick a device, Pick a device first")
})
})