-
Notifications
You must be signed in to change notification settings - Fork 468
Description
Describe the feature you'd like:
I've got a component like this:
export const Form: React.FC<Props> = props => (
<form onSubmit={props.onSubmit} onReset={props.onReset}>
{props.children}
<br />
<button type="submit">{props.submitText ?? 'Submit'}</button>
{props.onReset && <button type="reset">Reset</button>}
</form>
);
Currently in my tests I'm doing this:
describe('when the form is submitted', () => {
it('calls the submit handler once', () => {
render(<Form onSubmit={handleSubmit} />);
userEvent.click(screen.getByText('Submit'));
expect(handleSubmit).toHaveBeenCalledTimes(1);
});
});
As afaik that's the most efficient query I can use to get the "submit" button (without adding a test-id).
The issue is that it's dependent on the text of the button being Submit
- what I really want is to do getByRole('button', { type: 'submit' })
to select "a submit button".
Likewise this would be useful for the reset button which is in the same camp.
The only drawback I can think of here is a little increase in amount of code :)
Suggested implementation:
I've not looked at the code, but I'd expect this should be doable by checking if the node being checked has a type
attribute and if so compare its value to the type
option if present.
Describe alternatives you've considered:
- Adding a test-id to my element, which seems overkill.
- Matching against the text, which is what I'm currently doing and is fine, but I think it would be easy to make this a little less brittle.
- Using the
name
option ofgetByRole
, but that'd suffer from the same issue as above.
Teachability, Documentation, Adoption, Migration Strategy:
I imagine it'd be a matter of updating the current docs for the role queries to include this as a new option, with a similar format as the docs for the existing options such as checked
, selected
, etc.
I'm happy to help try and implement this :)