Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(input): input interaction tests #4579

Open
wants to merge 3 commits into
base: canary
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 81 additions & 5 deletions packages/components/input/__tests__/input.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,94 @@ describe("Input", () => {

const {container} = render(<Input label="test input" onFocus={onFocus} />);

container.querySelector("input")?.focus();
container.querySelector("input")?.blur();
act(() => {
container.querySelector("input")?.focus();
});
act(() => {
container.querySelector("input")?.blur();
});

expect(onFocus).toHaveBeenCalledTimes(1);
});

it("should work with keyboard input", async () => {
const {getByTestId} = render(<Input data-testid="input" />);

const input = getByTestId("input") as HTMLInputElement;

const user = userEvent.setup();

act(() => {
input.focus();
});
expect(input.value).toBe("");

await user.keyboard("Hello World!");
expect(input.value).toBe("Hello World!");

await user.keyboard("[Backspace][Backspace]");
expect(input.value).toBe("Hello Worl");

await user.keyboard("[ArrowLeft][Delete]");
expect(input.value).toBe("Hello Wor");
});

it("should highlight text with user multi-clicks", async () => {
const {getByTestId} = render(<Input data-testid="input" defaultValue="Hello World!" />);

const input = getByTestId("input") as HTMLInputElement;

const user = userEvent.setup();

expect(input.value).toBe("Hello World!");

// in react testing library, input dblClick selects the word/symbol, tripleClick selects the entire text
await user.tripleClick(input);
await user.keyboard("Goodbye World!");
expect(input.value).toBe("Goodbye World!");

await user.tripleClick(input);
await user.keyboard("[Delete]");
expect(input.value).toBe("");
});

it("should focus input on click", async () => {
const {getByTestId} = render(<Input data-testid="input" />);

const input = getByTestId("input") as HTMLInputElement;
const innerWrapper = document.querySelector("[data-slot='inner-wrapper']") as HTMLDivElement;
const inputWrapper = document.querySelector("[data-slot='input-wrapper']") as HTMLDivElement;

const user = userEvent.setup();

expect(document.activeElement).not.toBe(input);

await user.click(input);
expect(document.activeElement).toBe(input);
act(() => {
input.blur();
});
expect(document.activeElement).not.toBe(input);

await user.click(innerWrapper);
expect(document.activeElement).toBe(input);
act(() => {
input.blur();
});
expect(document.activeElement).not.toBe(input);

await user.click(inputWrapper);
expect(document.activeElement).toBe(input);
act(() => {
input.blur();
});
expect(document.activeElement).not.toBe(input);
});

it("ref should update the value", () => {
const ref = React.createRef<HTMLInputElement>();

const {container} = render(<Input ref={ref} type="text" />);
render(<Input ref={ref} type="text" />);

if (!ref.current) {
throw new Error("ref is null");
Expand All @@ -138,8 +216,6 @@ describe("Input", () => {

ref.current!.value = value;

container.querySelector("input")?.focus();
Copy link
Contributor Author

@Peterl561 Peterl561 Jan 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is a little dubious since it only passed because the state change caused by .focus() didn't happen until after the test finished as it wasn't wrapped by act. Any interaction with the input will reset input.value.


expect(ref.current?.value)?.toBe(value);
});

Expand Down
Loading