Skip to content

feat: ...byRole type queries accepts second arg #875

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

Closed
wants to merge 17 commits into from

Conversation

kiranjd
Copy link
Contributor

@kiranjd kiranjd commented Dec 2, 2021

Closes #827

  • accepts a 'name' property to refine query

Summary

Test plan

@kiranjd kiranjd force-pushed the feat/by-role-second-arg branch from ca7b4c7 to ea68aa1 Compare December 2, 2021 15:01
Copy link
Member

@thymikee thymikee left a comment

Choose a reason for hiding this comment

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

Looks like it's a good direction. Please take a look at 31ef0b9 to see how to resolve API changes in a backward compatible way

@AugustinLF
Copy link
Collaborator

@kiranjd is it something you're still interested in getting in? This is a feature that is clearly lacking for our current API, and I'd love to help you getting that in a mergeable state.

@kiranjd
Copy link
Contributor Author

kiranjd commented Mar 28, 2022

@AugustinLF Missed following up on this. I remember I had a few questions. I'll post the questions and follow it up to finish within this week. Thank you 🙏

import type { WaitForOptions } from '../waitFor';
import {
ErrorWithStack,
prepareErrorMessage,
createQueryByError,
} from './errors';

type QueryOptions = {
Copy link
Contributor Author

@kiranjd kiranjd Mar 30, 2022

Choose a reason for hiding this comment

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

The name in QueryOptions type for getBy is optional. But, it's a must for the function, filterWithName. Is this duplication that I get rid of or is this okay?
(I haven't worked with types all that much 😬 )

- adds tests for deprecations
- change second param on findBy queries
@kiranjd kiranjd marked this pull request as ready for review March 30, 2022 11:54
@kiranjd kiranjd force-pushed the feat/by-role-second-arg branch from 291ebcc to a5f168c Compare March 30, 2022 13:35
'interval',
'stackTraceError',
];
const warnDeprectedWaitForOptionsUsage = (queryOptions?: WaitForOptions) => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
const warnDeprectedWaitForOptionsUsage = (queryOptions?: WaitForOptions) => {
const warnDeprecatedWaitForOptionsUsage = (queryOptions?: WaitForOptions) => {

Copy link
Member

Choose a reason for hiding this comment

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

Also pls move it to helpers/errors.ts, as this looks like reusable function that can be used with other findByXxx queries if they decide to go with query options.

@AugustinLF
Copy link
Collaborator

@kiranjd Do you need any help, is there anything I can do to help you getting that in? :)

@@ -19,6 +47,10 @@ type QueryAllReturn = Array<ReactTestInstance>;
type FindReturn = Promise<GetReturn>;
type FindAllReturn = Promise<GetAllReturn>;

export type QueryOptions = {
Copy link
Member

Choose a reason for hiding this comment

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

This type should be renamed to ByRoleOptions bacause this type of option is only relevant to byRole queries.

isNodeValid(node) && matcherFn(node.props[name], matcher);

return (
matchesRole && !!getQueriesForElement(node).queryByText(options.name)
Copy link
Member

Choose a reason for hiding this comment

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

it looks like we should match for either queryByText(options.name) or queryByLabelText(options.name)

@@ -31,8 +65,27 @@ const makeA11yQuery = <P extends unknown, M extends unknown>(
queryNames: QueryNames,
matcherFn: (prop: P, value: M) => boolean
) => (instance: ReactTestInstance) => {
const getBy = (matcher: M) => {
const filterWithName = (
Copy link
Member

Choose a reason for hiding this comment

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

Suggested name filterByAccessibilityName

@mdjastrzebski
Copy link
Member

Let's pause this PR for couple of days till we get a11y query reorganisation from #325 done. This should allow for easy separate query options type per each query, so that the name options will be allowed only for byRole queries and not all a11y queries.

@kiranjd
Copy link
Contributor Author

kiranjd commented May 5, 2022 via email

@kiranjd
Copy link
Contributor Author

kiranjd commented Jul 25, 2022

@mdjastrzebski is this a good time to take this PR further? If so, should I raise a separate PR to continue the work?

@AugustinLF
Copy link
Collaborator

@kiranjd I feel like yes we should be able to start working on that once again. I'd however wait for the go from @mdjastrzebski who might have something else planned before that. Nonetheless, feel free to let me know if you need any help on that, I'm very keen in getting this one in.

@mdjastrzebski
Copy link
Member

@kiranjd go ahead 👍🏻 Now is the right time to work on this PR, as we've got a11y properly refactored.

@AugustinLF
Copy link
Collaborator

AugustinLF commented Aug 25, 2022

@kiranjd is there anything we can do to help you to get that in? It'd bring so much value, I'm really looking to get that one in <3

@kiranjd
Copy link
Contributor Author

kiranjd commented Aug 25, 2022

@AugustinLF Thanks for the support. I will work on this and hopefully get this in a mergeable state by this weekend 🤞🏻

@AugustinLF
Copy link
Collaborator

@kiranjd if you're short on time to finish this PR I can definitely help fixing the last few things to get it merged, let me know if you want help :)

@kiranjd
Copy link
Contributor Author

kiranjd commented Sep 15, 2022

@AugustinLF Have tried to redo the changes from the last state when it was working. There seems to be a lot of refactoring undergone in the meanwhile.
I would really appreciate your help in taking this further. Thank you for understanding :)

@mdjastrzebski
Copy link
Member

@kiranjd yeah RNTL codebase did move a lot since the original PR submission :-)

@AugustinLF I would be great if you could help to move this forward, as byRole query with name option has a huge potential to be one of the top recommended queries as in the case of RTL: https://kentcdodds.com/blog/common-mistakes-with-react-testing-library#not-using-byrole-most-of-the-time

@mdjastrzebski
Copy link
Member

This PR has been rebased and updated by @AugustinLF as #1127. Thank you @kiranjd 👏🏻 for the initial implementation!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

getByRole should accept a second argument to refine query as in react-testing-library
4 participants