Skip to content

chore(BackToTop) update tests to new standarts#7833

Merged
tlabaj merged 6 commits intopatternfly:mainfrom
dominik-petrik:backToTop
Sep 7, 2022
Merged

chore(BackToTop) update tests to new standarts#7833
tlabaj merged 6 commits intopatternfly:mainfrom
dominik-petrik:backToTop

Conversation

@dominik-petrik
Copy link
Copy Markdown
Contributor

What: Closes #7813

Additional issues: There seems to be an issue with toBeVisible(). When component visibility is controlled by css property, toBeVisible reports the component as visible even though it has css property that should hide the component (eg. display: none)

Probably a JSDOM limitation. Actually, the problem is that the css isn't actually loaded into the document in JSDOM. If it were, then that would work. If you can come up with a custom jest transform that could insert the css file into the document during tests, then you'd be set.

I've worked around that by checking for hidden class instead("pf-m-hidden").

@patternfly-build
Copy link
Copy Markdown
Collaborator

patternfly-build commented Aug 16, 2022

@dominik-petrik dominik-petrik marked this pull request as draft August 17, 2022 12:59
@dominik-petrik dominik-petrik marked this pull request as ready for review August 17, 2022 14:11
@nicolethoen
Copy link
Copy Markdown
Contributor

@wise-king-sullyman
I'm trying to learn and absorb as much as I can by watching you review these test revamp PRs.
In his case, I see @dominik-petrik is using getByTestId('backtotop') to query for the test results. In the case where the rendered component is a role-less div is this the best way to query? Or is there a better alternative?

@nicolethoen
Copy link
Copy Markdown
Contributor

In the case of the BackToTop component, there is a button inside the component, is that something we could/should be querying by?

@wise-king-sullyman
Copy link
Copy Markdown
Collaborator

@nicolethoen IMO yeah for anything that can be ascertained by grabbing the button with a getByRole query that would definitely be preferable.

For things that we need to grab the containing div for it's a bit more up to debate. I think I would rather see us grab the button, then reach up from it and grab its parent rather than getting the div directly using a test id here, but that wouldn't be universally true.

Copy link
Copy Markdown
Collaborator

@wise-king-sullyman wise-king-sullyman left a comment

Choose a reason for hiding this comment

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

This is a strong first swing at this! I do have a few requests for additional tests and like @nicolethoen would like to see some tweaks to the queries being used though.

Copy link
Copy Markdown
Contributor

@thatblindgeye thatblindgeye left a comment

Choose a reason for hiding this comment

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

Awesome work on this! Other than Austn's comment, only had one comment below

Comment on lines +58 to +59
expect(screen.getByTestId('backtotop').firstChild).toBeVisible();
expect(screen.getByTestId('backtotop').firstChild).not.toHaveClass('pf-m-hidden');
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Since the earlier tests are just checking for the pf-m-hidden class, could we remove the toBeVisible line here?

@dominik-petrik dominik-petrik marked this pull request as draft August 30, 2022 11:58
Copy link
Copy Markdown
Collaborator

@wise-king-sullyman wise-king-sullyman left a comment

Choose a reason for hiding this comment

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

I have a few nits that I would like to see addressed, but none are true blockers so I'm happy to approve 🙂

Really awesome work on this!

Comment on lines +10 to +11
<p>{variant}</p>
<p>{iconPosition}</p>
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nit: can you update these to have some additional text which you include in your getByText query to ensure that we don't get false positive because of text elsewhere in the component?

Suggested change
<p>{variant}</p>
<p>{iconPosition}</p>
<p>Variant: {variant}</p>
<p>Icon position: {iconPosition}</p>

expect(screen.getByRole(`button`).parentElement).toHaveClass('pf-m-hidden');
});

test('Renders BackToTop when isAlwaysVisible prop is set', () => {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nit: can you adjust this test name since it isn't really testing that BackToTop renders, but rather that it renders without the hidden class?

test('Passes correct text content to button child component', () => {
render(<BackToTop title="Back to the future" />);

expect(screen.getByText('Back to the future')).toBeVisible();
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nit: here I would rather see us use the getByRole query to select the button and assert that it's text is what we expect using the toHaveTextContent matcher.

Comment on lines +150 to +154
test('Passes correct icon to button child component', () => {
render(<BackToTop />);

expect(screen.getByTestId('icon').firstChild).toBeTruthy();
});
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nit: for the icon I would rather see us do something like what I did for the angle right icon in the accordion toggle where we mock it out (since the SVG isn't actually a responsibility of BackToTop), and then I would just assert that the icon div contains the text you pass in for the mock so that we know it's being sent to Button properly.

@tlabaj tlabaj merged commit af109db into patternfly:main Sep 7, 2022
andyyvo pushed a commit to andyyvo/patternfly-react that referenced this pull request Sep 9, 2022
* chore(BackToTop) update tests to new standarts

* add more tests

* add a few more tests,  update queries

* add more tests, mocking in progress

* mock button child element, test props passed to it

* update user-event calls
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Back to top test revamp

7 participants