-
Notifications
You must be signed in to change notification settings - Fork 251
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
Provide a convenient way to select the whole text with type() #1043
Comments
I'd rather not add any options to The magic happening when these options are used is not obvious to many users and isn't in line with the philosophy to simulate a user interaction. The await user.tripleClick(element) // selects a whole line - in <input/> this is everything
await user.keyboard('somethingNew') |
That makes sense, but presumably that can't be combined with The description for type says
Is it no longer a goal to have a convenient way to suffinctly fill in a form? |
Without the options, await user.click(element)
await user.keyboard('foo') If that's what you want, you can use If you use To me await user.type(element, 'foo', {initialSelectionStart: 0, initialSelectionEnd: 1000}) doesn't seem more convenient than await user.tripleClick(element)
await user.keyboard('foo') But the latter is unambiguous regarding the events that should happen. |
I see what you're saying - I think my mental model was that the convenience helpers are a way of succinctly expressing what the user is doing at a higher level. My app doesn't care that they use triple-click to select the text, the user interaction I care about is that the user replaces the content of the text box with new text. I can write a wrapper for my own tests but that ends up being quite specific to my own code. |
Would a |
Yeah, something specifically intended to replace a whole textbox would be great! 👍 |
I created my own custom // custom-user-events.ts
import {
UserEvent,
UserEventApi,
} from "@testing-library/user-event/dist/types/setup/setup";
type CustomUserEvents = {
clearAndType: (
...args: Parameters<UserEventApi["type"]>
) => ReturnType<UserEventApi["type"]>;
};
export async function clearAndType(
user: UserEvent,
...args: Parameters<CustomUserEvents["clearAndType"]>
): Promise<void> {
const [element] = args;
await user.clear(element);
await user.type(...args);
}
export default CustomUserEvents; // utils.tsx
import * as customUserEvents from "./custom-user-events";
import type CustomUserEventTypes from "./custom-user-events";
function setup(jsx) {
const baseUser = userEvent.setup();
const customUser = Object.entries(customUserEvents).reduce(
(accumulator, [key, customUserEvent]) => {
return {
...accumulator,
[key]: customUserEvent.bind(null, baseUser),
};
},
{}
) as CustomUserEventTypes;
const user = {
...baseUser,
...customUser,
};
return {
user,
...render(jsx),
};
} // foo-spec.tsx
import { setup } from './utils'
test('edits name', async () => {
const { user } = setup(<MyComponent />)
user.type(screen.getByLabelText("Name"), "Dwight Schrute")
user.clearAndType(screen.getByLabelText("Name"), "Michael Scott")
expect(screen.getByLabelText("Name")).toHaveValue("Michael Scott")
}) Works for me and can't really think of any reason why this would be a bad idea. Feels kind of similar to custom queries. |
Problem description
When using
.type()
I've been finding a common pattern that emerges is variations on the followingTo represent selecting the whole content and then overwriting it when typing
There was some related discussion in #755 (comment)
Suggested solution
Adding a new property like
selectAll
would be convenient, but probably opens up some awkward interactions ifinitialSelectionStart
orinitialSelectionEnd
are also supplied.From poking around in the code it looks like if I simply set a large value for
initialSelectionEnd
then it'll get clamped to the length of the input - which is basically what I want (although it's still quite verbose in a test). Perhaps the answer would be document the pattern of using a large number orInfinity
as the selection end?Additional context
No response
The text was updated successfully, but these errors were encountered: