fleet/frontend/components/forms/fields/InputFieldWithIcon/InputFieldWithIcon.tests.tsx
jacobshandling 06c48216f7
UI: Add Tooltip show delay across app (#33091)
## For #31869

- Add fine grain controls for tooltip show and hide delay behavior
- Default to 250ms show delay across app
- Update ~30 unit tests to expect new delay
- See
[note](https://github.com/fleetdm/fleet/issues/31869#issuecomment-3300660487)


https://github.com/user-attachments/assets/5969e0f7-c137-491f-8430-6f21d01b9350

- [x] Changes file added for user-visible changes in `changes/`
- [x] QA'd all new/changed functionality manually

---------

Co-authored-by: Jacob Shandling <jacob@fleetdm.com>
2025-09-18 09:42:30 -07:00

132 lines
3.3 KiB
TypeScript

import React from "react";
import { render, screen, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { renderWithSetup } from "test/test-utils";
// @ts-ignore
import InputFieldWithIcon from "./InputFieldWithIcon";
describe("InputFieldWithIcon Component", () => {
const mockOnChange = jest.fn();
beforeEach(() => {
jest.clearAllMocks();
});
test("renders with label and placeholder", () => {
render(
<InputFieldWithIcon
value=""
onChange={mockOnChange}
label="Test Input"
placeholder="Enter text"
name="test-input"
/>
);
expect(screen.getByText(/test input/i)).toBeInTheDocument();
expect(screen.getByPlaceholderText(/enter text/i)).toBeInTheDocument();
});
test("calls onChange when input value changes", async () => {
render(
<InputFieldWithIcon
value=""
onChange={mockOnChange}
label="Test Input"
placeholder="Enter text"
name="test-input"
/>
);
// Change the input value
await userEvent.type(
screen.getByPlaceholderText(/enter text/i),
"New Value"
);
expect(mockOnChange).toHaveBeenCalledTimes(9); // 'New Value' has 9 characters
});
test("renders help text when provided", () => {
render(
<InputFieldWithIcon
value=""
onChange={mockOnChange}
label="Test Input"
placeholder="Enter text"
name="test-input"
helpText="This is a help text."
/>
);
expect(screen.getByText(/this is a help text/i)).toBeInTheDocument();
});
test("renders error message when provided", () => {
render(
<InputFieldWithIcon
value=""
onChange={mockOnChange}
label="Test Input"
placeholder="Enter text"
name="test-input"
error="This is an error message."
/>
);
expect(screen.getByText(/this is an error message/i)).toBeInTheDocument();
});
test("renders clear button when clearButton is true and input has value", () => {
render(
<InputFieldWithIcon
value="Some Value"
onChange={mockOnChange}
label="Test Input"
placeholder="Enter text"
name="test-input"
clearButton
/>
);
expect(screen.getByRole("button")).toBeInTheDocument();
});
test("clears input value when clear button is clicked", async () => {
render(
<InputFieldWithIcon
value="Some Value"
onChange={mockOnChange}
label="Test Input"
placeholder="Enter text"
name="test-input"
clearButton
/>
);
// Click the clear button
await userEvent.click(screen.getByRole("button"));
expect(mockOnChange).toHaveBeenCalledTimes(1);
expect(mockOnChange).toHaveBeenCalledWith("");
});
test("renders tooltip when provided", async () => {
const { user } = renderWithSetup(
<InputFieldWithIcon
value=""
onChange={mockOnChange}
label="Test Input"
placeholder="Enter text"
name="test-input"
tooltip="This is a tooltip."
/>
);
await user.hover(screen.getByText(/test input/i));
await waitFor(() => {
const tooltip = screen.getByText("This is a tooltip.");
expect(tooltip).toBeInTheDocument();
});
});
});