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

Migrate ArticlePagination to RTL. #824

Merged
merged 2 commits into from
Nov 14, 2022

Conversation

huwshimi
Copy link
Collaborator

Done

  • Migrate ArticlePagination to React Testing Library to prepare for React 18.

QA

n/a

@webteam-app
Copy link

Demo starting at https://react-components-824.demos.haus

<ArticlePagination
nextLabel="Consectetur adipisicing elit"
nextURL="#next"
previousLabel="Lorem ipsum dolor sit amet"
previousURL="#previous"
/>
);
expect(wrapper).toMatchSnapshot();
expect(screen.getByRole("contentinfo")).toMatchSnapshot();
Copy link
Member

Choose a reason for hiding this comment

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

One thing that I don't like about RTL is selectors like this.
How do I know that "ArticlePagination" is has contentinfo role? It's nowhere in the component code. My guess is it has this role because it's a "footer" element. But using querySelector("footer") would at least be explicit about it. In here we are pretending to be using semantic selector (because RTE forces us to it), but all we have is an indirect node query with confusing role that no one will know where it comes from.

Maybe for the cases like this (when we need root element of the component) we should have some testing ID convention in place? So all components will have some testing id on root element?

Thoughts @huwshimi @petermakowski ?

Copy link
Member

Choose a reason for hiding this comment

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

Seems like there is a way to get to the container element at least: https://testing-library.com/docs/react-testing-library/api/#container-1

so when doing snapshots maybe we could use that instead of getting root element via implicit role?

const { container } = render(…);
expect(container).toMatchSnapshot();

Still, it won't solve the problem if we really want to access the root element of the component (and check class names on it or something).

Copy link
Contributor

Choose a reason for hiding this comment

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

In here we are pretending to be using semantic selector (because RTE forces us to it), but all we have is an indirect node query with confusing role that no one will know where it comes from.

Testing library operates on the same roles that are exposed to the user. You can check the role of an element by using logRoles.

Alternatively you can navigate the UI using a screen reader or inspect using developer tools and you will get the same results.

image

Maybe for the cases like this (when we need root element of the component) we should have some testing ID convention in place? So all components will have some testing id on root element?

No need to use a test id to get the root element of the component, you can use container.firstChild.
https://testing-library.com/docs/react-testing-library/api/#container-1

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The downside of using the container rule is that at the moment we'd have to disable both the no container and no dom access linting rules for each test that we use it in.

I'll fix this PR up with whatever we decide in: #847.

Copy link
Member

Choose a reason for hiding this comment

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

I'll approve and merge it as is for now, not to block RTL migration any longer.
But once we have all the tests in place would be good to get back, decides any rules/good practices and follow up according to them.

Copy link
Member

@bartaz bartaz left a comment

Choose a reason for hiding this comment

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

I'll approve and merge it as is for now, not to block RTL migration any longer.
But once we have all the tests in place would be good to get back, decides any rules/good practices and follow up according to them.

@bartaz bartaz merged commit b183e4d into canonical:main Nov 14, 2022
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.

4 participants