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

DOP-2642: Use React Testing Library instead of Enzyme #528

Merged
merged 11 commits into from
Jan 21, 2022
Merged

Conversation

casthewiz
Copy link
Contributor

@casthewiz casthewiz commented Jan 20, 2022

Stories/Links:

DOP-2642

Staging Links:

N/A - no source files changed.

Notes:

Changes all of our tests to render with RTL instead of enzyme.

Enzyme will remain in our package.json and package-lock files but will be removed in the changeset for the actual React 17 upgrade.

Quirky/Awkward Selectors:

React Testing Library is meant to work with the actual dom elements rendered by our React components, with queries targeting display text, labels, roles, titles, or alt text, etc.

So there are some scattered... quirky selectors in our tests now, eg:
expect(versionDropdown.parentElement).toHaveAttribute('aria-disabled', 'true');
where the versionDropdown was selected via the rendered text, but the element which is disabled is actually the parent element.

There's some scattered instances of mySelectedElement.closest(<tag I actually want to assert on>) also - I've tried to add TODO's where it seems like we could change the implementation to make testing (and accessibility) a little better.

Removed Tests

Some of our test suites either had redundant tests (FeedbackWidget and useFeedbackState tested many of the same things) or tests that couldn't be accommodated easily (Sidenav has tests across viewpoints, and the css when rendered wouldn't update when the viewport was changed - jsdom limitation with styled components; jsdom powers React Testing Library).

I've tried to add TODO's where it seemed like the tests could be re-added in the future, or with sufficient time, but if anything stands out, please feel free to highlight!

Copy link
Collaborator

@rayangler rayangler left a comment

Choose a reason for hiding this comment

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

Amazing work on all of this! Mostly just nitpicks and clarifying questions for now

package.json Show resolved Hide resolved
tests/unit/Chapters.test.js Outdated Show resolved Hide resolved
tests/unit/Chapters.test.js Outdated Show resolved Hide resolved
tests/unit/DeprecatedVersionSelector.test.js Outdated Show resolved Hide resolved
tests/unit/Pagination.test.js Outdated Show resolved Hide resolved
tests/unit/ProductsList.test.js Outdated Show resolved Hide resolved
tests/unit/QuizWidget.test.js Outdated Show resolved Hide resolved
tests/unit/SearchResults.test.js Outdated Show resolved Hide resolved
tests/unit/Sidenav.test.js Outdated Show resolved Hide resolved
const wrapper = await mountSidenav();
expect(findCollapseButton(wrapper)).toHaveAttribute('aria-expanded', 'true');

// js-dom isn't properly reflecting styled components updating active css across media queries
Copy link
Collaborator

Choose a reason for hiding this comment

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

Would it be better to move all of our components that use emotion's styled to just use css for easier testing? Or would switching to css not fix this problem?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Just wanted to bump this question in case it got lost! No worries if this is something that is unknown for now

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure if switching to css would solve the problem - I'm also not very keen on trying to change the implementation of our css for what seems to be a testing library quirk 🤔

I'm not 100% sure why this isn't behaving as expected either - for now I've added a workaround similar to what I did for FeedbackWidget, where we try to assert that the style rule exists for display:none at mobile size viewports.

Copy link
Collaborator

@rayangler rayangler left a comment

Choose a reason for hiding this comment

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

LGTM!

const wrapper = await mountSidenav();
expect(findCollapseButton(wrapper)).toHaveAttribute('aria-expanded', 'true');

// js-dom isn't properly reflecting styled components updating active css across media queries
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just wanted to bump this question in case it got lost! No worries if this is something that is unknown for now

@casthewiz casthewiz merged commit 2e6690b into master Jan 21, 2022
@casthewiz casthewiz deleted the DOP-2642 branch January 21, 2022 19:09
graysonhicks pushed a commit that referenced this pull request Jan 20, 2023
* DOP-2642: Update low/mid complexity tests to use RTL, not enzyme

* DOP-2642: Add ThemeProvider to all deeply referenced renders

* DOP-2642: Convert FeedbackWidget and Code tests to RTL

* DOP-2642: Update jest.config.js to remove enzyme and add jest-dom

* DOP-2642: Update Hook tests, mid-complexity tests

* DOP-2642: Everything except Redoc

* DOP-2642: OpenAPI tests mocked with RTL

* DOP-2642: Update Snapshots from merge

* DOP-2642: Pardon the dust

* DOP-2642: Workaround for css not updating properly across viewports
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.

2 participants