Skip to content

Commit e77de2b

Browse files
Peterl561wingkwong
andauthored
test(input): input interaction tests (#4579)
* test(input): user interaction tests * test(input): missing act wrappers --------- Co-authored-by: WK Wong <[email protected]>
1 parent 475b2ff commit e77de2b

File tree

1 file changed

+81
-5
lines changed

1 file changed

+81
-5
lines changed

packages/components/input/__tests__/input.test.tsx

+81-5
Original file line numberDiff line numberDiff line change
@@ -120,16 +120,94 @@ describe("Input", () => {
120120

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

123-
container.querySelector("input")?.focus();
124-
container.querySelector("input")?.blur();
123+
act(() => {
124+
container.querySelector("input")?.focus();
125+
});
126+
act(() => {
127+
container.querySelector("input")?.blur();
128+
});
125129

126130
expect(onFocus).toHaveBeenCalledTimes(1);
127131
});
128132

133+
it("should work with keyboard input", async () => {
134+
const {getByTestId} = render(<Input data-testid="input" />);
135+
136+
const input = getByTestId("input") as HTMLInputElement;
137+
138+
const user = userEvent.setup();
139+
140+
act(() => {
141+
input.focus();
142+
});
143+
expect(input.value).toBe("");
144+
145+
await user.keyboard("Hello World!");
146+
expect(input.value).toBe("Hello World!");
147+
148+
await user.keyboard("[Backspace][Backspace]");
149+
expect(input.value).toBe("Hello Worl");
150+
151+
await user.keyboard("[ArrowLeft][Delete]");
152+
expect(input.value).toBe("Hello Wor");
153+
});
154+
155+
it("should highlight text with user multi-clicks", async () => {
156+
const {getByTestId} = render(<Input data-testid="input" defaultValue="Hello World!" />);
157+
158+
const input = getByTestId("input") as HTMLInputElement;
159+
160+
const user = userEvent.setup();
161+
162+
expect(input.value).toBe("Hello World!");
163+
164+
// in react testing library, input dblClick selects the word/symbol, tripleClick selects the entire text
165+
await user.tripleClick(input);
166+
await user.keyboard("Goodbye World!");
167+
expect(input.value).toBe("Goodbye World!");
168+
169+
await user.tripleClick(input);
170+
await user.keyboard("[Delete]");
171+
expect(input.value).toBe("");
172+
});
173+
174+
it("should focus input on click", async () => {
175+
const {getByTestId} = render(<Input data-testid="input" />);
176+
177+
const input = getByTestId("input") as HTMLInputElement;
178+
const innerWrapper = document.querySelector("[data-slot='inner-wrapper']") as HTMLDivElement;
179+
const inputWrapper = document.querySelector("[data-slot='input-wrapper']") as HTMLDivElement;
180+
181+
const user = userEvent.setup();
182+
183+
expect(document.activeElement).not.toBe(input);
184+
185+
await user.click(input);
186+
expect(document.activeElement).toBe(input);
187+
act(() => {
188+
input.blur();
189+
});
190+
expect(document.activeElement).not.toBe(input);
191+
192+
await user.click(innerWrapper);
193+
expect(document.activeElement).toBe(input);
194+
act(() => {
195+
input.blur();
196+
});
197+
expect(document.activeElement).not.toBe(input);
198+
199+
await user.click(inputWrapper);
200+
expect(document.activeElement).toBe(input);
201+
act(() => {
202+
input.blur();
203+
});
204+
expect(document.activeElement).not.toBe(input);
205+
});
206+
129207
it("ref should update the value", () => {
130208
const ref = React.createRef<HTMLInputElement>();
131209

132-
const {container} = render(<Input ref={ref} type="text" />);
210+
render(<Input ref={ref} type="text" />);
133211

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

139217
ref.current!.value = value;
140218

141-
container.querySelector("input")?.focus();
142-
143219
expect(ref.current?.value)?.toBe(value);
144220
});
145221

0 commit comments

Comments
 (0)